From ab81231e6dd36c457884dc0a12a0a0a8fa498595 Mon Sep 17 00:00:00 2001 From: Stefan Jonasson Date: Sun, 20 Sep 2015 14:11:42 +0200 Subject: [PATCH 1/2] Changed flow so we got one callback per platorm instead of per device which caused race conditions in the telldus library. --- homeassistant/components/light/tellstick.py | 16 ++++++++-------- homeassistant/components/switch/tellstick.py | 19 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index 91c48f68e70..32fee78e305 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -35,6 +35,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): for switch in switches_and_lights: if switch.methods(tellcore_constants.TELLSTICK_DIM): lights.append(TellstickLight(switch, core)) + + def _device_event_callback(id_, method, data, cid): + for light_device in lights: + if light_device.tellstick_device.id == id_: + light_device.update_ha_state(True) + + core.register_device_event(_device_event_callback) + add_devices_callback(lights) @@ -50,14 +58,6 @@ class TellstickLight(Light): 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): diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index 3b510738b06..a61cc8db28b 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -46,7 +46,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): for switch in switches_and_lights: if not switch.methods(tellcore_constants.TELLSTICK_DIM): switches.append( - TellstickSwitchDevice(switch, signal_repetitions, core)) + TellstickSwitchDevice(switch, signal_repetitions)) + + def _device_event_callback(id_, method, data, cid): + for switch_device in switches: + if switch_device.tellstick_device.id == id_: + switch_device.update_ha_state(True) + + core.register_device_event(_device_event_callback) add_devices_callback(switches) @@ -56,18 +63,10 @@ class TellstickSwitchDevice(ToggleEntity): last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON | tellcore_constants.TELLSTICK_TURNOFF) - def __init__(self, tellstick_device, signal_repetitions, core): + def __init__(self, tellstick_device, signal_repetitions): 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): From a60a9202a5d6ad1e7ab53f7b3be7e2286923dcff Mon Sep 17 00:00:00 2001 From: Stefan Jonasson Date: Sun, 20 Sep 2015 14:17:32 +0200 Subject: [PATCH 2/2] cleanup --- homeassistant/components/light/tellstick.py | 6 ++++-- homeassistant/components/switch/tellstick.py | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index 32fee78e305..3f7002b5b03 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -34,9 +34,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): for switch in switches_and_lights: if switch.methods(tellcore_constants.TELLSTICK_DIM): - lights.append(TellstickLight(switch, core)) + lights.append(TellstickLight(switch)) + # pylint: disable=unused-argument def _device_event_callback(id_, method, data, cid): + """ Called from the TelldusCore library to update one device """ for light_device in lights: if light_device.tellstick_device.id == id_: light_device.update_ha_state(True) @@ -54,7 +56,7 @@ class TellstickLight(Light): tellcore_constants.TELLSTICK_UP | tellcore_constants.TELLSTICK_DOWN) - def __init__(self, tellstick_device, core): + def __init__(self, tellstick_device): self.tellstick_device = tellstick_device self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name} self._brightness = 0 diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index a61cc8db28b..8f0150543de 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -48,7 +48,9 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): switches.append( TellstickSwitchDevice(switch, signal_repetitions)) + # pylint: disable=unused-argument def _device_event_callback(id_, method, data, cid): + """ Called from the TelldusCore library to update one device """ for switch_device in switches: if switch_device.tellstick_device.id == id_: switch_device.update_ha_state(True)