Expose ambient light setting for iot dimmers (#1210)

This PR adds a setting to control the ambient light enabled/disabled.

Also fixes the getters.
This commit is contained in:
Teemu R. 2024-10-31 12:17:18 +01:00 committed by GitHub
parent 9975bbf26a
commit 6c141c3b65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 10 deletions

View File

@ -1,16 +1,11 @@
"""Implementation of the ambient light (LAS) module found in some dimmers.""" """Implementation of the ambient light (LAS) module found in some dimmers."""
import logging
from ...feature import Feature from ...feature import Feature
from ..iotmodule import IotModule, merge from ..iotmodule import IotModule, merge
# TODO create tests and use the config reply there _LOGGER = logging.getLogger(__name__)
# [{"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}]}]
class AmbientLight(IotModule): class AmbientLight(IotModule):
@ -18,6 +13,19 @@ class AmbientLight(IotModule):
def _initialize_features(self): def _initialize_features(self):
"""Initialize features after the initial update.""" """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( self._add_feature(
Feature( Feature(
device=self._device, device=self._device,
@ -41,15 +49,25 @@ class AmbientLight(IotModule):
return req 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 @property
def presets(self) -> dict: def presets(self) -> dict:
"""Return device-defined presets for brightness setting.""" """Return device-defined presets for brightness setting."""
return self.data["level_array"] return self.config["level_array"]
@property @property
def enabled(self) -> bool: def enabled(self) -> bool:
"""Return True if the module is enabled.""" """Return True if the module is enabled."""
return bool(self.data["enable"]) return bool(self.config["enable"])
@property @property
def ambientlight_brightness(self) -> int: def ambientlight_brightness(self) -> int:

View File

@ -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