mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-26 16:46:23 +00:00
Add timezone to on_since attributes (#978)
This allows them to displayed in HA without errors.
This commit is contained in:
parent
4cf395483f
commit
6cdbbefb90
@ -18,7 +18,7 @@ import collections.abc
|
|||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
||||||
|
|
||||||
from ..device import Device, WifiNetwork
|
from ..device import Device, WifiNetwork
|
||||||
@ -345,7 +345,8 @@ class IotDevice(Device):
|
|||||||
category=Feature.Category.Debug,
|
category=Feature.Category.Debug,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if "on_time" in self._sys_info:
|
# iot strips calculate on_since from the children
|
||||||
|
if "on_time" in self._sys_info or self.device_type == Device.Type.Strip:
|
||||||
self._add_feature(
|
self._add_feature(
|
||||||
Feature(
|
Feature(
|
||||||
device=self,
|
device=self,
|
||||||
@ -665,7 +666,9 @@ class IotDevice(Device):
|
|||||||
|
|
||||||
on_time = self._sys_info["on_time"]
|
on_time = self._sys_info["on_time"]
|
||||||
|
|
||||||
return datetime.now().replace(microsecond=0) - timedelta(seconds=on_time)
|
return datetime.now(timezone.utc).astimezone().replace(
|
||||||
|
microsecond=0
|
||||||
|
) - timedelta(seconds=on_time)
|
||||||
|
|
||||||
@property # type: ignore
|
@property # type: ignore
|
||||||
@requires_update
|
@requires_update
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from ..device_type import DeviceType
|
from ..device_type import DeviceType
|
||||||
@ -148,7 +148,7 @@ class IotStrip(IotDevice):
|
|||||||
if self.is_off:
|
if self.is_off:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return max(plug.on_since for plug in self.children if plug.on_since is not None)
|
return min(plug.on_since for plug in self.children if plug.on_since is not None)
|
||||||
|
|
||||||
async def current_consumption(self) -> float:
|
async def current_consumption(self) -> float:
|
||||||
"""Get the current power consumption in watts."""
|
"""Get the current power consumption in watts."""
|
||||||
@ -372,7 +372,9 @@ class IotStripPlug(IotPlug):
|
|||||||
info = self._get_child_info()
|
info = self._get_child_info()
|
||||||
on_time = info["on_time"]
|
on_time = info["on_time"]
|
||||||
|
|
||||||
return datetime.now().replace(microsecond=0) - timedelta(seconds=on_time)
|
return datetime.now(timezone.utc).astimezone().replace(
|
||||||
|
microsecond=0
|
||||||
|
) - timedelta(seconds=on_time)
|
||||||
|
|
||||||
@property # type: ignore
|
@property # type: ignore
|
||||||
@requires_update
|
@requires_update
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
||||||
|
|
||||||
from ..aestransport import AesTransport
|
from ..aestransport import AesTransport
|
||||||
@ -357,12 +357,25 @@ class SmartDevice(Device):
|
|||||||
@property
|
@property
|
||||||
def time(self) -> datetime:
|
def time(self) -> datetime:
|
||||||
"""Return the time."""
|
"""Return the time."""
|
||||||
if self._parent and Module.Time in self._parent.modules:
|
if (self._parent and (time_mod := self._parent.modules.get(Module.Time))) or (
|
||||||
_timemod = self._parent.modules[Module.Time]
|
time_mod := self.modules.get(Module.Time)
|
||||||
else:
|
):
|
||||||
_timemod = self.modules[Module.Time]
|
return time_mod.time
|
||||||
|
|
||||||
return _timemod.time
|
# We have no device time, use current local time.
|
||||||
|
return datetime.now(timezone.utc).astimezone().replace(microsecond=0)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def on_since(self) -> datetime | None:
|
||||||
|
"""Return the time that the device was turned on or None if turned off."""
|
||||||
|
if (
|
||||||
|
not self._info.get("device_on")
|
||||||
|
or (on_time := self._info.get("on_time")) is None
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
|
||||||
|
on_time = cast(float, on_time)
|
||||||
|
return self.time - timedelta(seconds=on_time)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def timezone(self) -> dict:
|
def timezone(self) -> dict:
|
||||||
@ -489,20 +502,6 @@ class SmartDevice(Device):
|
|||||||
energy = self.modules[Module.Energy]
|
energy = self.modules[Module.Energy]
|
||||||
return energy.emeter_today
|
return energy.emeter_today
|
||||||
|
|
||||||
@property
|
|
||||||
def on_since(self) -> datetime | None:
|
|
||||||
"""Return the time that the device was turned on or None if turned off."""
|
|
||||||
if (
|
|
||||||
not self._info.get("device_on")
|
|
||||||
or (on_time := self._info.get("on_time")) is None
|
|
||||||
):
|
|
||||||
return None
|
|
||||||
on_time = cast(float, on_time)
|
|
||||||
if (timemod := self.modules.get(Module.Time)) is not None:
|
|
||||||
return timemod.time - timedelta(seconds=on_time)
|
|
||||||
else: # We have no device time, use current local time.
|
|
||||||
return datetime.now().replace(microsecond=0) - timedelta(seconds=on_time)
|
|
||||||
|
|
||||||
async def wifi_scan(self) -> list[WifiNetwork]:
|
async def wifi_scan(self) -> list[WifiNetwork]:
|
||||||
"""Scan for available wifi networks."""
|
"""Scan for available wifi networks."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user