From 18803e8e507c9b44934e15320c1bbed7fa30b788 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 31 Jul 2023 20:26:21 -0700 Subject: [PATCH] Revert is_proactively_adapting checks This reverts commit 39fd8f2be0f3d6bef27705d167b5eab8113d5709. --- custom_components/adaptive_lighting/switch.py | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 4e621a96..f635c23b 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1273,6 +1273,21 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): _LOGGER.debug("%s: '%s' is locked", self._name, light) return + if context.parent_id is not None and self.manager.is_proactively_adapting( + context.parent_id, + ): + # Skip if adaptation was already executed by the service call interceptor. + # The context.parent_id is the context.id of the service call that was intercepted + # and context.id here is from the resulting "light_event" event. + _LOGGER.debug( + "%s: Skipping reactive adaptation of light %s with context.id=%s and context.parent_id=%s", + self._name, + light, + context.id, + context.parent_id, + ) + return + data = await self.prepare_adaptation_data( light, transition, @@ -1459,9 +1474,9 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): await self._adapt_light(light, context, transition) async def _respond_to_off_to_on_event(self, entity_id: str, event: Event) -> None: - assert not self.manager.is_proactively_adapting(event.context.id) if ( not self._detect_non_ha_changes + and not self.manager.is_proactively_adapting(event.context.id) and not self.manager._off_to_on_state_event_is_from_turn_on( entity_id, event, @@ -2454,18 +2469,16 @@ class AdaptiveLightingManager: entity_id, event.context.id, ) + # Skip resetting here because `_service_interceptor_turn_on_handler` + # ran *just* before this and already reset the light and populated + # `self.last_service_data` with the new data. + if not self.is_proactively_adapting(event.context.id): + self.reset(entity_id, reset_manual_control=False) - if self.is_proactively_adapting(event.context.id): - _LOGGER.debug( - "Skipping responding to 'off' → 'on' event for '%s' with context.id='%s' because" - " we are already proactively adapting", - entity_id, - event.context.id, - ) - # Note: the reset below already happened in `_service_interceptor_turn_on_handler` - return + # We do not break out here if we are proactively adapting because + # we need the manual_control setting of _respond_to_off_to_on_event. + # It will just bail out before adapting if proactively adapting. - self.reset(entity_id, reset_manual_control=False) lock = self.turn_off_locks.setdefault(entity_id, asyncio.Lock()) async with lock: if await self.just_turned_off(entity_id):