diff --git a/kasa/iot/modules/ambientlight.py b/kasa/iot/modules/ambientlight.py index d6470d26..691f88f1 100644 --- a/kasa/iot/modules/ambientlight.py +++ b/kasa/iot/modules/ambientlight.py @@ -1,16 +1,11 @@ """Implementation of the ambient light (LAS) module found in some dimmers.""" +import logging + from ...feature import Feature from ..iotmodule import IotModule, merge -# TODO create tests and use the config reply there -# [{"hw_id":0,"enable":0,"dark_index":1,"min_adc":0,"max_adc":2450, -# "level_array":[{"name":"cloudy","adc":490,"value":20}, -# {"name":"overcast","adc":294,"value":12}, -# {"name":"dawn","adc":222,"value":9}, -# {"name":"twilight","adc":222,"value":9}, -# {"name":"total darkness","adc":111,"value":4}, -# {"name":"custom","adc":2400,"value":97}]}] +_LOGGER = logging.getLogger(__name__) class AmbientLight(IotModule): @@ -18,6 +13,19 @@ class AmbientLight(IotModule): def _initialize_features(self): """Initialize features after the initial update.""" + self._add_feature( + Feature( + device=self._device, + container=self, + id="ambient_light_enabled", + name="Ambient light enabled", + icon="mdi:brightness-percent", + attribute_getter="enabled", + attribute_setter="set_enabled", + type=Feature.Type.Switch, + category=Feature.Category.Config, + ) + ) self._add_feature( Feature( device=self._device, @@ -41,15 +49,25 @@ class AmbientLight(IotModule): return req + @property + def config(self) -> dict: + """Return current ambient light config.""" + config = self.data["get_config"] + devs = config["devs"] + if len(devs) != 1: + _LOGGER.error("Unexpected number of devs in config: %s", config) + + return devs[0] + @property def presets(self) -> dict: """Return device-defined presets for brightness setting.""" - return self.data["level_array"] + return self.config["level_array"] @property def enabled(self) -> bool: """Return True if the module is enabled.""" - return bool(self.data["enable"]) + return bool(self.config["enable"]) @property def ambientlight_brightness(self) -> int: diff --git a/kasa/tests/iot/modules/test_ambientlight.py b/kasa/tests/iot/modules/test_ambientlight.py new file mode 100644 index 00000000..d7c58475 --- /dev/null +++ b/kasa/tests/iot/modules/test_ambientlight.py @@ -0,0 +1,46 @@ +from pytest_mock import MockerFixture + +from kasa import Module +from kasa.iot import IotDimmer +from kasa.iot.modules.ambientlight import AmbientLight +from kasa.tests.device_fixtures import dimmer_iot + + +@dimmer_iot +def test_ambientlight_getters(dev: IotDimmer): + assert Module.IotAmbientLight in dev.modules + ambientlight: AmbientLight = dev.modules[Module.IotAmbientLight] + + assert ambientlight.enabled == ambientlight.config["enable"] + assert ambientlight.presets == ambientlight.config["level_array"] + + assert ( + ambientlight.ambientlight_brightness + == ambientlight.data["get_current_brt"]["value"] + ) + + +@dimmer_iot +async def test_ambientlight_setters(dev: IotDimmer, mocker: MockerFixture): + ambientlight: AmbientLight = dev.modules[Module.IotAmbientLight] + query_helper = mocker.patch("kasa.iot.IotDimmer._query_helper") + + await ambientlight.set_enabled(True) + query_helper.assert_called_with("smartlife.iot.LAS", "set_enable", {"enable": True}) + + await ambientlight.set_brightness_limit(10) + query_helper.assert_called_with( + "smartlife.iot.LAS", "set_brt_level", {"index": 0, "value": 10} + ) + + +@dimmer_iot +def test_ambientlight_feature(dev: IotDimmer): + assert Module.IotAmbientLight in dev.modules + ambientlight: AmbientLight = dev.modules[Module.IotAmbientLight] + + enabled = dev.features["ambient_light_enabled"] + assert ambientlight.enabled == enabled.value + + brightness = dev.features["ambient_light"] + assert ambientlight.ambientlight_brightness == brightness.value