Fix HKC exceptions during BLE startup not being caught (#80882)

This commit is contained in:
J. Nick Koston 2022-10-24 09:29:13 -05:00 committed by GitHub
parent 9978296ae2
commit a7610909de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -35,6 +35,7 @@ from .const import (
IDENTIFIER_LEGACY_ACCESSORY_ID,
IDENTIFIER_LEGACY_SERIAL_NUMBER,
IDENTIFIER_SERIAL_NUMBER,
STARTUP_EXCEPTIONS,
)
from .device_trigger import async_fire_triggers, async_setup_triggers_for_entry
@ -187,11 +188,12 @@ class HKDevice:
"""
try:
await self.pairing.async_populate_accessories_state(force_update=True)
except (asyncio.TimeoutError, AccessoryNotFoundError):
except STARTUP_EXCEPTIONS as ex:
_LOGGER.debug(
"Failed to populate BLE accessory state for %s, accessory may be sleeping"
" and will be retried the next time it advertises",
" and will be retried the next time it advertises: %s",
self.config_entry.title,
ex,
)
async def async_setup(self) -> None:
@ -220,7 +222,7 @@ class HKDevice:
# BLE devices may sleep and we can't force a connection
raise
entry.async_on_unload(
self.hass.bus.async_listen_once(
self.hass.bus.async_listen(
EVENT_HOMEASSISTANT_STARTED,
self._async_retry_populate_ble_accessory_state,
)

View file

@ -1,6 +1,12 @@
"""Constants for the homekit_controller component."""
import asyncio
from typing import Final
from aiohomekit.exceptions import (
AccessoryDisconnectedError,
AccessoryNotFoundError,
EncryptionError,
)
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes
@ -94,3 +100,10 @@ CHARACTERISTIC_PLATFORMS = {
# Device classes
DEVICE_CLASS_ECOBEE_MODE: Final = "homekit_controller__ecobee_mode"
STARTUP_EXCEPTIONS = (
asyncio.TimeoutError,
AccessoryNotFoundError,
EncryptionError,
AccessoryDisconnectedError,
)