Reolink unregistered webhook on unexpected error (#87538)

This commit is contained in:
starkillerOG 2023-02-08 12:23:27 +01:00 committed by GitHub
parent 2545694d41
commit 5e214f2e43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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(
f"Error while trying to setup {host.api.host}:{host.api.port}: {str(err)}"
) from err
except Exception: # pylint: disable=broad-except
await host.stop()
raise
config_entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, host.stop)

View File

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