From e1e36967d2f73a54e0e4c24b39720eecc5d547ac Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 4 Apr 2022 14:24:43 -1000 Subject: [PATCH 1/2] Ensure state is restored when turning back on Fixes https://github.com/home-assistant/core/issues/69039 --- kasa/smartbulb.py | 13 +++++++++++-- kasa/tests/test_bulb.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/kasa/smartbulb.py b/kasa/smartbulb.py index e5dcfbe90..c2d095395 100644 --- a/kasa/smartbulb.py +++ b/kasa/smartbulb.py @@ -34,6 +34,9 @@ class HSV(NamedTuple): r"KL430": ColorTempRange(2500, 9000), } + +NON_COLOR_MODE_FLAGS = {"transition_period", "on_off"} + _LOGGER = logging.getLogger(__name__) @@ -211,8 +214,14 @@ async def set_light_state(self, state: Dict, *, transition: int = None) -> Dict: if "on_off" not in state: state["on_off"] = 1 - # This is necessary to allow turning on into a specific state - state["ignore_default"] = 1 + # If we are turning on without any color mode flags, + # we do not want to set ignore_default to ensure + # we restore the previous state. + if state["on_off"] and NON_COLOR_MODE_FLAGS.issuperset(state): + state["ignore_default"] = 0 + else: + # This is necessary to allow turning on into a specific state + state["ignore_default"] = 1 light_state = await self._query_helper( self.LIGHT_SERVICE, self.SET_LIGHT_METHOD, state diff --git a/kasa/tests/test_bulb.py b/kasa/tests/test_bulb.py index ea8a28cb8..de40de6ad 100644 --- a/kasa/tests/test_bulb.py +++ b/kasa/tests/test_bulb.py @@ -232,3 +232,17 @@ async def test_non_dimmable(dev): assert dev.brightness == 0 with pytest.raises(SmartDeviceException): await dev.set_brightness(100) + + +@bulb +async def test_ignore_default_not_set_without_color_mode_change_turn_on(dev, mocker): + query_helper = mocker.patch("kasa.SmartBulb._query_helper") + # When turning back ignore do not ignore + # default so we restore state + await dev.turn_on() + args, kwargs = query_helper.call_args_list[0] + assert args[2] == {"on_off": 1, "ignore_default": 0} + + await dev.turn_off() + args, kwargs = query_helper.call_args_list[1] + assert args[2] == {"on_off": 0, "ignore_default": 1} From fbcf717537cb5b08a37de6e76f164f9850904ed3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 Apr 2022 06:22:24 -1000 Subject: [PATCH 2/2] Update kasa/tests/test_bulb.py Co-authored-by: Teemu R. --- kasa/tests/test_bulb.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kasa/tests/test_bulb.py b/kasa/tests/test_bulb.py index de40de6ad..032154424 100644 --- a/kasa/tests/test_bulb.py +++ b/kasa/tests/test_bulb.py @@ -237,8 +237,7 @@ async def test_non_dimmable(dev): @bulb async def test_ignore_default_not_set_without_color_mode_change_turn_on(dev, mocker): query_helper = mocker.patch("kasa.SmartBulb._query_helper") - # When turning back ignore do not ignore - # default so we restore state + # When turning back without settings, ignore default to restore the state await dev.turn_on() args, kwargs = query_helper.call_args_list[0] assert args[2] == {"on_off": 1, "ignore_default": 0}