From 00047009e2bfa1b6cc3de8267cfbfd1e1ef0d27a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 1 Mar 2015 11:02:11 -0800 Subject: [PATCH] Only poll for device updates if necessary --- homeassistant/helpers/device_component.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/device_component.py b/homeassistant/helpers/device_component.py index 41c10ae90d9..7df15d87533 100644 --- a/homeassistant/helpers/device_component.py +++ b/homeassistant/helpers/device_component.py @@ -25,6 +25,7 @@ class DeviceComponent(object): self.devices = {} self.group = None + self.is_polling = False def setup(self, config): """ @@ -47,9 +48,6 @@ class DeviceComponent(object): self._setup_platform(p_type, p_config) - self.hass.track_time_change(self._update_device_states, - second=range(0, 60, self.scan_interval)) - if self.discovery_platforms: discovery.listen(self.hass, self.discovery_platforms.keys(), self._device_discovered) @@ -88,6 +86,20 @@ class DeviceComponent(object): if self.group is not None: self.group.update_tracked_entity_ids(self.devices.keys()) + self._start_polling() + + def _start_polling(self): + """ Start polling device states if necessary. """ + if self.is_polling or \ + not any(device.should_poll for device in self.devices.values()): + return + + self.is_polling = True + + self.hass.track_time_change( + self._update_device_states, + second=range(0, 60, self.scan_interval)) + def _setup_platform(self, platform_type, config, discovery_info=None): """ Tries to setup a platform for this component. """ platform_name = '{}.{}'.format(self.domain, platform_type)