Reolink unregistered webhook on unexpected error (#87538)

This commit is contained in:
starkillerOG 2023-02-08 12:23:27 +01:00 committed by Paulus Schoutsen
parent 640f5f41cc
commit 0a69f825d2
2 changed files with 25 additions and 21 deletions

View File

@ -54,6 +54,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
raise ConfigEntryNotReady( raise ConfigEntryNotReady(
f"Error while trying to setup {host.api.host}:{host.api.port}: {str(err)}" f"Error while trying to setup {host.api.host}:{host.api.port}: {str(err)}"
) from err ) from err
except Exception: # pylint: disable=broad-except
await host.stop()
raise
config_entry.async_on_unload( config_entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, host.stop) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, host.stop)

View File

@ -138,24 +138,27 @@ class ReolinkHost:
async def disconnect(self): async def disconnect(self):
"""Disconnect from the API, so the connection will be released.""" """Disconnect from the API, so the connection will be released."""
await self._api.unsubscribe()
try: try:
await self._api.logout() await self._api.unsubscribe()
except aiohttp.ClientConnectorError as err: except (
aiohttp.ClientConnectorError,
asyncio.TimeoutError,
ReolinkError,
) as err:
_LOGGER.error( _LOGGER.error(
"Reolink connection error while logging out for host %s:%s: %s", "Reolink error while unsubscribing from host %s:%s: %s",
self._api.host, self._api.host,
self._api.port, self._api.port,
str(err), str(err),
) )
except asyncio.TimeoutError:
_LOGGER.error( try:
"Reolink connection timeout while logging out for host %s:%s", await self._api.logout()
self._api.host, except (
self._api.port, aiohttp.ClientConnectorError,
) asyncio.TimeoutError,
except ReolinkError as err: ReolinkError,
) as err:
_LOGGER.error( _LOGGER.error(
"Reolink error while logging out for host %s:%s: %s", "Reolink error while logging out for host %s:%s: %s",
self._api.host, self._api.host,
@ -165,13 +168,13 @@ class ReolinkHost:
async def stop(self, event=None): async def stop(self, event=None):
"""Disconnect the API.""" """Disconnect the API."""
await self.unregister_webhook() self.unregister_webhook()
await self.disconnect() await self.disconnect()
async def subscribe(self) -> None: async def subscribe(self) -> None:
"""Subscribe to motion events and register the webhook as a callback.""" """Subscribe to motion events and register the webhook as a callback."""
if self.webhook_id is None: if self.webhook_id is None:
await self.register_webhook() self.register_webhook()
if self._api.subscribed: if self._api.subscribed:
_LOGGER.debug( _LOGGER.debug(
@ -248,7 +251,7 @@ class ReolinkHost:
self._api.host, self._api.host,
) )
async def register_webhook(self) -> None: def register_webhook(self) -> None:
"""Register the webhook for motion events.""" """Register the webhook for motion events."""
self.webhook_id = f"{DOMAIN}_{self.unique_id.replace(':', '')}_ONVIF" self.webhook_id = f"{DOMAIN}_{self.unique_id.replace(':', '')}_ONVIF"
event_id = self.webhook_id event_id = self.webhook_id
@ -263,8 +266,7 @@ class ReolinkHost:
try: try:
base_url = get_url(self._hass, prefer_external=True) base_url = get_url(self._hass, prefer_external=True)
except NoURLAvailableError as err: except NoURLAvailableError as err:
webhook.async_unregister(self._hass, event_id) self.unregister_webhook()
self.webhook_id = None
raise ReolinkWebhookException( raise ReolinkWebhookException(
f"Error registering URL for webhook {event_id}: " f"Error registering URL for webhook {event_id}: "
"HomeAssistant URL is not available" "HomeAssistant URL is not available"
@ -275,11 +277,10 @@ class ReolinkHost:
_LOGGER.debug("Registered webhook: %s", event_id) _LOGGER.debug("Registered webhook: %s", event_id)
async def unregister_webhook(self): def unregister_webhook(self):
"""Unregister the webhook for motion events.""" """Unregister the webhook for motion events."""
if self.webhook_id: _LOGGER.debug("Unregistering webhook %s", self.webhook_id)
_LOGGER.debug("Unregistering webhook %s", self.webhook_id) webhook.async_unregister(self._hass, self.webhook_id)
webhook.async_unregister(self._hass, self.webhook_id)
self.webhook_id = None self.webhook_id = None
async def handle_webhook( async def handle_webhook(