2024-02-19 17:01:31 +00:00
|
|
|
"""Implementation of time module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-10-08 07:16:51 +00:00
|
|
|
from datetime import datetime, timedelta, timezone, tzinfo
|
2024-09-28 18:14:31 +00:00
|
|
|
from typing import cast
|
2024-02-19 17:01:31 +00:00
|
|
|
|
2024-10-15 07:59:25 +00:00
|
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
2024-10-08 07:16:51 +00:00
|
|
|
|
2024-10-08 11:21:01 +00:00
|
|
|
from ...cachedzoneinfo import CachedZoneInfo
|
2024-02-19 17:01:31 +00:00
|
|
|
from ...feature import Feature
|
2024-10-15 07:59:25 +00:00
|
|
|
from ...interfaces import Time as TimeInterface
|
2024-02-19 17:01:31 +00:00
|
|
|
from ..smartmodule import SmartModule
|
|
|
|
|
|
|
|
|
2024-10-15 07:59:25 +00:00
|
|
|
class Time(SmartModule, TimeInterface):
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Implementation of device_local_time."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "time"
|
|
|
|
QUERY_GETTER_NAME = "get_device_time"
|
|
|
|
|
2024-10-08 11:21:01 +00:00
|
|
|
_timezone: tzinfo = timezone.utc
|
|
|
|
|
2024-09-28 18:14:31 +00:00
|
|
|
def _initialize_features(self):
|
|
|
|
"""Initialize features after the initial update."""
|
2024-02-19 17:01:31 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
2024-09-28 18:14:31 +00:00
|
|
|
device=self._device,
|
2024-06-21 16:42:43 +00:00
|
|
|
id="device_time",
|
|
|
|
name="Device time",
|
2024-02-19 17:01:31 +00:00
|
|
|
attribute_getter="time",
|
|
|
|
container=self,
|
2024-06-23 06:39:34 +00:00
|
|
|
category=Feature.Category.Debug,
|
2024-06-25 16:30:36 +00:00
|
|
|
type=Feature.Type.Sensor,
|
2024-02-19 17:01:31 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-10-08 11:21:01 +00:00
|
|
|
async def _post_update_hook(self):
|
|
|
|
"""Perform actions after a device update."""
|
2024-02-19 17:01:31 +00:00
|
|
|
td = timedelta(minutes=cast(float, self.data.get("time_diff")))
|
2024-10-08 07:16:51 +00:00
|
|
|
if region := self.data.get("region"):
|
|
|
|
try:
|
|
|
|
# Zoneinfo will return a DST aware object
|
2024-10-08 11:21:01 +00:00
|
|
|
tz: tzinfo = await CachedZoneInfo.get_cached_zone_info(region)
|
2024-10-08 07:16:51 +00:00
|
|
|
except ZoneInfoNotFoundError:
|
|
|
|
tz = timezone(td, region)
|
2024-02-19 17:01:31 +00:00
|
|
|
else:
|
|
|
|
# in case the device returns a blank region this will result in the
|
|
|
|
# tzname being a UTC offset
|
|
|
|
tz = timezone(td)
|
2024-10-08 11:21:01 +00:00
|
|
|
self._timezone = tz
|
|
|
|
|
|
|
|
@property
|
|
|
|
def timezone(self) -> tzinfo:
|
|
|
|
"""Return current timezone."""
|
|
|
|
return self._timezone
|
2024-10-08 07:16:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def time(self) -> datetime:
|
|
|
|
"""Return device's current datetime."""
|
2024-02-19 17:01:31 +00:00
|
|
|
return datetime.fromtimestamp(
|
|
|
|
cast(float, self.data.get("timestamp")),
|
2024-10-08 07:16:51 +00:00
|
|
|
tz=self.timezone,
|
2024-02-19 17:01:31 +00:00
|
|
|
)
|
|
|
|
|
2024-10-15 07:59:25 +00:00
|
|
|
async def set_time(self, dt: datetime) -> dict:
|
2024-02-19 17:01:31 +00:00
|
|
|
"""Set device time."""
|
2024-10-15 07:59:25 +00:00
|
|
|
if not dt.tzinfo:
|
|
|
|
timestamp = dt.replace(tzinfo=self.timezone).timestamp()
|
|
|
|
utc_offset = cast(timedelta, self.timezone.utcoffset(dt))
|
|
|
|
else:
|
|
|
|
timestamp = dt.timestamp()
|
|
|
|
utc_offset = cast(timedelta, dt.utcoffset())
|
|
|
|
time_diff = utc_offset / timedelta(minutes=1)
|
|
|
|
|
|
|
|
params: dict[str, int | str] = {
|
|
|
|
"timestamp": int(timestamp),
|
|
|
|
"time_diff": int(time_diff),
|
|
|
|
}
|
|
|
|
if tz := dt.tzinfo:
|
|
|
|
region = tz.key if isinstance(tz, ZoneInfo) else dt.tzname()
|
|
|
|
# tzname can return null if a simple timezone object is provided.
|
|
|
|
if region:
|
|
|
|
params["region"] = region
|
|
|
|
return await self.call("set_device_time", params)
|
2024-10-23 15:17:27 +00:00
|
|
|
|
|
|
|
async def _check_supported(self):
|
|
|
|
"""Additional check to see if the module is supported by the device.
|
|
|
|
|
|
|
|
Hub attached sensors report the time module but do return device time.
|
|
|
|
"""
|
|
|
|
if self._device._is_hub_child:
|
|
|
|
return False
|
|
|
|
return await super()._check_supported()
|