Expose PIR enabled setting for iot dimmers (#1174)

This adds PIR enabled feature to iot dimmers, making it possible to
enable and disable the motion detection.
This commit is contained in:
Teemu R.
2024-10-31 11:41:11 +01:00
committed by GitHub
parent 530cf4b523
commit 9975bbf26a
3 changed files with 93 additions and 8 deletions

View File

@@ -2,11 +2,15 @@
from __future__ import annotations
import logging
from enum import Enum
from ...exceptions import KasaException
from ...feature import Feature
from ..iotmodule import IotModule
_LOGGER = logging.getLogger(__name__)
class Range(Enum):
"""Range for motion detection."""
@@ -17,27 +21,51 @@ class Range(Enum):
Custom = 3
# TODO: use the config reply in tests
# {"enable":0,"version":"1.0","trigger_index":2,"cold_time":60000,
# "min_adc":0,"max_adc":4095,"array":[80,50,20,0],"err_code":0}}}
class Motion(IotModule):
"""Implements the motion detection (PIR) module."""
def _initialize_features(self):
"""Initialize features after the initial update."""
# Only add features if the device supports the module
if "get_config" not in self.data:
return
if "enable" not in self.config:
_LOGGER.warning("%r initialized, but no enable in response")
return
self._add_feature(
Feature(
device=self._device,
container=self,
id="pir_enabled",
name="PIR enabled",
icon="mdi:motion-sensor",
attribute_getter="enabled",
attribute_setter="set_enabled",
type=Feature.Type.Switch,
category=Feature.Category.Config,
)
)
def query(self):
"""Request PIR configuration."""
return self.query_for_command("get_config")
@property
def config(self) -> dict:
"""Return current configuration."""
return self.data["get_config"]
@property
def range(self) -> Range:
"""Return motion detection range."""
return Range(self.data["trigger_index"])
return Range(self.config["trigger_index"])
@property
def enabled(self) -> bool:
"""Return True if module is enabled."""
return bool(self.data["enable"])
return bool(self.config["enable"])
async def set_enabled(self, state: bool):
"""Enable/disable PIR."""
@@ -63,7 +91,7 @@ class Motion(IotModule):
@property
def inactivity_timeout(self) -> int:
"""Return inactivity timeout in milliseconds."""
return self.data["cold_time"]
return self.config["cold_time"]
async def set_inactivity_timeout(self, timeout: int):
"""Set inactivity timeout in milliseconds.