From 6e7a588d40396fd3696593cad4394c1008917193 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Sat, 21 Jan 2023 00:25:59 +0100 Subject: [PATCH] Add brightness to lightstrip's set_effect (#415) * Add brightness parameter to lightstrip's set_effect * Use None as default as effects have different default brightnesses --- kasa/smartlightstrip.py | 9 ++++++--- kasa/tests/test_lightstrip.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/kasa/smartlightstrip.py b/kasa/smartlightstrip.py index 08723453..ae16aa75 100644 --- a/kasa/smartlightstrip.py +++ b/kasa/smartlightstrip.py @@ -89,16 +89,19 @@ class SmartLightStrip(SmartBulb): @requires_update async def set_effect( - self, - effect: str, + self, effect: str, *, brightness: Optional[int] = None ) -> None: """Set an effect on the device. :param str effect: The effect to set + :param int brightness: The wanted brightness """ if effect not in EFFECT_MAPPING_V1: raise SmartDeviceException(f"The effect {effect} is not a built in effect.") - await self.set_custom_effect(EFFECT_MAPPING_V1[effect]) + effect_dict = EFFECT_MAPPING_V1[effect] + if brightness is not None: + effect_dict["brightness"] = brightness + await self.set_custom_effect(effect_dict) @requires_update async def set_custom_effect( diff --git a/kasa/tests/test_lightstrip.py b/kasa/tests/test_lightstrip.py index c90cedee..962bb150 100644 --- a/kasa/tests/test_lightstrip.py +++ b/kasa/tests/test_lightstrip.py @@ -30,6 +30,23 @@ async def test_effects_lightstrip_set_effect(dev: SmartLightStrip): assert dev.state_information["Effect"] == "Candy Cane" +@lightstrip +@pytest.mark.parametrize("brightness", [100, 50]) +async def test_effects_lightstrip_set_effect_brightness( + dev: SmartLightStrip, brightness, mocker +): + query_helper = mocker.patch("kasa.SmartLightStrip._query_helper") + + if brightness == 100: # test that default brightness works + await dev.set_effect("Candy Cane") + else: + await dev.set_effect("Candy Cane", brightness=brightness) + + args, kwargs = query_helper.call_args_list[0] + payload = args[2] + assert payload["brightness"] == brightness + + @lightstrip async def test_effects_lightstrip_has_effects(dev: SmartLightStrip): assert dev.has_effects is True