Guest User

Untitled

a guest
Nov 13th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. """
  2. A homeassistant switch component to enable or disable fritz! box call deflections / call forwarding
  3. tr 064 needs to be enabled, and call deflections have to be pre-defined in the box's ui.
  4.  
  5. removing and adding calldeflections while homeassistant is running will break this ;-)
  6. """
  7. import logging
  8.  
  9. import voluptuous as vol
  10.  
  11. from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
  12. from homeassistant.const import (CONF_HOST, CONF_PORT, CONF_PASSWORD, CONF_USERNAME)
  13. import homeassistant.helpers.config_validation as cv
  14.  
  15. REQUIREMENTS = ['fritzconnection==0.6.5']
  16.  
  17. _LOGGER = logging.getLogger(__name__)
  18.  
  19. DEFAULT_HOST = '169.254.1.1' # IP valid for all Fritz!Box routers
  20. DEFAULT_PORT = 49000
  21.  
  22. ATTR_TRIGGER_NUMBER = 'trigger_number'
  23. ATTR_TARGET_NUMBER = 'target_number'
  24. ATTR_UID = 'uid'
  25.  
  26. SERVICE = 'X_AVM-DE_OnTel'
  27.  
  28. PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
  29. vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
  30. vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
  31. vol.Required(CONF_PASSWORD): cv.string,
  32. vol.Optional(CONF_USERNAME, default=''): cv.string,
  33. })
  34.  
  35. def setup_platform(hass, config, add_entities, discovery_info=None):
  36. """Set up Fritz!Box call deflection switch platform."""
  37. host = config.get(CONF_HOST)
  38. port = config.get(CONF_PORT)
  39. username = config.get(CONF_USERNAME)
  40. password = config.get(CONF_PASSWORD)
  41.  
  42. from lxml import etree
  43. import fritzconnection as fc
  44.  
  45. fritz_connection = None;
  46. if username != '':
  47. fritz_connection = fc.FritzConnection(address=host, password=password, user=username, port=port)
  48. else:
  49. fritz_connection = fc.FritzConnection(address=host, password=password, port=port)
  50.  
  51. raw_deflections = fritz_connection.call_action(SERVICE, "GetDeflections")["NewDeflectionList"];
  52. element_tree = etree.fromstring(raw_deflections);
  53. switches = []
  54. for element in element_tree.findall('Item'):
  55. uid = element.find('DeflectionId').text
  56. enabled = int(element.find('Enable').text)
  57. from_number = element.find('Number').text
  58. to_number = element.find('DeflectionToNumber').text
  59. # forwardings without to_number are blocked numbers
  60. if to_number is None:
  61. continue
  62.  
  63. switches.append(FritzCallDeflectSwitch(hass=hass, fritz_connection=fritz_connection, uid=uid, from_number=from_number, to_number=to_number, enabled=enabled))
  64.  
  65. add_entities(switches);
  66.  
  67. class FritzCallDeflectSwitch(SwitchDevice):
  68. """Representation of a FRITZ! call deflection switch."""
  69.  
  70. def __init__(self, hass, fritz_connection, uid, from_number, to_number, enabled):
  71. """Initialize the switch."""
  72. self._hass = hass;
  73. self._fritz_connection = fritz_connection;
  74. self._uid = uid;
  75. self._from_number = from_number;
  76. self._to_number = to_number;
  77. self._enabled = enabled;
  78.  
  79. @property
  80. def name(self):
  81. """Return the name of the FRITZ! call deflection (just the uid)"""
  82. return "fritzdeflect_" + str(self._uid);
  83.  
  84. @property
  85. def device_state_attributes(self):
  86. """Return the state attributes of the call deflection."""
  87. attrs = {}
  88.  
  89. attrs[ATTR_TRIGGER_NUMBER] = "{}".format(self._from_number)
  90. attrs[ATTR_TARGET_NUMBER] = "{}".format(self._to_number)
  91. attrs[ATTR_UID] = "{}".format(self._uid);
  92.  
  93. return attrs
  94.  
  95. @property
  96. def is_on(self):
  97. """Return true if switch is on."""
  98. return self._enabled;
  99.  
  100. def turn_on(self, **kwargs):
  101. """Turn the switch on."""
  102. myargs = {'NewDeflectionId': self._uid, 'NewEnable': 1 }
  103. self._fritz_connection.call_action(SERVICE, "SetDeflectionEnable", **myargs);
  104.  
  105. def turn_off(self, **kwargs):
  106. """Turn the switch off."""
  107. myargs = {'NewDeflectionId': self._uid, 'NewEnable': 0 }
  108. self._fritz_connection.call_action(SERVICE, "SetDeflectionEnable", **myargs);
  109.  
  110. def update(self):
  111. """Get the latest data from the fritz box and update the state"""
  112. kwargs = {'NewDeflectionId': self._uid }
  113. updated_dict = self._fritz_connection.call_action(SERVICE, "GetDeflection", **kwargs);
  114. self._from_number = updated_dict['NewNumber'];
  115. self._to_number = updated_dict['NewDeflectionToNumber'];
  116. self._enabled = int(updated_dict['NewEnable']);
Add Comment
Please, Sign In to add comment