Add fallback to device zone time or no timezone to onvif when setting time fails (#91882)

This commit is contained in:
J. Nick Koston 2023-04-24 07:37:56 -05:00 committed by GitHub
parent ddb3955a23
commit 392a9f32c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -151,15 +151,31 @@ class ONVIFDevice:
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."""
@ -216,7 +232,8 @@ class ONVIFDevice:
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', "