Prevent onvif from blocking startup (#38256)

This commit is contained in:
J. Nick Koston 2020-07-26 20:51:53 -10:00 committed by GitHub
parent da30ed06d8
commit 8b06d1d4bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ from aiohttp.client_exceptions import ServerDisconnectedError
from onvif import ONVIFCamera, ONVIFService from onvif import ONVIFCamera, ONVIFService
from zeep.exceptions import Fault 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.helpers.event import async_track_point_in_utc_time
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
@ -114,30 +114,31 @@ class EventManager:
async def async_pull_messages(self, _now: dt = None) -> None: async def async_pull_messages(self, _now: dt = None) -> None:
"""Pull messages from device.""" """Pull messages from device."""
try: if self.hass.state == CoreState.running:
pullpoint = self.device.create_pullpoint_service() try:
req = pullpoint.create_type("PullMessages") pullpoint = self.device.create_pullpoint_service()
req.MessageLimit = 100 req = pullpoint.create_type("PullMessages")
req.Timeout = dt.timedelta(seconds=60) req.MessageLimit = 100
response = await pullpoint.PullMessages(req) req.Timeout = dt.timedelta(seconds=60)
response = await pullpoint.PullMessages(req)
# Renew subscription if less than two hours is left # Renew subscription if less than two hours is left
if ( if (
dt_util.as_utc(response.TerminationTime) - dt_util.utcnow() dt_util.as_utc(response.TerminationTime) - dt_util.utcnow()
).total_seconds() < 7200: ).total_seconds() < 7200:
await self.async_renew() await self.async_renew()
# Parse response # Parse response
await self.async_parse_messages(response.NotificationMessage) await self.async_parse_messages(response.NotificationMessage)
except ServerDisconnectedError: except ServerDisconnectedError:
pass pass
except Fault: except Fault:
pass pass
# Update entities # Update entities
for update_callback in self._listeners: for update_callback in self._listeners:
update_callback() update_callback()
# Reschedule another pull # Reschedule another pull
if self._listeners: if self._listeners: