2
2
3
3
from __future__ import annotations
4
4
5
+ import logging
5
6
from enum import Enum
6
7
7
8
from ...exceptions import KasaException
9
+ from ...feature import Feature
8
10
from ..iotmodule import IotModule
9
11
12
+ _LOGGER = logging .getLogger (__name__ )
13
+
10
14
11
15
class Range (Enum ):
12
16
"""Range for motion detection."""
@@ -17,27 +21,51 @@ class Range(Enum):
17
21
Custom = 3
18
22
19
23
20
- # TODO: use the config reply in tests
21
- # {"enable":0,"version":"1.0","trigger_index":2,"cold_time":60000,
22
- # "min_adc":0,"max_adc":4095,"array":[80,50,20,0],"err_code":0}}}
23
-
24
-
25
24
class Motion (IotModule ):
26
25
"""Implements the motion detection (PIR) module."""
27
26
27
+ def _initialize_features (self ):
28
+ """Initialize features after the initial update."""
29
+ # Only add features if the device supports the module
30
+ if "get_config" not in self .data :
31
+ return
32
+
33
+ if "enable" not in self .config :
34
+ _LOGGER .warning ("%r initialized, but no enable in response" )
35
+ return
36
+
37
+ self ._add_feature (
38
+ Feature (
39
+ device = self ._device ,
40
+ container = self ,
41
+ id = "pir_enabled" ,
42
+ name = "PIR enabled" ,
43
+ icon = "mdi:motion-sensor" ,
44
+ attribute_getter = "enabled" ,
45
+ attribute_setter = "set_enabled" ,
46
+ type = Feature .Type .Switch ,
47
+ category = Feature .Category .Config ,
48
+ )
49
+ )
50
+
28
51
def query (self ):
29
52
"""Request PIR configuration."""
30
53
return self .query_for_command ("get_config" )
31
54
55
+ @property
56
+ def config (self ) -> dict :
57
+ """Return current configuration."""
58
+ return self .data ["get_config" ]
59
+
32
60
@property
33
61
def range (self ) -> Range :
34
62
"""Return motion detection range."""
35
- return Range (self .data ["trigger_index" ])
63
+ return Range (self .config ["trigger_index" ])
36
64
37
65
@property
38
66
def enabled (self ) -> bool :
39
67
"""Return True if module is enabled."""
40
- return bool (self .data ["enable" ])
68
+ return bool (self .config ["enable" ])
41
69
42
70
async def set_enabled (self , state : bool ):
43
71
"""Enable/disable PIR."""
@@ -63,7 +91,7 @@ async def set_range(
63
91
@property
64
92
def inactivity_timeout (self ) -> int :
65
93
"""Return inactivity timeout in milliseconds."""
66
- return self .data ["cold_time" ]
94
+ return self .config ["cold_time" ]
67
95
68
96
async def set_inactivity_timeout (self , timeout : int ):
69
97
"""Set inactivity timeout in milliseconds.
0 commit comments