From 435b2ce5d0c82b24659bf295999cc76b2f48a2c4 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 9 Aug 2023 14:48:18 -0700 Subject: [PATCH] Fix adapt_only_on_bare_turn_on and apply does not result in manual_control (#729) * Fix adapt_only_on_bare_turn_on and apply does not result in manual_control Closes #723 * fix style --- custom_components/adaptive_lighting/switch.py | 8 ++++++-- tests/test_switch.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 89106b45..150f2ef4 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1376,9 +1376,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): timer = self.manager.transition_timers.get(light) if timer is not None and timer.is_running(): _LOGGER.debug( - "%s: Light '%s' is still transitioning", + "%s: Light '%s' is still transitioning, context.id='%s'", self._name, light, + context.id, ) elif ( # This is to prevent lights immediately turning on after @@ -1390,9 +1391,10 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): and turn_off.time_fired > turn_on.time_fired ): _LOGGER.debug( - "%s: Light '%s' was turned just turned off", + "%s: Light '%s' was turned just turned off, context.id='%s'", self._name, light, + context.id, ) else: filtered_lights.append(light) @@ -1489,6 +1491,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self._take_over_control and self._adapt_only_on_bare_turn_on and from_turn_on + # adaptive_lighting.apply can turn on light, so check this is not our context + and not is_our_context(event.context) ): service_data = self.manager.turn_on_event[entity_id].data[ATTR_SERVICE_DATA] if self.manager._mark_manual_control_if_non_bare_turn_on( diff --git a/tests/test_switch.py b/tests/test_switch.py index 6ffd1a21..4744f878 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -740,6 +740,22 @@ async def test_manual_control( await change_manual_control(False, {}) assert all([not manual_control[eid] for eid in switch.lights]) + # Turn off light and turn on using adaptive_lighting.apply + await turn_light(False) + await hass.services.async_call( + DOMAIN, + SERVICE_APPLY, + { + ATTR_ENTITY_ID: ENTITY_SWITCH, + CONF_LIGHTS: [ENTITY_LIGHT_1], + CONF_TURN_ON_LIGHTS: True, + }, + blocking=True, + ) + await hass.async_block_till_done() + assert hass.states.get(ENTITY_LIGHT_1).state == STATE_ON + assert not manual_control[ENTITY_LIGHT_1] + async def test_auto_reset_manual_control(hass): switch, (light, *_) = await setup_lights_and_switch(