mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 11:13:34 +00:00
Add waterleak alert timestamp (#1162)
The T300 reports the timestamp of the last alarm, this exposes it to consumers.
This commit is contained in:
parent
acd0202cab
commit
8a17752ae2
@ -2,10 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartmodule import SmartModule
|
||||
from ..smartmodule import Module, SmartModule
|
||||
|
||||
|
||||
class WaterleakStatus(Enum):
|
||||
@ -47,6 +48,18 @@ class WaterleakSensor(SmartModule):
|
||||
type=Feature.Type.BinarySensor,
|
||||
)
|
||||
)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
self._device,
|
||||
id="water_alert_timestamp",
|
||||
name="Last alert timestamp",
|
||||
container=self,
|
||||
attribute_getter="alert_timestamp",
|
||||
icon="mdi:alert",
|
||||
category=Feature.Category.Info,
|
||||
type=Feature.Type.Sensor,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
@ -62,3 +75,14 @@ class WaterleakSensor(SmartModule):
|
||||
def alert(self) -> bool:
|
||||
"""Return true if alarm is active."""
|
||||
return self._device.sys_info["in_alarm"]
|
||||
|
||||
@property
|
||||
def alert_timestamp(self) -> datetime | None:
|
||||
"""Return timestamp of the last leak trigger."""
|
||||
# The key is not always be there, maybe if it hasn't ever been triggered?
|
||||
if "trigger_timestamp" not in self._device.sys_info:
|
||||
return None
|
||||
|
||||
ts = self._device.sys_info["trigger_timestamp"]
|
||||
tz = self._device.modules[Module.Time].timezone
|
||||
return datetime.fromtimestamp(ts, tz=tz)
|
||||
|
1073
kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json
vendored
1073
kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json
vendored
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
import pytest
|
||||
@ -15,6 +16,8 @@ waterleak = parametrize(
|
||||
("feature", "prop_name", "type"),
|
||||
[
|
||||
("water_alert", "alert", int),
|
||||
# Can be converted to 'datetime | None' after py3.9 support is dropped
|
||||
("water_alert_timestamp", "alert_timestamp", (datetime, type(None))),
|
||||
("water_leak", "status", Enum),
|
||||
],
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user