Skip to content

Commit

Permalink
Add fallback to device zone time or no timezone to onvif when setting…
Browse files Browse the repository at this point in the history
… time fails (#91882)
  • Loading branch information
bdraco authored Apr 24, 2023
1 parent ddb3955 commit 392a9f3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions homeassistant/components/onvif/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,31 @@ async def async_manually_set_date_and_time(self) -> None:
dt_param.DaylightSavings = bool(time.localtime().tm_isdst)
dt_param.UTCDateTime = device_time.UTCDateTime
# Retrieve timezone from system
dt_param.TimeZone = str(system_date.astimezone().tzinfo)
dt_param.UTCDateTime.Date.Year = system_date.year
dt_param.UTCDateTime.Date.Month = system_date.month
dt_param.UTCDateTime.Date.Day = system_date.day
dt_param.UTCDateTime.Time.Hour = system_date.hour
dt_param.UTCDateTime.Time.Minute = system_date.minute
dt_param.UTCDateTime.Time.Second = system_date.second
LOGGER.debug("SetSystemDateAndTime: %s", dt_param)
await device_mgmt.SetSystemDateAndTime(dt_param)
system_timezone = str(system_date.astimezone().tzinfo)
timezone_names: list[str | None] = [system_timezone]
if (time_zone := device_time.TimeZone) and system_timezone != time_zone.TZ:
timezone_names.append(time_zone.TZ)
timezone_names.append(None)
timezone_max_idx = len(timezone_names) - 1
LOGGER.debug(
"%s: SetSystemDateAndTime: timezone_names:%s", self.name, timezone_names
)
for idx, timezone_name in enumerate(timezone_names):
dt_param.TimeZone = timezone_name
LOGGER.debug("%s: SetSystemDateAndTime: %s", self.name, dt_param)
try:
await device_mgmt.SetSystemDateAndTime(dt_param)
LOGGER.debug("%s: SetSystemDateAndTime: success", self.name)
return
except Fault:
if idx == timezone_max_idx:
raise

async def async_check_date_and_time(self) -> None:
"""Warns if device and system date not synced."""
Expand Down Expand Up @@ -216,7 +232,8 @@ async def async_check_date_and_time(self) -> None:
dt_diff = cam_date - system_date
self._dt_diff_seconds = dt_diff.total_seconds()

if self._dt_diff_seconds > 5:
# It could be off either direction, so we need to check the absolute value
if abs(self._dt_diff_seconds) > 5:
LOGGER.warning(
(
"The date/time on %s (UTC) is '%s', "
Expand Down

0 comments on commit 392a9f3

Please sign in to comment.