Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add timezone to on_since attributes #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions kasa/iot/iotdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -345,7 +345,8 @@ async def _initialize_features(self):
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,
Expand Down Expand Up @@ -665,7 +666,9 @@ def on_since(self) -> datetime | None:

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
Expand Down
8 changes: 5 additions & 3 deletions kasa/iot/iotstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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
Expand Down Expand Up @@ -148,7 +148,7 @@ def on_since(self) -> datetime | None:
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."""
Expand Down Expand Up @@ -372,7 +372,9 @@ def on_since(self) -> datetime | None:
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
Expand Down
39 changes: 19 additions & 20 deletions kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import base64
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast

from ..aestransport import AesTransport
Expand Down Expand Up @@ -357,12 +357,25 @@
@property
def time(self) -> datetime:
"""Return the time."""
if self._parent and Module.Time in self._parent.modules:
_timemod = self._parent.modules[Module.Time]
else:
_timemod = self.modules[Module.Time]
if (self._parent and (time_mod := self._parent.modules.get(Module.Time))) or (
time_mod := self.modules.get(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)

Check warning on line 366 in kasa/smart/smartdevice.py

View check run for this annotation

Codecov / codecov/patch

kasa/smart/smartdevice.py#L366

Added line #L366 was not covered by tests

@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
def timezone(self) -> dict:
Expand Down Expand Up @@ -489,20 +502,6 @@
energy = self.modules[Module.Energy]
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]:
"""Scan for available wifi networks."""

Expand Down
Loading