From aeae4edb74256c575ed318919ed959b00f57d19f Mon Sep 17 00:00:00 2001 From: Jason Hunter Date: Mon, 18 May 2020 23:02:23 -0400 Subject: [PATCH] Fix ONVIF subscription renewal (#35792) * fix subscription renewal * catch ValueError for #35762 --- homeassistant/components/onvif/device.py | 16 ++++++++++++++-- homeassistant/components/onvif/event.py | 3 ++- homeassistant/components/onvif/parsers.py | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/onvif/device.py b/homeassistant/components/onvif/device.py index c15f4bf6877..8e69e148da3 100644 --- a/homeassistant/components/onvif/device.py +++ b/homeassistant/components/onvif/device.py @@ -54,6 +54,8 @@ class ONVIFDevice: self.profiles: List[Profile] = [] self.max_resolution: int = 0 + self._dt_diff_seconds: int = 0 + @property def name(self) -> str: """Return the name of this device.""" @@ -100,6 +102,16 @@ class ONVIFDevice: if self.capabilities.ptz: self.device.create_ptz_service() + if self._dt_diff_seconds > 300 and self.capabilities.events: + self.capabilities.events = False + LOGGER.warning( + "The system clock on '%s' is more than 5 minutes off. " + "Although this device supports events, they will be " + "disabled until the device clock is fixed as we will " + "not be able to renew the subscription.", + self.name, + ) + if self.capabilities.events: self.events = EventManager( self.hass, self.device, self.config_entry.unique_id @@ -179,9 +191,9 @@ class ONVIFDevice: ) dt_diff = cam_date - system_date - dt_diff_seconds = dt_diff.total_seconds() + self._dt_diff_seconds = dt_diff.total_seconds() - if dt_diff_seconds > 5: + if self._dt_diff_seconds > 5: LOGGER.warning( "The date/time on the device (UTC) is '%s', " "which is different from the system '%s', " diff --git a/homeassistant/components/onvif/event.py b/homeassistant/components/onvif/event.py index ee7d4349dc4..888fe5bd92b 100644 --- a/homeassistant/components/onvif/event.py +++ b/homeassistant/components/onvif/event.py @@ -104,7 +104,8 @@ class EventManager: if not self._subscription: return - await self._subscription.Renew(dt_util.utcnow() + dt.timedelta(minutes=10)) + termination_time = (dt_util.utcnow() + dt.timedelta(minutes=30)).isoformat() + await self._subscription.Renew(termination_time) async def async_pull_messages(self, _now: dt = None) -> None: """Pull messages from device.""" diff --git a/homeassistant/components/onvif/parsers.py b/homeassistant/components/onvif/parsers.py index 4fd4ffd2891..438601106b5 100644 --- a/homeassistant/components/onvif/parsers.py +++ b/homeassistant/components/onvif/parsers.py @@ -308,7 +308,7 @@ async def async_parse_last_reboot(uid: str, msg) -> Event: dt_util.parse_datetime(msg.Message._value_1.Data.SimpleItem[0].Value) ), ) - except (AttributeError, KeyError): + except (AttributeError, KeyError, ValueError): return None @@ -331,7 +331,7 @@ async def async_parse_last_reset(uid: str, msg) -> Event: ), entity_enabled=False, ) - except (AttributeError, KeyError): + except (AttributeError, KeyError, ValueError): return None @@ -354,5 +354,5 @@ async def async_parse_last_clock_sync(uid: str, msg) -> Event: ), entity_enabled=False, ) - except (AttributeError, KeyError): + except (AttributeError, KeyError, ValueError): return None