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

waterleaksensor: use parent's Time for alert timestamp by rytilahti · Pull Request #1614 · python-kasa/python-kasa · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 14 additions & 1 deletion kasa/smart/modules/waterleaksensor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from datetime import datetime
from enum import Enum
from typing import TYPE_CHECKING

from ...feature import Feature
from ...interfaces.time import Time
from ..smartmodule import Module, SmartModule


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

@property
def _time_module(self) -> Time:
"""Return time module from the parent for timestamp calculation."""
parent = self._device.parent
if TYPE_CHECKING:
from ..smartdevice import SmartDevice

assert isinstance(parent, SmartDevice)

return parent.modules[Module.Time]

@property
def alert_timestamp(self) -> datetime | None:
"""Return timestamp of the last leak trigger."""
Expand All @@ -84,5 +97,5 @@ def alert_timestamp(self) -> datetime | None:
return None

ts = self._device.sys_info["trigger_timestamp"]
tz = self._device.modules[Module.Time].timezone
tz = self._time_module.timezone
return datetime.fromtimestamp(ts, tz=tz)
16 changes: 14 additions & 2 deletions tests/smart/modules/test_waterleak.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

from kasa.smart.modules import WaterleakSensor

from ...conftest import get_device_for_fixture_protocol
from ...device_fixtures import parametrize

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


@pytest.fixture
async def parent(request):
"""Get a dummy parent for tz tests."""
return await get_device_for_fixture_protocol("H100(EU)_1.0_1.5.5.json", "SMART")


@waterleak
@pytest.mark.parametrize(
("feature", "prop_name", "type"),
Expand All @@ -21,8 +28,9 @@
("water_leak", "status", Enum),
],
)
async def test_waterleak_properties(dev, feature, prop_name, type):
async def test_waterleak_properties(dev, parent, feature, prop_name, type):
"""Test that features are registered and work as expected."""
dev._parent = parent
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]

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


@waterleak
async def test_waterleak_features(dev):
async def test_waterleak_features(dev, parent):
"""Test waterleak features."""
dev._parent = parent
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]

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

assert "water_alert" in dev.features
assert dev.features["water_alert"].value == waterleak.alert

assert "water_alert_timestamp" in dev.features
assert dev.features["water_alert_timestamp"].value == waterleak.alert_timestamp
Loading

Back | FazBrowse Home | New Git URL