From 6cc5bee960518fd7e554c7e878e5937d584f2d62 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 29 May 2023 13:53:52 -0500 Subject: [PATCH] Fix onvif setup failing when unable to parse camera time (#93677) --- homeassistant/components/onvif/device.py | 46 +++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/onvif/device.py b/homeassistant/components/onvif/device.py index e470b3c700b..a524d8ea519 100644 --- a/homeassistant/components/onvif/device.py +++ b/homeassistant/components/onvif/device.py @@ -23,7 +23,7 @@ from homeassistant.const import ( CONF_USERNAME, Platform, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback import homeassistant.util.dt as dt_util from .const import ( @@ -189,14 +189,19 @@ class ONVIFDevice: dt_param.DateTimeType = "Manual" # Retrieve DST setting from system dt_param.DaylightSavings = bool(time.localtime().tm_isdst) - dt_param.UTCDateTime = device_time.UTCDateTime + dt_param.UTCDateTime = { + "Date": { + "Year": system_date.year, + "Month": system_date.month, + "Day": system_date.day, + }, + "Time": { + "Hour": system_date.hour, + "Minute": system_date.minute, + "Second": system_date.second, + }, + } # Retrieve timezone from system - 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 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: @@ -283,6 +288,22 @@ class ONVIFDevice: if abs(self._dt_diff_seconds) < 5: return + if device_time.DateTimeType != "Manual": + self._async_log_time_out_of_sync(cam_date_utc, system_date) + return + + # Set Date and Time ourselves if Date and Time is set manually in the camera. + try: + await self.async_manually_set_date_and_time() + except (RequestError, TransportError, IndexError, Fault): + LOGGER.warning("%s: Could not sync date/time on this camera", self.name) + self._async_log_time_out_of_sync(cam_date_utc, system_date) + + @callback + def _async_log_time_out_of_sync( + self, cam_date_utc: dt.datetime, system_date: dt.datetime + ) -> None: + """Log a warning if the camera and system date/time are not synced.""" LOGGER.warning( ( "The date/time on %s (UTC) is '%s', " @@ -294,15 +315,6 @@ class ONVIFDevice: system_date, ) - if device_time.DateTimeType != "Manual": - return - - # Set Date and Time ourselves if Date and Time is set manually in the camera. - try: - await self.async_manually_set_date_and_time() - except (RequestError, TransportError, IndexError, Fault): - LOGGER.warning("%s: Could not sync date/time on this camera", self.name) - async def async_get_device_info(self) -> DeviceInfo: """Obtain information about this device.""" device_mgmt = await self.device.create_devicemgmt_service()