mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-12 10:28:01 +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:
@@ -318,6 +318,7 @@ class IotStripPlug(IotPlug):
|
||||
self._set_sys_info(parent.sys_info)
|
||||
self._device_type = DeviceType.StripSocket
|
||||
self.protocol = parent.protocol # Must use the same connection as the parent
|
||||
self._on_since: datetime | None = None
|
||||
|
||||
async def _initialize_modules(self):
|
||||
"""Initialize modules not added in init."""
|
||||
@@ -438,14 +439,20 @@ class IotStripPlug(IotPlug):
|
||||
def on_since(self) -> datetime | None:
|
||||
"""Return on-time, if available."""
|
||||
if self.is_off:
|
||||
self._on_since = None
|
||||
return None
|
||||
|
||||
info = self._get_child_info()
|
||||
on_time = info["on_time"]
|
||||
|
||||
return datetime.now(timezone.utc).astimezone().replace(
|
||||
microsecond=0
|
||||
) - timedelta(seconds=on_time)
|
||||
time = datetime.now(timezone.utc).astimezone().replace(microsecond=0)
|
||||
|
||||
on_since = 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 # type: ignore
|
||||
@requires_update
|
||||
|
Reference in New Issue
Block a user