mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-10 14:57:07 +00:00
da441bc697
Also updates CI pypy versions to be 3.9 and 3.10 which are the currently [supported versions](https://www.pypy.org/posts/2024/01/pypy-v7315-release.html). Otherwise latest cryptography doesn't ship with pypy3.8 wheels and is unable to build on windows. Also updates the `codecov-action` to v4 which fixed some intermittent uploading errors.
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""Implementation of the motion detection (PIR) module found in some dimmers."""
|
|
|
|
from enum import Enum
|
|
from typing import Optional
|
|
|
|
from ...exceptions import KasaException
|
|
from ..iotmodule import IotModule
|
|
|
|
|
|
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}}}
|
|
|
|
|
|
class Motion(IotModule):
|
|
"""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(
|
|
self, *, range: Optional[Range] = None, custom_range: Optional[int] = None
|
|
):
|
|
"""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:
|
|
raise KasaException("Either range or custom_range need to be defined")
|
|
|
|
return await self.call("set_trigger_sens", payload)
|
|
|
|
@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})
|