Skip to content

Commit

Permalink
Add feature for ambient light sensor (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty35 committed Feb 22, 2024
1 parent a87fc3b commit f965b14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 4 additions & 4 deletions devtools/helpers/smartrequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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):
Expand Down
28 changes: 26 additions & 2 deletions kasa/iot/modules/ambientlight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Implementation of the ambient light (LAS) module found in some dimmers."""
from ..iotmodule import IotModule
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,
Expand All @@ -14,9 +15,27 @@
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_config"),
self.query_for_command("get_current_brt"),
)

return req

@property
def presets(self) -> dict:
Expand All @@ -28,6 +47,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)})
Expand Down

0 comments on commit f965b14

Please sign in to comment.