Fix iot light effect brightness (#1092)

Fixes issue where the brightness of the `iot` light effect is set properly
on the light effect but read back incorrectly from the light.
This commit is contained in:
Steven B.
2024-07-31 15:58:48 +01:00
committed by GitHub
parent cb0077f634
commit 31ec27c1c8
8 changed files with 101 additions and 37 deletions

View File

@@ -292,6 +292,26 @@ class FakeIotTransport(BaseTransport):
self.proto["system"]["get_sysinfo"]["lighting_effect_state"] = dict(effect)
def transition_light_state(self, state_changes, *args):
# Setting the light state on a device will turn off any active lighting effects.
# Unless it's just the brightness in which case it will update the brightness for
# the lighting effect
if lighting_effect_state := self.proto["system"]["get_sysinfo"].get(
"lighting_effect_state"
):
if (
"hue" in state_changes
or "saturation" in state_changes
or "color_temp" in state_changes
):
lighting_effect_state["enable"] = 0
elif (
lighting_effect_state["enable"] == 1
and state_changes.get("on_off") != 0
and (brightness := state_changes.get("brightness"))
):
lighting_effect_state["brightness"] = brightness
return
_LOGGER.debug("Setting light state to %s", state_changes)
light_state = self.proto["system"]["get_sysinfo"]["light_state"]
@@ -317,12 +337,6 @@ class FakeIotTransport(BaseTransport):
_LOGGER.debug("New light state: %s", new_state)
self.proto["system"]["get_sysinfo"]["light_state"] = new_state
# Setting the light state on a device will turn off any active lighting effects.
if lighting_effect_state := self.proto["system"]["get_sysinfo"].get(
"lighting_effect_state"
):
lighting_effect_state["enable"] = 0
def set_preferred_state(self, new_state, *args):
"""Implement set_preferred_state."""
self.proto["system"]["get_sysinfo"]["preferred_state"][new_state["index"]] = (

View File

@@ -271,13 +271,14 @@ class FakeSmartTransport(BaseTransport):
def _set_light_strip_effect(self, info, params):
"""Set or remove values as per the device behaviour."""
info["get_device_info"]["lighting_effect"]["enable"] = params["enable"]
info["get_device_info"]["lighting_effect"]["name"] = params["name"]
info["get_device_info"]["lighting_effect"]["id"] = params["id"]
# Brightness is not always available
if (brightness := params.get("brightness")) is not None:
info["get_device_info"]["lighting_effect"]["brightness"] = brightness
info["get_lighting_effect"] = copy.deepcopy(params)
if "enable" in params:
info["get_device_info"]["lighting_effect"]["enable"] = params["enable"]
info["get_device_info"]["lighting_effect"]["name"] = params["name"]
info["get_device_info"]["lighting_effect"]["id"] = params["id"]
info["get_lighting_effect"] = copy.deepcopy(params)
def _set_led_info(self, info, params):
"""Set or remove values as per the device behaviour."""

View File

@@ -30,26 +30,23 @@ async def test_light_strip_effect(dev: Device, mocker: MockerFixture):
call = mocker.spy(light_effect, "call")
light = dev.modules[Module.Light]
light_call = mocker.spy(light, "call")
assert feature.choices == light_effect.effect_list
assert feature.choices
for effect in chain(reversed(feature.choices), feature.choices):
if effect == LightEffect.LIGHT_EFFECTS_OFF:
off_effect = (
light_effect.effect
if light_effect.effect in light_effect._effect_mapping
else "Aurora"
)
await light_effect.set_effect(effect)
if effect == LightEffect.LIGHT_EFFECTS_OFF:
light_call.assert_called()
continue
# Start with the current effect data
params = light_effect.data["lighting_effect"]
enable = effect != LightEffect.LIGHT_EFFECTS_OFF
params["enable"] = enable
if enable:
params = light_effect._effect_mapping[effect]
params["enable"] = enable
params["brightness"] = brightness.brightness # use the existing brightness
if effect != LightEffect.LIGHT_EFFECTS_OFF:
params = {**light_effect._effect_mapping[effect]}
else:
params = {**light_effect._effect_mapping[off_effect]}
params["enable"] = 0
params["brightness"] = brightness.brightness # use the existing brightness
call.assert_called_with("set_lighting_effect", params)

View File

@@ -133,6 +133,31 @@ async def test_light_effect_module(dev: Device, mocker: MockerFixture):
call.assert_not_called()
@light_effect
async def test_light_effect_brightness(dev: Device, mocker: MockerFixture):
"""Test that light module uses light_effect for brightness when active."""
light_module = dev.modules[Module.Light]
light_effect = dev.modules[Module.LightEffect]
await light_effect.set_effect(light_effect.LIGHT_EFFECTS_OFF)
await light_module.set_brightness(50)
await dev.update()
assert light_effect.effect == light_effect.LIGHT_EFFECTS_OFF
assert light_module.brightness == 50
await light_effect.set_effect(light_effect.effect_list[1])
await dev.update()
# assert light_module.brightness == 100
await light_module.set_brightness(75)
await dev.update()
assert light_module.brightness == 75
await light_effect.set_effect(light_effect.LIGHT_EFFECTS_OFF)
await dev.update()
assert light_module.brightness == 50
@dimmable
async def test_light_brightness(dev: Device):
"""Test brightness setter and getter."""
@@ -201,6 +226,9 @@ async def test_light_set_state(dev: Device):
assert isinstance(dev, Device)
light = next(get_parent_and_child_modules(dev, Module.Light))
assert light
# For fixtures that have a light effect active switch off
if light_effect := light._device.modules.get(Module.LightEffect):
await light_effect.set_effect(light_effect.LIGHT_EFFECTS_OFF)
await light.set_state(LightState(light_on=False))
await dev.update()