Possible refactor of _update_attrs_and_maybe_adapt_lights

This commit is contained in:
Bas Nijholt 2023-04-03 23:43:09 -07:00
commit 8285564015

View file

@ -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: