mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-01 21:58:17 +00:00
waterleaksensor: use parent's Time for alert timestamp (#1614)
Some checks are pending
CI / Perform linting checks (3.13) (push) Waiting to run
CI / Python 3.11 on macos-latest (push) Blocked by required conditions
CI / Python 3.12 on macos-latest (push) Blocked by required conditions
CI / Python 3.13 on macos-latest (push) Blocked by required conditions
CI / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.11 on windows-latest (push) Blocked by required conditions
CI / Python 3.12 on windows-latest (push) Blocked by required conditions
CI / Python 3.13 on windows-latest (push) Blocked by required conditions
CodeQL checks / Analyze (python) (push) Waiting to run
Some checks are pending
CI / Perform linting checks (3.13) (push) Waiting to run
CI / Python 3.11 on macos-latest (push) Blocked by required conditions
CI / Python 3.12 on macos-latest (push) Blocked by required conditions
CI / Python 3.13 on macos-latest (push) Blocked by required conditions
CI / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.11 on windows-latest (push) Blocked by required conditions
CI / Python 3.12 on windows-latest (push) Blocked by required conditions
CI / Python 3.13 on windows-latest (push) Blocked by required conditions
CodeQL checks / Analyze (python) (push) Waiting to run
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.
This commit is contained in:
@@ -4,8 +4,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from ...feature import Feature
|
from ...feature import Feature
|
||||||
|
from ...interfaces.time import Time
|
||||||
from ..smartmodule import Module, SmartModule
|
from ..smartmodule import Module, SmartModule
|
||||||
|
|
||||||
|
|
||||||
@@ -76,6 +78,17 @@ class WaterleakSensor(SmartModule):
|
|||||||
"""Return true if alarm is active."""
|
"""Return true if alarm is active."""
|
||||||
return self._device.sys_info["in_alarm"]
|
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
|
@property
|
||||||
def alert_timestamp(self) -> datetime | None:
|
def alert_timestamp(self) -> datetime | None:
|
||||||
"""Return timestamp of the last leak trigger."""
|
"""Return timestamp of the last leak trigger."""
|
||||||
@@ -84,5 +97,5 @@ class WaterleakSensor(SmartModule):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
ts = self._device.sys_info["trigger_timestamp"]
|
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)
|
return datetime.fromtimestamp(ts, tz=tz)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import pytest
|
|||||||
|
|
||||||
from kasa.smart.modules import WaterleakSensor
|
from kasa.smart.modules import WaterleakSensor
|
||||||
|
|
||||||
|
from ...conftest import get_device_for_fixture_protocol
|
||||||
from ...device_fixtures import parametrize
|
from ...device_fixtures import parametrize
|
||||||
|
|
||||||
waterleak = parametrize(
|
waterleak = parametrize(
|
||||||
@@ -12,6 +13,12 @@ waterleak = parametrize(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
@waterleak
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("feature", "prop_name", "type"),
|
("feature", "prop_name", "type"),
|
||||||
@@ -21,8 +28,9 @@ waterleak = parametrize(
|
|||||||
("water_leak", "status", Enum),
|
("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."""
|
"""Test that features are registered and work as expected."""
|
||||||
|
dev._parent = parent
|
||||||
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
|
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
|
||||||
|
|
||||||
prop = getattr(waterleak, prop_name)
|
prop = getattr(waterleak, prop_name)
|
||||||
@@ -34,8 +42,9 @@ async def test_waterleak_properties(dev, feature, prop_name, type):
|
|||||||
|
|
||||||
|
|
||||||
@waterleak
|
@waterleak
|
||||||
async def test_waterleak_features(dev):
|
async def test_waterleak_features(dev, parent):
|
||||||
"""Test waterleak features."""
|
"""Test waterleak features."""
|
||||||
|
dev._parent = parent
|
||||||
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
|
waterleak: WaterleakSensor = dev.modules["WaterleakSensor"]
|
||||||
|
|
||||||
assert "water_leak" in dev.features
|
assert "water_leak" in dev.features
|
||||||
@@ -43,3 +52,6 @@ async def test_waterleak_features(dev):
|
|||||||
|
|
||||||
assert "water_alert" in dev.features
|
assert "water_alert" in dev.features
|
||||||
assert dev.features["water_alert"].value == waterleak.alert
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user