diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index 8068d20bb74..91c48f68e70 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -8,7 +8,7 @@ import logging from homeassistant.components.light import Light, ATTR_BRIGHTNESS from homeassistant.const import ATTR_FRIENDLY_NAME import tellcore.constants as tellcore_constants - +from tellcore.library import DirectCallbackDispatcher REQUIREMENTS = ['tellcore-py==1.0.4'] @@ -22,13 +22,19 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): "Failed to import tellcore") return [] - core = telldus.TelldusCore() + # pylint: disable=no-member + if telldus.TelldusCore.callback_dispatcher is None: + dispatcher = DirectCallbackDispatcher() + core = telldus.TelldusCore(callback_dispatcher=dispatcher) + else: + core = telldus.TelldusCore() + switches_and_lights = core.devices() lights = [] for switch in switches_and_lights: if switch.methods(tellcore_constants.TELLSTICK_DIM): - lights.append(TellstickLight(switch)) + lights.append(TellstickLight(switch, core)) add_devices_callback(lights) @@ -40,15 +46,23 @@ class TellstickLight(Light): tellcore_constants.TELLSTICK_UP | tellcore_constants.TELLSTICK_DOWN) - def __init__(self, tellstick): - self.tellstick = tellstick - self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name} + def __init__(self, tellstick_device, core): + self.tellstick_device = tellstick_device + self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name} self._brightness = 0 + self.callback_id = core.register_device_event(self._device_event) + + # pylint: disable=unused-argument + def _device_event(self, id_, method, data, cid): + """ Called when a state has changed . """ + + if self.tellstick_device.id == id_: + self.update_ha_state() @property def name(self): """ Returns the name of the switch if any. """ - return self.tellstick.name + return self.tellstick_device.name @property def is_on(self): @@ -62,7 +76,7 @@ class TellstickLight(Light): def turn_off(self, **kwargs): """ Turns the switch off. """ - self.tellstick.turn_off() + self.tellstick_device.turn_off() self._brightness = 0 def turn_on(self, **kwargs): @@ -74,11 +88,11 @@ class TellstickLight(Light): else: self._brightness = brightness - self.tellstick.dim(self._brightness) + self.tellstick_device.dim(self._brightness) def update(self): """ Update state of the light. """ - last_command = self.tellstick.last_sent_command( + last_command = self.tellstick_device.last_sent_command( self.last_sent_command_mask) if last_command == tellcore_constants.TELLSTICK_TURNON: @@ -88,6 +102,11 @@ class TellstickLight(Light): elif (last_command == tellcore_constants.TELLSTICK_DIM or last_command == tellcore_constants.TELLSTICK_UP or last_command == tellcore_constants.TELLSTICK_DOWN): - last_sent_value = self.tellstick.last_sent_value() + last_sent_value = self.tellstick_device.last_sent_value() if last_sent_value is not None: self._brightness = last_sent_value + + @property + def should_poll(self): + """ Tells Home Assistant not to poll this entity. """ + return False diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index ae064d4fdb8..3b510738b06 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -11,11 +11,10 @@ signal_repetitions: 3 """ import logging - from homeassistant.const import ATTR_FRIENDLY_NAME from homeassistant.helpers.entity import ToggleEntity import tellcore.constants as tellcore_constants - +from tellcore.library import DirectCallbackDispatcher SINGAL_REPETITIONS = 1 REQUIREMENTS = ['tellcore-py==1.0.4'] @@ -31,16 +30,23 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): "Failed to import tellcore") return + # pylint: disable=no-member + if telldus.TelldusCore.callback_dispatcher is None: + dispatcher = DirectCallbackDispatcher() + core = telldus.TelldusCore(callback_dispatcher=dispatcher) + else: + core = telldus.TelldusCore() + signal_repetitions = config.get('signal_repetitions', SINGAL_REPETITIONS) - core = telldus.TelldusCore() switches_and_lights = core.devices() switches = [] for switch in switches_and_lights: if not switch.methods(tellcore_constants.TELLSTICK_DIM): - switches.append(TellstickSwitchDevice(switch, signal_repetitions)) + switches.append( + TellstickSwitchDevice(switch, signal_repetitions, core)) add_devices_callback(switches) @@ -50,15 +56,28 @@ class TellstickSwitchDevice(ToggleEntity): last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON | tellcore_constants.TELLSTICK_TURNOFF) - def __init__(self, tellstick, signal_repetitions): - self.tellstick = tellstick - self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name} + def __init__(self, tellstick_device, signal_repetitions, core): + self.tellstick_device = tellstick_device + self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name} self.signal_repetitions = signal_repetitions + self.callback_id = core.register_device_event(self._device_event) + + # pylint: disable=unused-argument + def _device_event(self, id_, method, data, cid): + """ Called when a state has changed . """ + + if self.tellstick_device.id == id_: + self.update_ha_state() + + @property + def should_poll(self): + """ Tells Home Assistant not to poll this entity. """ + return False @property def name(self): """ Returns the name of the switch if any. """ - return self.tellstick.name + return self.tellstick_device.name @property def state_attributes(self): @@ -68,7 +87,7 @@ class TellstickSwitchDevice(ToggleEntity): @property def is_on(self): """ True if switch is on. """ - last_command = self.tellstick.last_sent_command( + last_command = self.tellstick_device.last_sent_command( self.last_sent_command_mask) return last_command == tellcore_constants.TELLSTICK_TURNON @@ -76,9 +95,9 @@ class TellstickSwitchDevice(ToggleEntity): def turn_on(self, **kwargs): """ Turns the switch on. """ for _ in range(self.signal_repetitions): - self.tellstick.turn_on() + self.tellstick_device.turn_on() def turn_off(self, **kwargs): """ Turns the switch off. """ for _ in range(self.signal_repetitions): - self.tellstick.turn_off() + self.tellstick_device.turn_off()