2022-01-29 16:53:18 +00:00
|
|
|
"""Implementation of the motion detection (PIR) module found in some dimmers."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-29 16:53:18 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
from ...exceptions import KasaException
|
2024-02-19 17:01:31 +00:00
|
|
|
from ..iotmodule import IotModule
|
2022-01-29 16:53:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Range(Enum):
|
|
|
|
"""Range for motion detection."""
|
|
|
|
|
|
|
|
Far = 0
|
|
|
|
Mid = 1
|
|
|
|
Near = 2
|
|
|
|
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}}}
|
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class Motion(IotModule):
|
2022-01-29 16:53:18 +00:00
|
|
|
"""Implements the motion detection (PIR) module."""
|
|
|
|
|
|
|
|
def query(self):
|
|
|
|
"""Request PIR configuration."""
|
|
|
|
return self.query_for_command("get_config")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def range(self) -> Range:
|
|
|
|
"""Return motion detection range."""
|
|
|
|
return Range(self.data["trigger_index"])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self) -> bool:
|
|
|
|
"""Return True if module is enabled."""
|
|
|
|
return bool(self.data["enable"])
|
|
|
|
|
|
|
|
async def set_enabled(self, state: bool):
|
|
|
|
"""Enable/disable PIR."""
|
|
|
|
return await self.call("set_enable", {"enable": int(state)})
|
|
|
|
|
|
|
|
async def set_range(
|
2024-04-17 13:39:24 +00:00
|
|
|
self, *, range: Range | None = None, custom_range: int | None = None
|
2022-01-29 16:53:18 +00:00
|
|
|
):
|
|
|
|
"""Set the range for the sensor.
|
|
|
|
|
|
|
|
:param range: for using standard ranges
|
|
|
|
:param custom_range: range in decimeters, overrides the range parameter
|
|
|
|
"""
|
|
|
|
if custom_range is not None:
|
|
|
|
payload = {"index": Range.Custom.value, "value": custom_range}
|
|
|
|
elif range is not None:
|
|
|
|
payload = {"index": range.value}
|
|
|
|
else:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Either range or custom_range need to be defined")
|
2022-01-29 16:53:18 +00:00
|
|
|
|
|
|
|
return await self.call("set_trigger_sens", payload)
|
2023-05-17 18:08:05 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def inactivity_timeout(self) -> int:
|
|
|
|
"""Return inactivity timeout in milliseconds."""
|
|
|
|
return self.data["cold_time"]
|
|
|
|
|
|
|
|
async def set_inactivity_timeout(self, timeout: int):
|
|
|
|
"""Set inactivity timeout in milliseconds.
|
|
|
|
|
|
|
|
Note, that you need to delete the default "Smart Control" rule in the app
|
|
|
|
to avoid reverting this back to 60 seconds after a period of time.
|
|
|
|
"""
|
|
|
|
return await self.call("set_cold_time", {"cold_time": timeout})
|