add adapt_delay config

Option to set a delay between when a lightstate off -> on event is detected, and lights are adapted. Trying to adapt lights that are still going through their initial turning on fade in transition can cause flickering, so setting this to a number higher than the transition time avoids the problem.
This commit is contained in:
MangoScango 2021-12-24 11:44:34 -05:00
commit c509bd81c0
2 changed files with 19 additions and 0 deletions

View file

@ -52,6 +52,7 @@ CONF_MANUAL_CONTROL = "manual_control"
SERVICE_APPLY = "apply"
CONF_TURN_ON_LIGHTS = "turn_on_lights"
CONF_ADAPT_DELAY, DEFAULT_ADAPT_DELAY = "adapt_delay", 0
TURNING_OFF_DELAY = 5
@ -81,6 +82,7 @@ VALIDATION_TUPLES = [
(CONF_TAKE_OVER_CONTROL, DEFAULT_TAKE_OVER_CONTROL, bool),
(CONF_DETECT_NON_HA_CHANGES, DEFAULT_DETECT_NON_HA_CHANGES, bool),
(CONF_SEPARATE_TURN_ON_COMMANDS, DEFAULT_SEPARATE_TURN_ON_COMMANDS, bool),
(CONF_ADAPT_DELAY, DEFAULT_ADAPT_DELAY, int_between(0, 100)),
]

View file

@ -128,6 +128,7 @@ from .const import (
SUN_EVENT_MIDNIGHT,
SUN_EVENT_NOON,
TURNING_OFF_DELAY,
CONF_ADAPT_DELAY,
VALIDATION_TUPLES,
replace_none_str,
)
@ -572,6 +573,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
self._separate_turn_on_commands = data[CONF_SEPARATE_TURN_ON_COMMANDS]
self._take_over_control = data[CONF_TAKE_OVER_CONTROL]
self._transition = data[CONF_TRANSITION]
self._adapt_delay = data[CONF_ADAPT_DELAY]
_loc = get_astral_location(self.hass)
if isinstance(_loc, tuple):
# Astral v2.2
@ -966,6 +968,21 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
"%s: Cancelling adjusting lights for %s", self._name, entity_id
)
return
if self._adapt_delay > 0:
_LOGGER.debug(
"%s: sleep started for '%s' with context.id='%s'",
self._name,
entity_id,
event.context.id,
)
await asyncio.sleep(self._adapt_delay)
_LOGGER.debug(
"%s: sleep ended for '%s' with context.id='%s'",
self._name,
entity_id,
event.context.id,
)
await self._update_attrs_and_maybe_adapt_lights(
lights=[entity_id],