diff --git a/homeassistant/components/switch/wemo.py b/homeassistant/components/switch/wemo.py index bad471ce437..a343711ccc3 100644 --- a/homeassistant/components/switch/wemo.py +++ b/homeassistant/components/switch/wemo.py @@ -9,11 +9,14 @@ 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.3'] +REQUIREMENTS = ['pywemo==0.3.4'] _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 +24,18 @@ 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() + + 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(EVENT_HOMEASSISTANT_STOP, stop_wemo) + if discovery_info is not None: location = discovery_info[2] mac = discovery_info[3] @@ -47,6 +62,23 @@ class WemoSwitch(SwitchDevice): self.insight_params = None self.maker_params = None + _WEMO_SUBSCRIPTION_REGISTRY.register(wemo) + _WEMO_SUBSCRIPTION_REGISTRY.on( + wemo, None, self._update_callback) + + def _update_callback(self, _device, _params): + """ Called by the wemo device callback to update state. """ + _LOGGER.info( + 'Subscription update for %s, sevice=%s', + self.name, _device) + self.update_ha_state(True) + + @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 """ diff --git a/requirements_all.txt b/requirements_all.txt index 4af86d6dade..a9ec467e8b1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -173,7 +173,7 @@ hikvision==0.4 orvibo==1.1.0 # homeassistant.components.switch.wemo -pywemo==0.3.3 +pywemo==0.3.4 # homeassistant.components.tellduslive tellive-py==0.5.2