From a0c1202ad6d61ef8f57eba2480ecbaac07d0dd25 Mon Sep 17 00:00:00 2001 From: Stefan Jonasson Date: Wed, 23 Sep 2015 08:26:40 +0200 Subject: [PATCH] Try to make the connection to the tellcore library more stable --- homeassistant/components/light/tellstick.py | 10 +++++++++- homeassistant/components/switch/tellstick.py | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index 19ce1a06d4a..de612ea7551 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -41,9 +41,17 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Called from the TelldusCore library to update one device """ for light_device in lights: if light_device.tellstick_device.id == id_: + # Execute the update in another thread + threading.Thread(target=light_device.update_ha_state, daemon=False, args=(True,)).start() light_device.update_ha_state(True) - core.register_device_event(_device_event_callback) + callback_id = core.register_device_event(_device_event_callback) + + def unload_telldus_lib(): + if callback_id is not None: + core.unregister_callback(callback_id) + + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, unload_telldus_lib) add_devices_callback(lights) diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index 96b10a0a977..c17347bd587 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -52,9 +52,17 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ 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) + # Execute the update in another thread + threading.Thread(target=switch_device.update_ha_state, daemon=False).start() + break - core.register_device_event(_device_event_callback) + callback_id = core.register_device_event(_device_event_callback) + + def unload_telldus_lib(): + if callback_id is not None: + core.unregister_callback(callback_id) + + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, unload_telldus_lib) add_devices_callback(switches)