@@ -46,41 +46,45 @@ def adc_mid(self) -> int:
46
46
class PIRStatus :
47
47
"""Dataclass representing the current trigger state of an ADC PIR sensor."""
48
48
49
+ pir_config : PIRConfig
49
50
adc_value : int
50
51
51
- def get_pir_value (self , config : PIRConfig ) -> int :
52
+ @property
53
+ def pir_value (self ) -> int :
52
54
"""
53
55
Get the PIR status value in integer form.
54
56
55
57
Computes the PIR status value that this object represents,
56
58
using the given PIR configuration.
57
59
"""
58
- return config .adc_mid - self .adc_value
60
+ return self . pir_config .adc_mid - self .adc_value
59
61
60
- def get_pir_percent (self , config : PIRConfig ) -> float :
62
+ @property
63
+ def pir_percent (self ) -> float :
61
64
"""
62
65
Get the PIR status value in percentile form.
63
66
64
67
Computes the PIR status percentage that this object represents,
65
68
using the given PIR configuration.
66
69
"""
67
- value = self .get_pir_value ( config )
70
+ value = self .pir_value
68
71
divisor = (
69
- (config . adc_mid - config .adc_min )
72
+ (self . pir_config . adc_mid - self . pir_config .adc_min )
70
73
if (value < 0 )
71
- else (config . adc_max - config .adc_mid )
74
+ else (self . pir_config . adc_max - self . pir_config .adc_mid )
72
75
)
73
76
return (float (value ) / divisor ) * 100
74
77
75
- def get_pir_triggered (self , config : PIRConfig ) -> bool :
78
+ @property
79
+ def pir_triggered (self ) -> bool :
76
80
"""
77
81
Get the PIR status trigger state.
78
82
79
83
Compute the PIR trigger state this object represents,
80
84
using the given PIR configuration.
81
85
"""
82
- return (config .enabled ) and (
83
- abs (self .get_pir_percent ( config )) > (100 - config .threshold )
86
+ return (self . pir_config .enabled ) and (
87
+ abs (self .pir_percent ) > (100 - self . pir_config .threshold )
84
88
)
85
89
86
90
@@ -329,12 +333,13 @@ async def set_range(self, range: Range) -> dict:
329
333
def _parse_range_value (self , value : str ) -> Range :
330
334
"""Attempt to parse a range value from the given string."""
331
335
value = value .strip ().capitalize ()
332
- if value not in Range ._member_names_ :
336
+ try :
337
+ return Range [value ]
338
+ except KeyError :
333
339
raise KasaException (
334
340
f"Invalid range value: '{ value } '."
335
341
f" Valid options are: { Range ._member_names_ } "
336
- )
337
- return Range [value ]
342
+ ) from KeyError
338
343
339
344
async def _set_range_from_str (self , input : str ) -> dict :
340
345
value = self ._parse_range_value (input )
@@ -374,12 +379,13 @@ async def set_inactivity_timeout(self, timeout: int) -> dict:
374
379
@property
375
380
def pir_state (self ) -> PIRStatus :
376
381
"""Return cached PIR status."""
377
- return PIRStatus (self .data ["get_adc_value" ]["value" ])
382
+ return PIRStatus (self .pir_config , self . data ["get_adc_value" ]["value" ])
378
383
379
384
async def get_pir_state (self ) -> PIRStatus :
380
385
"""Return real-time PIR status."""
381
- current = await self .call ("get_adc_value" )
382
- return PIRStatus (current ["value" ])
386
+ latest = await self .call ("get_adc_value" )
387
+ self .data ["get_adc_value" ] = latest
388
+ return PIRStatus (self .pir_config , latest ["value" ])
383
389
384
390
@property
385
391
def adc_value (self ) -> int :
@@ -389,14 +395,14 @@ def adc_value(self) -> int:
389
395
@property
390
396
def pir_value (self ) -> int :
391
397
"""Return the computed PIR sensor value."""
392
- return self .pir_state .get_pir_value ( self . pir_config )
398
+ return self .pir_state .pir_value
393
399
394
400
@property
395
401
def pir_percent (self ) -> float :
396
402
"""Return the computed PIR sensor value, in percentile form."""
397
- return self .pir_state .get_pir_percent ( self . pir_config )
403
+ return self .pir_state .pir_percent
398
404
399
405
@property
400
406
def pir_triggered (self ) -> bool :
401
407
"""Return if the motion sensor has been triggered."""
402
- return self .pir_state .get_pir_triggered ( self . pir_config )
408
+ return self .pir_state .pir_triggered
0 commit comments