mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Stabilise on_since value for smart devices (#1144)
Caches the `on_since` value to prevent jitter caused by the device calculations.
This commit is contained in:
@@ -66,6 +66,7 @@ class SmartDevice(Device):
|
||||
self._children: Mapping[str, SmartDevice] = {}
|
||||
self._last_update = {}
|
||||
self._last_update_time: float | None = None
|
||||
self._on_since: datetime | None = None
|
||||
|
||||
async def _initialize_children(self):
|
||||
"""Initialize children for power strips."""
|
||||
@@ -494,15 +495,25 @@ class SmartDevice(Device):
|
||||
|
||||
@property
|
||||
def on_since(self) -> datetime | None:
|
||||
"""Return the time that the device was turned on or None if turned off."""
|
||||
"""Return the time that the device was turned on or None if turned off.
|
||||
|
||||
This returns a cached value if the device reported value difference is under
|
||||
five seconds to avoid device-caused jitter.
|
||||
"""
|
||||
if (
|
||||
not self._info.get("device_on")
|
||||
or (on_time := self._info.get("on_time")) is None
|
||||
):
|
||||
self._on_since = None
|
||||
return None
|
||||
|
||||
on_time = cast(float, on_time)
|
||||
return self.time - timedelta(seconds=on_time)
|
||||
on_since = self.time - timedelta(seconds=on_time)
|
||||
if not self._on_since or timedelta(
|
||||
seconds=0
|
||||
) < on_since - self._on_since > timedelta(seconds=5):
|
||||
self._on_since = on_since
|
||||
return self._on_since
|
||||
|
||||
@property
|
||||
def timezone(self) -> dict:
|
||||
|
Reference in New Issue
Block a user