Fix onvif setup failing when unable to parse camera time (#93677)

pull/88639/head
J. Nick Koston 2023-05-29 13:53:52 -05:00 committed by GitHub
parent 2f1f32f0bb
commit 6cc5bee960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 17 deletions

View File

@ -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()