Fix changing brightness when effect is active (#1019)

This PR changes the behavior of `brightness` module if an effect is
active.
Currently, changing the brightness disables the effect when the
brightness is changed, this fixes that.
This will also improve the `set_effect` interface to use the current
brightness when an effect is activated.

* light_strip_effect: passing `bAdjusted` with the changed properties
changes the brightness.
* light_effect: the brightness is stored only in the rule, so we modify
it when adjusting the brightness. This is also done during the initial
effect activation.

---------

Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
This commit is contained in:
Teemu R
2024-07-01 13:59:24 +02:00
committed by GitHub
parent 2687c71c4b
commit b31a2ede7f
10 changed files with 321 additions and 21 deletions

View File

@@ -4,15 +4,14 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...interfaces.lighteffect import LightEffect as LightEffectInterface
from ..effects import EFFECT_MAPPING, EFFECT_NAMES
from ..smartmodule import SmartModule
from ..effects import EFFECT_MAPPING, EFFECT_NAMES, SmartLightEffect
from ..smartmodule import Module, SmartModule
if TYPE_CHECKING:
from ..smartdevice import SmartDevice
class LightStripEffect(SmartModule, LightEffectInterface):
class LightStripEffect(SmartModule, SmartLightEffect):
"""Implementation of dynamic light effects."""
REQUIRED_COMPONENT = "light_strip_lighting_effect"
@@ -22,6 +21,7 @@ class LightStripEffect(SmartModule, LightEffectInterface):
effect_list = [self.LIGHT_EFFECTS_OFF]
effect_list.extend(EFFECT_NAMES)
self._effect_list = effect_list
self._effect_mapping = EFFECT_MAPPING
@property
def name(self) -> str:
@@ -53,6 +53,28 @@ class LightStripEffect(SmartModule, LightEffectInterface):
return name
return self.LIGHT_EFFECTS_OFF
@property
def is_active(self) -> bool:
"""Return if effect is active."""
eff = self.data["lighting_effect"]
# softAP has enable=1, but brightness 0 which fails on tests
return bool(eff["enable"]) and eff["name"] in self._effect_list
@property
def brightness(self) -> int:
"""Return effect brightness."""
eff = self.data["lighting_effect"]
return eff["brightness"]
async def set_brightness(self, brightness: int, *, transition: int | None = None):
"""Set effect brightness."""
if brightness <= 0:
return await self.set_effect(self.LIGHT_EFFECTS_OFF)
# Need to pass bAdjusted to keep the existing effect running
eff = {"brightness": brightness, "bAdjusted": True}
return await self.set_custom_effect(eff)
@property
def effect_list(self) -> list[str]:
"""Return built-in effects list.
@@ -81,16 +103,24 @@ class LightStripEffect(SmartModule, LightEffectInterface):
:param int brightness: The wanted brightness
:param int transition: The wanted transition time
"""
brightness_module = self._device.modules[Module.Brightness]
if effect == self.LIGHT_EFFECTS_OFF:
effect_dict = dict(self.data["lighting_effect"])
effect_dict["enable"] = 0
elif effect not in EFFECT_MAPPING:
state = self._device.modules[Module.Light].state
await self._device.modules[Module.Light].set_state(state)
return
if effect not in self._effect_mapping:
raise ValueError(f"The effect {effect} is not a built in effect.")
else:
effect_dict = EFFECT_MAPPING[effect]
effect_dict = self._effect_mapping[effect]
# Use explicitly given brightness
if brightness is not None:
effect_dict["brightness"] = brightness
# Fall back to brightness reported by the brightness module
elif brightness_module.brightness:
effect_dict["brightness"] = brightness_module.brightness
if transition is not None:
effect_dict["transition"] = transition