From 1e2b5e699114ba4e54dc77a56858cdcd2f29a87c Mon Sep 17 00:00:00 2001 From: pavoni Date: Wed, 23 Dec 2015 15:46:18 +0000 Subject: [PATCH 1/9] Add support for subscriptions --- homeassistant/components/switch/wemo.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index bad471ce437..9924153033e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -11,9 +11,10 @@ import logging from homeassistant.components.switch import SwitchDevice from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY -REQUIREMENTS = ['pywemo==0.3.3'] +# REQUIREMENTS = ['pywemo==0.3.3'] _LOGGER = logging.getLogger(__name__) +_WEMO_SUBSCRIPTION_REGISTRY = None # pylint: disable=unused-argument, too-many-function-args def setup_platform(hass, config, add_devices_callback, discovery_info=None): @@ -21,6 +22,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): import pywemo import pywemo.discovery as discovery + global _WEMO_SUBSCRIPTION_REGISTRY + if _WEMO_SUBSCRIPTION_REGISTRY is None: + _WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry() + _WEMO_SUBSCRIPTION_REGISTRY.start() + if discovery_info is not None: location = discovery_info[2] mac = discovery_info[3] @@ -47,6 +53,21 @@ class WemoSwitch(SwitchDevice): self.insight_params = None self.maker_params = None + global _WEMO_SUBSCRIPTION_REGISTRY + _WEMO_SUBSCRIPTION_REGISTRY.register(wemo) + _WEMO_SUBSCRIPTION_REGISTRY.on(wemo, 'BinaryState', self._update_callback) + _WEMO_SUBSCRIPTION_REGISTRY.on(wemo, 'attributeList', self._update_callback) + + def _update_callback(self, _device, _params): + _LOGGER.info('Subscription update for %s, sevice=%s params=%s', self.name, _device, _params) + # import pdb; pdb.set_trace() + self.update() + + @property + def should_poll(self): + """ No polling should be needed with subscriptions, but leave in for initial version in case of issues. """ + return True + @property def unique_id(self): """ Returns the id of this WeMo switch """ From 09b894a4aac762cfd73e87c0685edd160b5335bd Mon Sep 17 00:00:00 2001 From: pavoni Date: Wed, 23 Dec 2015 15:57:51 +0000 Subject: [PATCH 2/9] Fix style issues --- homeassistant/components/switch/wemo.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 9924153033e..7e3caf92e6e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -16,6 +16,7 @@ _LOGGER = logging.getLogger(__name__) _WEMO_SUBSCRIPTION_REGISTRY = None + # pylint: disable=unused-argument, too-many-function-args def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Find and return WeMo switches. """ @@ -23,7 +24,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): import pywemo.discovery as discovery global _WEMO_SUBSCRIPTION_REGISTRY - if _WEMO_SUBSCRIPTION_REGISTRY is None: + if _WEMO_SUBSCRIPTION_REGISTRY is None: _WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry() _WEMO_SUBSCRIPTION_REGISTRY.start() @@ -53,19 +54,23 @@ class WemoSwitch(SwitchDevice): self.insight_params = None self.maker_params = None - global _WEMO_SUBSCRIPTION_REGISTRY _WEMO_SUBSCRIPTION_REGISTRY.register(wemo) - _WEMO_SUBSCRIPTION_REGISTRY.on(wemo, 'BinaryState', self._update_callback) - _WEMO_SUBSCRIPTION_REGISTRY.on(wemo, 'attributeList', self._update_callback) + _WEMO_SUBSCRIPTION_REGISTRY.on( + wemo, 'BinaryState', self._update_callback) + _WEMO_SUBSCRIPTION_REGISTRY.on( + wemo, 'attributeList', self._update_callback) def _update_callback(self, _device, _params): - _LOGGER.info('Subscription update for %s, sevice=%s params=%s', self.name, _device, _params) - # import pdb; pdb.set_trace() + """ Called by the wemo device callback to update state. """ + _LOGGER.info( + 'Subscription update for %s, sevice=%s params=%s', + self.name, _device, _params) self.update() @property def should_poll(self): - """ No polling should be needed with subscriptions, but leave in for initial version in case of issues. """ + """ No polling should be needed with subscriptions """ + # but leave in for initial version in case of issues. return True @property From 6d236b81690554bba5a00ae29d79ceead4d32711 Mon Sep 17 00:00:00 2001 From: pavoni Date: Wed, 23 Dec 2015 18:03:40 +0000 Subject: [PATCH 3/9] Force state update --- homeassistant/components/switch/wemo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 7e3caf92e6e..f3aeac4c84e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -66,6 +66,7 @@ class WemoSwitch(SwitchDevice): 'Subscription update for %s, sevice=%s params=%s', self.name, _device, _params) self.update() + self.update_ha_state() @property def should_poll(self): From 3f151428b701ed7d1c8bf93861ec36cd8fa95df0 Mon Sep 17 00:00:00 2001 From: pavoni Date: Thu, 24 Dec 2015 09:35:02 +0000 Subject: [PATCH 4/9] Update pywemo version, use wildcard filter --- homeassistant/components/switch/wemo.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index f3aeac4c84e..1861b42f450 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -11,7 +11,7 @@ import logging from homeassistant.components.switch import SwitchDevice from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY -# REQUIREMENTS = ['pywemo==0.3.3'] +REQUIREMENTS = ['pywemo==0.3.4'] _LOGGER = logging.getLogger(__name__) _WEMO_SUBSCRIPTION_REGISTRY = None @@ -56,9 +56,7 @@ class WemoSwitch(SwitchDevice): _WEMO_SUBSCRIPTION_REGISTRY.register(wemo) _WEMO_SUBSCRIPTION_REGISTRY.on( - wemo, 'BinaryState', self._update_callback) - _WEMO_SUBSCRIPTION_REGISTRY.on( - wemo, 'attributeList', self._update_callback) + wemo, None, self._update_callback) def _update_callback(self, _device, _params): """ Called by the wemo device callback to update state. """ From 4feef3dd0d12b392041c3f12d0b1c5b8da9e1a82 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 27 Dec 2015 23:09:39 +0000 Subject: [PATCH 5/9] Simplify update state call, shutdown properly. --- homeassistant/components/switch/wemo.py | 8 ++++++-- requirements_all.txt | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 1861b42f450..c15ef4612f0 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -27,6 +27,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if _WEMO_SUBSCRIPTION_REGISTRY is None: _WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry() _WEMO_SUBSCRIPTION_REGISTRY.start() + def stop_wemo(event): + """ Shutdown Wemo subscriptions and subscription thread on exit""" + _WEMO_SUBSCRIPTION_REGISTRY.stop() + + hass.bus.listen_once(hass.EVENT_HOMEASSISTANT_STOP, stop_wemo) if discovery_info is not None: location = discovery_info[2] @@ -63,8 +68,7 @@ class WemoSwitch(SwitchDevice): _LOGGER.info( 'Subscription update for %s, sevice=%s params=%s', self.name, _device, _params) - self.update() - self.update_ha_state() + self.update_ha_state(True) @property def should_poll(self): diff --git a/requirements_all.txt b/requirements_all.txt index 2375721fa7d..8494573de42 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -174,7 +174,7 @@ hikvision==0.4 orvibo==1.1.0 # homeassistant.components.switch.wemo -pywemo==0.3.3 +pywemo==0.3.4 # homeassistant.components.thermostat.heatmiser heatmiserV3==0.9.1 From 0d32bd7a19116621e0865e2a8067694d363906cc Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 27 Dec 2015 23:26:35 +0000 Subject: [PATCH 6/9] Simplify callback logging, add stop log --- homeassistant/components/switch/wemo.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index c15ef4612f0..41d7034425a 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -29,6 +29,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): _WEMO_SUBSCRIPTION_REGISTRY.start() def stop_wemo(event): """ Shutdown Wemo subscriptions and subscription thread on exit""" + _LOGGER.info("Shutting down subscriptions.") _WEMO_SUBSCRIPTION_REGISTRY.stop() hass.bus.listen_once(hass.EVENT_HOMEASSISTANT_STOP, stop_wemo) @@ -66,8 +67,8 @@ class WemoSwitch(SwitchDevice): def _update_callback(self, _device, _params): """ Called by the wemo device callback to update state. """ _LOGGER.info( - 'Subscription update for %s, sevice=%s params=%s', - self.name, _device, _params) + 'Subscription update for %s, sevice=%s', + self.name, _device) self.update_ha_state(True) @property From b114ba56ea58e03ca3a22286f082e4b7eb63199d Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 27 Dec 2015 23:31:48 +0000 Subject: [PATCH 7/9] Fix style issue --- homeassistant/components/switch/wemo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 41d7034425a..8d91beeaf5e 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -27,6 +27,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if _WEMO_SUBSCRIPTION_REGISTRY is None: _WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry() _WEMO_SUBSCRIPTION_REGISTRY.start() + def stop_wemo(event): """ Shutdown Wemo subscriptions and subscription thread on exit""" _LOGGER.info("Shutting down subscriptions.") From f5dd1466767a5c94e903e0eae7d2f38383251e4b Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 27 Dec 2015 23:56:18 +0000 Subject: [PATCH 8/9] Fix bug in shutdown --- homeassistant/components/switch/wemo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 8d91beeaf5e..97712493e5f 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -9,7 +9,7 @@ https://home-assistant.io/components/switch.wemo/ import logging from homeassistant.components.switch import SwitchDevice -from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY +from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY, EVENT_HOMEASSISTANT_STOP REQUIREMENTS = ['pywemo==0.3.4'] _LOGGER = logging.getLogger(__name__) @@ -33,7 +33,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): _LOGGER.info("Shutting down subscriptions.") _WEMO_SUBSCRIPTION_REGISTRY.stop() - hass.bus.listen_once(hass.EVENT_HOMEASSISTANT_STOP, stop_wemo) + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo) if discovery_info is not None: location = discovery_info[2] From 976d9f2d08294c13786e9f91c8bbd252ad997f24 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 28 Dec 2015 00:04:30 +0000 Subject: [PATCH 9/9] Fix style issue --- homeassistant/components/switch/wemo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index 97712493e5f..a343711ccc3 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -9,7 +9,8 @@ https://home-assistant.io/components/switch.wemo/ import logging from homeassistant.components.switch import SwitchDevice -from homeassistant.const import STATE_ON, STATE_OFF, STATE_STANDBY, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import ( + STATE_ON, STATE_OFF, STATE_STANDBY, EVENT_HOMEASSISTANT_STOP) REQUIREMENTS = ['pywemo==0.3.4'] _LOGGER = logging.getLogger(__name__)