8000 PIR Support Tweaks · python-kasa/python-kasa@1774b81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1774b81

Browse files
committed
PIR Support Tweaks
- Fix: Make PIRStatus into proper dataclass. - Fix: Update internal PIR state when async polling.
1 parent 5ebaadb commit 1774b81

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

kasa/iot/modules/motion.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,45 @@ def adc_mid(self) -> int:
4646
class PIRStatus:
4747
"""Dataclass representing the current trigger state of an ADC PIR sensor."""
4848

49+
pir_config: PIRConfig
4950
adc_value: int
5051

51-
def get_pir_value(self, config: PIRConfig) -> int:
52+
@property
53+
def pir_value(self) -> int:
5254
"""
5355
Get the PIR status value in integer form.
5456
5557
Computes the PIR status value that this object represents,
5658
using the given PIR configuration.
5759
"""
58-
return config.adc_mid - self.adc_value
60+
return self.pir_config.adc_mid - self.adc_value
5961

60-
def get_pir_percent(self, config: PIRConfig) -> float:
62+
@property
63+
def pir_percent(self) -> float:
6164
"""
6265
Get the PIR status value in percentile form.
6366
6467
Computes the PIR status percentage that this object represents,
6568
using the given PIR configuration.
6669
"""
67-
value = self.get_pir_value(config)
70+
value = self.pir_value
6871
divisor = (
69-
(config.adc_mid - config.adc_min)
72+
(self.pir_config.adc_mid - self.pir_config.adc_min)
7073
if (value < 0)
71-
else (config.adc_max - config.adc_mid)
74+
else (self.pir_config.adc_max - self.pir_config.adc_mid)
7275
)
7376
return (float(value) / divisor) * 100
7477

75-
def get_pir_triggered(self, config: PIRConfig) -> bool:
78+
@property
79+
def pir_triggered(self) -> bool:
7680
"""
7781
Get the PIR status trigger state.
7882
7983
Compute the PIR trigger state this object represents,
8084
using the given PIR configuration.
8185
"""
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)
8488
)
8589

8690

@@ -329,12 +333,13 @@ async def set_range(self, range: Range) -> dict:
329333
def _parse_range_value(self, value: str) -> Range:
330334
"""Attempt to parse a range value from the given string."""
331335
value = value.strip().capitalize()
332-
if value not in Range._member_names_:
336+
try:
337+
return Range[value]
338+
except KeyError:
333339
raise KasaException(
334340
f"Invalid range value: '{value}'."
335341
f" Valid options are: {Range._member_names_}"
336-
)
337-
return Range[value]
342+
) from KeyError
338343

339344
async def _set_range_from_str(self, input: str) -> dict:
340345
value = self._parse_range_value(input)
@@ -374,12 +379,13 @@ async def set_inactivity_timeout(self, timeout: int) -> dict:
374379
@property
375380
def pir_state(self) -> PIRStatus:
376381
"""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"])
378383

379384
async def get_pir_state(self) -> PIRStatus:
380385
"""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"])
383389

384390
@property
385391
def adc_value(self) -> int:
@@ -389,14 +395,14 @@ def adc_value(self) -> int:
389395
@property
390396
def pir_value(self) -> int:
391397
"""Return the computed PIR sensor value."""
392-
return self.pir_state.get_pir_value(self.pir_config)
398+
return self.pir_state.pir_value
393399

394400
@property
395401
def pir_percent(self) -> float:
396402
"""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
398404

399405
@property
400406
def pir_triggered(self) -> bool:
401407
"""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

Comments
 (0)
0