From 2368530c97f1637e0cc586303f5f0a1075e2946d Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Tue, 30 Jul 2024 19:07:58 +0100 Subject: [PATCH 1/2] Do not send light_on value to iot bulb set_state --- kasa/iot/modules/light.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kasa/iot/modules/light.py b/kasa/iot/modules/light.py index 8c4e22c90..7f38a4fa6 100644 --- a/kasa/iot/modules/light.py +++ b/kasa/iot/modules/light.py @@ -231,6 +231,8 @@ async def set_state(self, state: LightState) -> dict: state_dict["on_off"] = 1 else: state_dict["on_off"] = int(state.light_on) + # Remove the light_on from the dict + state_dict.pop("light_on", None) return await bulb._set_light_state(state_dict, transition=transition) @property From 85be2ac116d66d515678649b07604555d32c39e2 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Wed, 31 Jul 2024 11:51:36 +0100 Subject: [PATCH 2/2] Add test to check light_on not passed to set_light_state --- kasa/iot/iotbulb.py | 1 + kasa/tests/test_bulb.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/kasa/iot/iotbulb.py b/kasa/iot/iotbulb.py index 26c73096a..0d8eff06a 100644 --- a/kasa/iot/iotbulb.py +++ b/kasa/iot/iotbulb.py @@ -326,6 +326,7 @@ async def _set_light_state( self, state: dict, *, transition: int | None = None ) -> dict: """Set the light state.""" + state = {**state} if transition is not None: state["transition_period"] = transition diff --git a/kasa/tests/test_bulb.py b/kasa/tests/test_bulb.py index c78c539c9..002cbd419 100644 --- a/kasa/tests/test_bulb.py +++ b/kasa/tests/test_bulb.py @@ -9,7 +9,7 @@ Schema, ) -from kasa import Device, DeviceType, IotLightPreset, KasaException, Module +from kasa import Device, DeviceType, IotLightPreset, KasaException, LightState, Module from kasa.iot import IotBulb, IotDimmer from .conftest import ( @@ -96,6 +96,22 @@ async def test_set_hsv_transition(dev: IotBulb, mocker): ) +@bulb_iot +async def test_light_set_state(dev: IotBulb, mocker): + """Testing setting LightState on the light module.""" + light = dev.modules.get(Module.Light) + assert light + set_light_state = mocker.spy(dev, "_set_light_state") + state = LightState(light_on=True) + await light.set_state(state) + + set_light_state.assert_called_with({"on_off": 1}, transition=None) + state = LightState(light_on=False) + await light.set_state(state) + + set_light_state.assert_called_with({"on_off": 0}, transition=None) + + @color_bulb @turn_on async def test_invalid_hsv(dev: Device, turn_on):