From ef72272554bec8e5cdc50c2bbb690b96be9b67bf Mon Sep 17 00:00:00 2001 From: Benjamin Ness Date: Thu, 22 Feb 2024 10:46:37 -0600 Subject: [PATCH 1/4] Add feature for ambient light sensor --- kasa/iot/modules/ambientlight.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/kasa/iot/modules/ambientlight.py b/kasa/iot/modules/ambientlight.py index f1069448c..0bcd6262d 100644 --- a/kasa/iot/modules/ambientlight.py +++ b/kasa/iot/modules/ambientlight.py @@ -1,5 +1,6 @@ """Implementation of the ambient light (LAS) module found in some dimmers.""" -from ..iotmodule import IotModule +from ..iotmodule import IotModule, merge +from ...feature import Feature, FeatureType # TODO create tests and use the config reply there # [{"hw_id":0,"enable":0,"dark_index":1,"min_adc":0,"max_adc":2450, @@ -14,9 +15,28 @@ class AmbientLight(IotModule): """Implements ambient light controls for the motion sensor.""" + def __init__(self, device, module): + super().__init__(device, module) + self._add_feature( + Feature( + device=device, + container=self, + name="Ambient Light", + icon="mdi:brightness-percent", + attribute_getter="ambientlight_brightness", + type=FeatureType.Sensor, + ) + ) + def query(self): """Request configuration.""" - return self.query_for_command("get_config") + + req = merge( + self.query_for_command("get_realtime"), + self.query_for_command("get_current_brt"), + ) + + return req @property def presets(self) -> dict: @@ -28,6 +48,11 @@ def enabled(self) -> bool: """Return True if the module is enabled.""" return bool(self.data["enable"]) + @property + def ambientlight_brightness(self) -> int: + """Return True if the module is enabled.""" + return int(self.data["get_current_brt"]["value"]) + async def set_enabled(self, state: bool): """Enable/disable LAS.""" return await self.call("set_enable", {"enable": int(state)}) From 6713d7e8941c6fe5908fd546e6070411846a07b6 Mon Sep 17 00:00:00 2001 From: Benjamin Ness Date: Thu, 22 Feb 2024 10:51:11 -0600 Subject: [PATCH 2/4] Fix linting issues in other files not included in this feature --- devtools/helpers/smartrequests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/devtools/helpers/smartrequests.py b/devtools/helpers/smartrequests.py index de0a53ff4..279925190 100644 --- a/devtools/helpers/smartrequests.py +++ b/devtools/helpers/smartrequests.py @@ -168,7 +168,7 @@ def get_device_time() -> "SmartRequest": @staticmethod def get_wireless_scan_info( - params: Optional[GetRulesParams] = None + params: Optional[GetRulesParams] = None, ) -> "SmartRequest": """Get wireless scan info.""" return SmartRequest( @@ -273,7 +273,7 @@ def get_auto_light_info() -> "SmartRequest": @staticmethod def get_dynamic_light_effect_rules( - params: Optional[GetRulesParams] = None + params: Optional[GetRulesParams] = None, ) -> "SmartRequest": """Get dynamic light effect rules.""" return SmartRequest( @@ -292,7 +292,7 @@ def set_light_info(params: LightInfoParams) -> "SmartRequest": @staticmethod def set_dynamic_light_effect_rule_enable( - params: DynamicLightEffectParams + params: DynamicLightEffectParams, ) -> "SmartRequest": """Enable dynamic light effect rule.""" return SmartRequest("set_dynamic_light_effect_rule_enable", params) @@ -312,7 +312,7 @@ def get_component_info_requests(component_nego_response) -> List["SmartRequest"] @staticmethod def _create_request_dict( - smart_request: Union["SmartRequest", List["SmartRequest"]] + smart_request: Union["SmartRequest", List["SmartRequest"]], ) -> dict: """Create request dict to be passed to SmartProtocol.query().""" if isinstance(smart_request, list): From bbde9c76c135dadc958b075556ddfeb155359a34 Mon Sep 17 00:00:00 2001 From: Benjamin Ness Date: Thu, 22 Feb 2024 10:54:51 -0600 Subject: [PATCH 3/4] Fix linting issues --- kasa/iot/modules/ambientlight.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kasa/iot/modules/ambientlight.py b/kasa/iot/modules/ambientlight.py index 0bcd6262d..ff0c30c05 100644 --- a/kasa/iot/modules/ambientlight.py +++ b/kasa/iot/modules/ambientlight.py @@ -1,6 +1,6 @@ """Implementation of the ambient light (LAS) module found in some dimmers.""" -from ..iotmodule import IotModule, merge from ...feature import Feature, FeatureType +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, @@ -30,7 +30,6 @@ def __init__(self, device, module): def query(self): """Request configuration.""" - req = merge( self.query_for_command("get_realtime"), self.query_for_command("get_current_brt"), From d962189db26e39841f877fb2ac59d4268dc476b2 Mon Sep 17 00:00:00 2001 From: Benjamin Ness Date: Thu, 22 Feb 2024 11:31:47 -0600 Subject: [PATCH 4/4] Fix copy/paste error, switch back to get_config --- kasa/iot/modules/ambientlight.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kasa/iot/modules/ambientlight.py b/kasa/iot/modules/ambientlight.py index ff0c30c05..e14f2991d 100644 --- a/kasa/iot/modules/ambientlight.py +++ b/kasa/iot/modules/ambientlight.py @@ -31,7 +31,7 @@ def __init__(self, device, module): def query(self): """Request configuration.""" req = merge( - self.query_for_command("get_realtime"), + self.query_for_command("get_config"), self.query_for_command("get_current_brt"), )