mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Add timezone to on_since attributes (#978)
This allows them to displayed in HA without errors.
This commit is contained in:
@@ -18,7 +18,7 @@ import collections.abc
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
||||
|
||||
from ..device import Device, WifiNetwork
|
||||
@@ -345,7 +345,8 @@ class IotDevice(Device):
|
||||
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(
|
||||
Feature(
|
||||
device=self,
|
||||
@@ -665,7 +666,9 @@ class IotDevice(Device):
|
||||
|
||||
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
|
||||
@requires_update
|
||||
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
|
||||
from ..device_type import DeviceType
|
||||
@@ -148,7 +148,7 @@ class IotStrip(IotDevice):
|
||||
if self.is_off:
|
||||
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:
|
||||
"""Get the current power consumption in watts."""
|
||||
@@ -372,7 +372,9 @@ class IotStripPlug(IotPlug):
|
||||
info = self._get_child_info()
|
||||
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
|
||||
@requires_update
|
||||
|
Reference in New Issue
Block a user