Fix onvif setup when time set service is not functional (#92447)

This commit is contained in:
J. Nick Koston 2023-05-04 04:50:12 -05:00 committed by GitHub
parent 7dc5d131b4
commit 322c5152be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,7 @@ from httpx import RequestError
import onvif import onvif
from onvif import ONVIFCamera from onvif import ONVIFCamera
from onvif.exceptions import ONVIFError from onvif.exceptions import ONVIFError
from zeep.exceptions import Fault, XMLParseError from zeep.exceptions import Fault, TransportError, XMLParseError
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -203,11 +203,17 @@ class ONVIFDevice:
"""Warns if device and system date not synced.""" """Warns if device and system date not synced."""
LOGGER.debug("%s: Setting up the ONVIF device management service", self.name) LOGGER.debug("%s: Setting up the ONVIF device management service", self.name)
device_mgmt = self.device.create_devicemgmt_service() device_mgmt = self.device.create_devicemgmt_service()
system_date = dt_util.utcnow()
LOGGER.debug("%s: Retrieving current device date/time", self.name) LOGGER.debug("%s: Retrieving current device date/time", self.name)
try: try:
system_date = dt_util.utcnow()
device_time = await device_mgmt.GetSystemDateAndTime() device_time = await device_mgmt.GetSystemDateAndTime()
except RequestError as err:
LOGGER.warning(
"Couldn't get device '%s' date/time. Error: %s", self.name, err
)
return
if not device_time: if not device_time:
LOGGER.debug( LOGGER.debug(
"""Couldn't get device '%s' date/time. """Couldn't get device '%s' date/time.
@ -227,10 +233,9 @@ class ONVIFDevice:
tzone = dt_util.get_time_zone(device_time.TimeZone.TZ) or tzone tzone = dt_util.get_time_zone(device_time.TimeZone.TZ) or tzone
if cdate is None: if cdate is None:
LOGGER.warning( LOGGER.warning("%s: Could not retrieve date/time on this camera", self.name)
"%s: Could not retrieve date/time on this camera", self.name return
)
else:
cam_date = dt.datetime( cam_date = dt.datetime(
cdate.Date.Year, cdate.Date.Year,
cdate.Date.Month, cdate.Date.Month,
@ -255,7 +260,9 @@ class ONVIFDevice:
self._dt_diff_seconds = dt_diff.total_seconds() self._dt_diff_seconds = dt_diff.total_seconds()
# It could be off either direction, so we need to check the absolute value # It could be off either direction, so we need to check the absolute value
if abs(self._dt_diff_seconds) > 5: if abs(self._dt_diff_seconds) < 5:
return
LOGGER.warning( LOGGER.warning(
( (
"The date/time on %s (UTC) is '%s', " "The date/time on %s (UTC) is '%s', "
@ -266,13 +273,15 @@ class ONVIFDevice:
cam_date_utc, cam_date_utc,
system_date, system_date,
) )
if device_time.DateTimeType == "Manual":
if device_time.DateTimeType != "Manual":
return
# Set Date and Time ourselves if Date and Time is set manually in the camera. # Set Date and Time ourselves if Date and Time is set manually in the camera.
try:
await self.async_manually_set_date_and_time() await self.async_manually_set_date_and_time()
except RequestError as err: except (RequestError, TransportError):
LOGGER.warning( LOGGER.warning("%s: Could not sync date/time on this camera", self.name)
"Couldn't get device '%s' date/time. Error: %s", self.name, err
)
async def async_get_device_info(self) -> DeviceInfo: async def async_get_device_info(self) -> DeviceInfo:
"""Obtain information about this device.""" """Obtain information about this device."""
@ -328,7 +337,7 @@ class ONVIFDevice:
"""Start the event handler.""" """Start the event handler."""
with suppress(*GET_CAPABILITIES_EXCEPTIONS, XMLParseError): with suppress(*GET_CAPABILITIES_EXCEPTIONS, XMLParseError):
onvif_capabilities = self.onvif_capabilities or {} onvif_capabilities = self.onvif_capabilities or {}
pull_point_support = onvif_capabilities.get("Events", {}).get( pull_point_support = (onvif_capabilities.get("Events") or {}).get(
"WSPullPointSupport" "WSPullPointSupport"
) )
LOGGER.debug("%s: WSPullPointSupport: %s", self.name, pull_point_support) LOGGER.debug("%s: WSPullPointSupport: %s", self.name, pull_point_support)