From 597e050ab2a769cb9ef5fed5df9403f8d36cdb85 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 9 Aug 2023 13:54:18 -0700 Subject: [PATCH] Prevent light.turn_on of light that was just turned off (#727) Closes #726 --- custom_components/adaptive_lighting/switch.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 35b1d7e5..89106b45 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1120,6 +1120,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self.manager.reset(*self.lights) async def _async_update_at_interval_action(self, now=None) -> None: # noqa: ARG002 + """Update the attributes and maybe adapt the lights.""" await self._update_attrs_and_maybe_adapt_lights( context=self.create_context("interval"), transition=self._transition, @@ -1331,7 +1332,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): data, ) - async def _update_attrs_and_maybe_adapt_lights( + async def _update_attrs_and_maybe_adapt_lights( # noqa: PLR0912 self, *, context: Context, @@ -1379,6 +1380,20 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self._name, light, ) + elif ( + # This is to prevent lights immediately turning on after + # being turned off in 'interval' update, see #726 + not self._detect_non_ha_changes + and is_our_context(context, "interval") + and (turn_on := self.manager.turn_on_event.get(light)) + and (turn_off := self.manager.turn_off_event.get(light)) + and turn_off.time_fired > turn_on.time_fired + ): + _LOGGER.debug( + "%s: Light '%s' was turned just turned off", + self._name, + light, + ) else: filtered_lights.append(light)