From 8285564015b8479a61d7801712270db8bea0317d Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 3 Apr 2023 23:43:09 -0700 Subject: [PATCH 1/2] Possible refactor of _update_attrs_and_maybe_adapt_lights --- custom_components/adaptive_lighting/switch.py | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 4817f984..1ddaf2c8 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1129,21 +1129,20 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): if lights is None: lights = self._lights - filtered_lights = [] - if not force: - if self._only_once: - return - for light in lights: - # Don't adapt lights that haven't finished prior transitions. - if not self.turn_on_off_listener.transition_timers.get(light): - filtered_lights.append(light) - else: - filtered_lights = lights + if not force and self._only_once: + return + + filtered_lights = [ + light + for light in lights + if force or not self.turn_on_off_listener.transition_timers.get(light) + ] + if not filtered_lights: return await self._update_manual_control_and_maybe_adapt( - lights, transition, force, context + filtered_lights, transition, force, context ) async def _update_manual_control_and_maybe_adapt( @@ -1165,43 +1164,45 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): context.id, ) - if adapt_brightness is None: - adapt_brightness = self.adapt_brightness_switch.is_on - if adapt_color is None: - adapt_color = self.adapt_color_switch.is_on + adapt_brightness = adapt_brightness or self.adapt_brightness_switch.is_on + adapt_color = adapt_color or self.adapt_color_switch.is_on for light in lights: if not is_on(self.hass, light): continue - if self._take_over_control: - if self.turn_on_off_listener.is_manually_controlled( + manually_controlled = self.turn_on_off_listener.is_manually_controlled( + self, + light, + force, + adapt_brightness, + adapt_color, + ) + + significant_change = ( + self._detect_non_ha_changes + and not force + and await self.turn_on_off_listener.significant_change( self, light, - force, adapt_brightness, adapt_color, - ): + context, + ) + ) + + if self._take_over_control and (manually_controlled or significant_change): + if manually_controlled: _LOGGER.debug( "%s: '%s' is being manually controlled, stop adapting, context.id=%s.", self._name, light, context.id, ) - continue - if ( - self._detect_non_ha_changes - and not force - and await self.turn_on_off_listener.significant_change( - self, - light, - adapt_brightness, - adapt_color, - context, - ) - ): + else: _fire_manual_control_event(self, light, context, is_async=False) - continue + continue + await self._adapt_light(light, transition, force=force, context=context) async def _sleep_mode_switch_state_event(self, event: Event) -> None: From b9273d2d060eb4350571d54130d3a187b69160fd Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Sat, 8 Apr 2023 13:26:39 -0500 Subject: [PATCH 2/2] Revert "Revert "cleaned up"" This reverts commit 11b268148b098af4f790a6f41c8b87b14ea0748a. --- custom_components/adaptive_lighting/switch.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 8b40e9b9..fd894489 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1234,8 +1234,6 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): transition: int | None, force: bool, context: Context | None, - adapt_brightness: bool | None = None, - adapt_color: bool | None = None, ) -> None: assert context is not None _LOGGER.debug( @@ -1247,8 +1245,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): context.id, ) - adapt_brightness = adapt_brightness or self.adapt_brightness_switch.is_on - adapt_color = adapt_color or self.adapt_color_switch.is_on + adapt_brightness = self.adapt_brightness_switch.is_on + adapt_color = self.adapt_color_switch.is_on for light in lights: if not is_on(self.hass, light): @@ -1283,7 +1281,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): context.id, ) else: - _fire_manual_control_event(self, light, context, is_async=False) + _fire_manual_control_event(self, light, context) continue await self._adapt_light(light, transition, force=force, context=context)