From 9f68fd918453ec7ac6d3d6efd83990c52b100d7e Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Sun, 30 Apr 2017 09:05:29 +0200 Subject: [PATCH] Flux switch: avoid updates when off (#7363) * Flux switch: avoid updates when off Calling turn_on when the switch is already on would orphan the existing time tracker, losing our ability to cancel it when turn_off is called. * Cleanups The self.is_on property can now be found from self.unsub_tracker, so get rid of the self._state attribute. Add an entry guard to turn_on, making further conditionals unnecessary. --- homeassistant/components/switch/flux.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/switch/flux.py b/homeassistant/components/switch/flux.py index 0c58fb2a182..034fb7a4262 100644 --- a/homeassistant/components/switch/flux.py +++ b/homeassistant/components/switch/flux.py @@ -113,7 +113,6 @@ class FluxSwitch(SwitchDevice): """Initialize the Flux switch.""" self._name = name self.hass = hass - self._state = state self._lights = lights self._start_time = start_time self._stop_time = stop_time @@ -133,15 +132,19 @@ class FluxSwitch(SwitchDevice): @property def is_on(self): """Return true if switch is on.""" - return self._state + return self.unsub_tracker is not None def turn_on(self, **kwargs): """Turn on flux.""" - if not self._state: # make initial update - self.flux_update() - self._state = True + if self.is_on: + return + + # make initial update + self.flux_update() + self.unsub_tracker = track_time_change(self.hass, self.flux_update, second=[0, 30]) + self.schedule_update_ha_state() def turn_off(self, **kwargs): @@ -150,7 +153,6 @@ class FluxSwitch(SwitchDevice): self.unsub_tracker() self.unsub_tracker = None - self._state = False self.schedule_update_ha_state() def flux_update(self, now=None):