Fix Doorbird warning if registering favorites fail (#67262)

This commit is contained in:
Alan Tse 2022-02-26 00:56:07 -08:00 committed by GitHub
parent a3d30f6ecc
commit 34d38c7ada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from http import HTTPStatus from http import HTTPStatus
import logging import logging
from typing import Any
from aiohttp import web from aiohttp import web
from doorbirdpy import DoorBird from doorbirdpy import DoorBird
@ -166,7 +167,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok return unload_ok
async def _async_register_events(hass, doorstation): async def _async_register_events(
hass: HomeAssistant, doorstation: ConfiguredDoorBird
) -> bool:
try: try:
await hass.async_add_executor_job(doorstation.register_events, hass) await hass.async_add_executor_job(doorstation.register_events, hass)
except requests.exceptions.HTTPError: except requests.exceptions.HTTPError:
@ -243,7 +246,7 @@ class ConfiguredDoorBird:
"""Get token for device.""" """Get token for device."""
return self._token return self._token
def register_events(self, hass): def register_events(self, hass: HomeAssistant) -> None:
"""Register events on device.""" """Register events on device."""
# Get the URL of this server # Get the URL of this server
hass_url = get_url(hass) hass_url = get_url(hass)
@ -258,9 +261,10 @@ class ConfiguredDoorBird:
favorites = self.device.favorites() favorites = self.device.favorites()
for event in self.doorstation_events: for event in self.doorstation_events:
self._register_event(hass_url, event, favs=favorites) if self._register_event(hass_url, event, favs=favorites):
_LOGGER.info(
_LOGGER.info("Successfully registered URL for %s on %s", event, self.name) "Successfully registered URL for %s on %s", event, self.name
)
@property @property
def slug(self): def slug(self):
@ -270,21 +274,25 @@ class ConfiguredDoorBird:
def _get_event_name(self, event): def _get_event_name(self, event):
return f"{self.slug}_{event}" return f"{self.slug}_{event}"
def _register_event(self, hass_url, event, favs=None): def _register_event(
self, hass_url: str, event: str, favs: dict[str, Any] | None = None
) -> bool:
"""Add a schedule entry in the device for a sensor.""" """Add a schedule entry in the device for a sensor."""
url = f"{hass_url}{API_URL}/{event}?token={self._token}" url = f"{hass_url}{API_URL}/{event}?token={self._token}"
# Register HA URL as webhook if not already, then get the ID # Register HA URL as webhook if not already, then get the ID
if self.webhook_is_registered(url, favs=favs): if self.webhook_is_registered(url, favs=favs):
return return True
self.device.change_favorite("http", f"Home Assistant ({event})", url) self.device.change_favorite("http", f"Home Assistant ({event})", url)
if not self.webhook_is_registered(url): if not self.webhook_is_registered(url):
_LOGGER.warning( _LOGGER.warning(
'Could not find favorite for URL "%s". ' 'Skipping sensor "%s"', 'Unable to set favorite URL "%s". ' 'Event "%s" will not fire',
url, url,
event, event,
) )
return False
return True
def webhook_is_registered(self, url, favs=None) -> bool: def webhook_is_registered(self, url, favs=None) -> bool:
"""Return whether the given URL is registered as a device favorite.""" """Return whether the given URL is registered as a device favorite."""