From 8b06d1d4bd1f65e8b4c8018909d86f7e42f8d620 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 Jul 2020 20:51:53 -1000 Subject: [PATCH] Prevent onvif from blocking startup (#38256) --- homeassistant/components/onvif/event.py | 43 +++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/onvif/event.py b/homeassistant/components/onvif/event.py index 9084a06e7db..60a92e56ac0 100644 --- a/homeassistant/components/onvif/event.py +++ b/homeassistant/components/onvif/event.py @@ -6,7 +6,7 @@ from aiohttp.client_exceptions import ServerDisconnectedError from onvif import ONVIFCamera, ONVIFService from zeep.exceptions import Fault -from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from homeassistant.core import CALLBACK_TYPE, CoreState, HomeAssistant, callback from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.util import dt as dt_util @@ -114,30 +114,31 @@ class EventManager: async def async_pull_messages(self, _now: dt = None) -> None: """Pull messages from device.""" - try: - pullpoint = self.device.create_pullpoint_service() - req = pullpoint.create_type("PullMessages") - req.MessageLimit = 100 - req.Timeout = dt.timedelta(seconds=60) - response = await pullpoint.PullMessages(req) + if self.hass.state == CoreState.running: + try: + pullpoint = self.device.create_pullpoint_service() + req = pullpoint.create_type("PullMessages") + req.MessageLimit = 100 + req.Timeout = dt.timedelta(seconds=60) + response = await pullpoint.PullMessages(req) - # Renew subscription if less than two hours is left - if ( - dt_util.as_utc(response.TerminationTime) - dt_util.utcnow() - ).total_seconds() < 7200: - await self.async_renew() + # Renew subscription if less than two hours is left + if ( + dt_util.as_utc(response.TerminationTime) - dt_util.utcnow() + ).total_seconds() < 7200: + await self.async_renew() - # Parse response - await self.async_parse_messages(response.NotificationMessage) + # Parse response + await self.async_parse_messages(response.NotificationMessage) - except ServerDisconnectedError: - pass - except Fault: - pass + except ServerDisconnectedError: + pass + except Fault: + pass - # Update entities - for update_callback in self._listeners: - update_callback() + # Update entities + for update_callback in self._listeners: + update_callback() # Reschedule another pull if self._listeners: