FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

waterleaksensor: use parent's Time for alert timestamp (#1614) · python-kasa/python-kasa@a87926f · GitHub

Repository navigation

Commit a87926f

Browse files
authored
waterleaksensor: use parent's Time for alert timestamp (#1614)
We do not, by design, add Time module for hub's children. This has a side-effect that we need to fallback to the parent's time module to allow presenting the correct timestamp for the last alert.
1 parent 347bf9a commit a87926f

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎kasa/smart/modules/waterleaksensor.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from datetime import datetime
66
from enum import Enum
7+
from typing import TYPE_CHECKING
78

89
from ...feature import Feature
10+
from ...interfaces.time import Time
911
from ..smartmodule import Module, SmartModule
1012

1113

@@ -76,6 +78,17 @@ def alert(self) -> bool:
7678
"""Return true if alarm is active."""
7779
return self._device.sys_info["in_alarm"]
7880

81+
@property
82+
def _time_module(self) -> Time:
83+
"""Return time module from the parent for timestamp calculation."""
84+
parent = self._device.parent
85+
if TYPE_CHECKING:
86+
from ..smartdevice import SmartDevice
87+
88+
assert isinstance(parent, SmartDevice)
89+
90+
return parent.modules[Module.Time]
91+
7992
@property
8093
def alert_timestamp(self) -> datetime | None:
8194
"""Return timestamp of the last leak trigger."""
@@ -84,5 +97,5 @@ def alert_timestamp(self) -> datetime | None:
8497
return None
8598

8699
ts = self._device.sys_info["trigger_timestamp"]
87-
tz = self._device.modules[Module.Time].timezone
100+
tz = self._time_module.timezone
88101
return datetime.fromtimestamp(ts, tz=tz)

‎tests/smart/modules/test_waterleak.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55

66
from kasa.smart.modules import WaterleakSensor
77

8+
from ...conftest import get_device_for_fixture_protocol
89
from ...device_fixtures import parametrize
910

1011
waterleak = parametrize(
1112
"has waterleak", component_filter="sensor_alarm", protocol_filter={"SMART.CHILD"}
1213
)
1314

1415

16+
@pytest.fixture
17+
async def parent(request):
18+
"""Get a dummy parent for tz tests."""
19+
return await get_device_for_fixture_protocol("H100(EU)_1.0_1.5.5.json", "SMART")
20+
21+
1522
@waterleak
1623
@pytest.mark.parametrize(
1724
("feature", "prop_name", "type"),
@@ -21,8 +28,9 @@
2128
("water_leak", "status", Enum),
2229
],
2330
)
24-
async def test_waterleak_properties(dev, feature, prop_name, type):
31+
async def test_waterleak_properties(dev, parent, feature, prop_name, type):
2532
"""Test that features are registered and work as expected."""
33+
dev._parent = parent
2634
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
2735

2836
prop = getattr(waterleak, prop_name)
@@ -34,12 +42,16 @@ async def test_waterleak_properties(dev, feature, prop_name, type):
3442

3543

3644
@waterleak
37-
async def test_waterleak_features(dev):
45+
async def test_waterleak_features(dev, parent):
3846
"""Test waterleak features."""
47+
dev._parent = parent
3948
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
4049

4150
assert "water_leak" in dev.features
4251
assert dev.features["water_leak"].value == waterleak.status
4352

4453
assert "water_alert" in dev.features
4554
assert dev.features["water_alert"].value == waterleak.alert
55+
56+
assert "water_alert_timestamp" in dev.features
57+
assert dev.features["water_alert_timestamp"].value == waterleak.alert_timestamp

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL