Enhance logging for ZHA device trigger validation (#76036)

* Enhance logging for ZHA device trigger validation

* use IntegrationError
pull/76119/head
David F. Mulcahey 2022-08-01 11:43:07 -04:00 committed by Franck Nijhof
parent b962a6e767
commit 6b588d41ff
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 21 additions and 5 deletions

View File

@ -142,6 +142,7 @@ class ZHAGateway:
self._log_relay_handler = LogRelayHandler(hass, self)
self.config_entry = config_entry
self._unsubs: list[Callable[[], None]] = []
self.initialized: bool = False
async def async_initialize(self) -> None:
"""Initialize controller and connect radio."""
@ -183,6 +184,7 @@ class ZHAGateway:
self._hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(self.coordinator_ieee)
self.async_load_devices()
self.async_load_groups()
self.initialized = True
@callback
def async_load_devices(self) -> None:
@ -217,7 +219,7 @@ class ZHAGateway:
async def async_initialize_devices_and_entities(self) -> None:
"""Initialize devices and load entities."""
_LOGGER.debug("Loading all devices")
_LOGGER.debug("Initializing all devices from Zigpy cache")
await asyncio.gather(
*(dev.async_initialize(from_cache=True) for dev in self.devices.values())
)

View File

@ -26,6 +26,7 @@ import zigpy.zdo.types as zdo_types
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.exceptions import IntegrationError
from homeassistant.helpers import device_registry as dr
from .const import (
@ -42,6 +43,7 @@ if TYPE_CHECKING:
from .gateway import ZHAGateway
_T = TypeVar("_T")
_LOGGER = logging.getLogger(__name__)
@dataclass
@ -170,10 +172,22 @@ def async_get_zha_device(hass: HomeAssistant, device_id: str) -> ZHADevice:
device_registry = dr.async_get(hass)
registry_device = device_registry.async_get(device_id)
if not registry_device:
_LOGGER.error("Device id `%s` not found in registry", device_id)
raise KeyError(f"Device id `{device_id}` not found in registry.")
zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY]
ieee_address = list(list(registry_device.identifiers)[0])[1]
ieee = zigpy.types.EUI64.convert(ieee_address)
if not zha_gateway.initialized:
_LOGGER.error("Attempting to get a ZHA device when ZHA is not initialized")
raise IntegrationError("ZHA is not initialized yet")
try:
ieee_address = list(list(registry_device.identifiers)[0])[1]
ieee = zigpy.types.EUI64.convert(ieee_address)
except (IndexError, ValueError) as ex:
_LOGGER.error(
"Unable to determine device IEEE for device with device id `%s`", device_id
)
raise KeyError(
f"Unable to determine device IEEE for device with device id `{device_id}`."
) from ex
return zha_gateway.devices[ieee]

View File

@ -13,7 +13,7 @@ from homeassistant.components.device_automation.exceptions import (
from homeassistant.components.homeassistant.triggers import event as event_trigger
from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, IntegrationError
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN
@ -39,7 +39,7 @@ async def async_validate_trigger_config(
trigger = (config[CONF_TYPE], config[CONF_SUBTYPE])
try:
zha_device = async_get_zha_device(hass, config[CONF_DEVICE_ID])
except (KeyError, AttributeError) as err:
except (KeyError, AttributeError, IntegrationError) as err:
raise InvalidDeviceAutomationConfig from err
if (
zha_device.device_automation_triggers is None