diff --git a/homeassistant/components/homekit_controller/__init__.py b/homeassistant/components/homekit_controller/__init__.py index 54da6e71c8c..aa56bbbda78 100644 --- a/homeassistant/components/homekit_controller/__init__.py +++ b/homeassistant/components/homekit_controller/__init__.py @@ -21,7 +21,7 @@ from homeassistant.helpers.typing import ConfigType from .config_flow import normalize_hkid from .connection import HKDevice -from .const import KNOWN_DEVICES, TRIGGERS +from .const import KNOWN_DEVICES from .utils import async_get_controller _LOGGER = logging.getLogger(__name__) @@ -59,7 +59,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await async_get_controller(hass) hass.data[KNOWN_DEVICES] = {} - hass.data[TRIGGERS] = {} async def _async_stop_homekit_controller(event: Event) -> None: await asyncio.gather( diff --git a/homeassistant/components/homekit_controller/device_trigger.py b/homeassistant/components/homekit_controller/device_trigger.py index bc1434f4bd9..229c8aecc00 100644 --- a/homeassistant/components/homekit_controller/device_trigger.py +++ b/homeassistant/components/homekit_controller/device_trigger.py @@ -224,7 +224,7 @@ async def async_setup_triggers_for_entry( # They have to be different accessories (they can be on the same bridge) # In practice, this is inline with what iOS actually supports AFAWCT. device_id = conn.devices[aid] - if device_id in hass.data[TRIGGERS]: + if TRIGGERS in hass.data and device_id in hass.data[TRIGGERS]: return False # Just because we recognize the service type doesn't mean we can actually @@ -246,15 +246,18 @@ def async_get_or_create_trigger_source( hass: HomeAssistant, device_id: str ) -> TriggerSource: """Get or create a trigger source for a device id.""" - if not (source := hass.data[TRIGGERS].get(device_id)): + trigger_sources: dict[str, TriggerSource] = hass.data.setdefault(TRIGGERS, {}) + if not (source := trigger_sources.get(device_id)): source = TriggerSource(hass) - hass.data[TRIGGERS][device_id] = source + trigger_sources[device_id] = source return source def async_fire_triggers(conn: HKDevice, events: dict[tuple[int, int], dict[str, Any]]): """Process events generated by a HomeKit accessory into automation triggers.""" - trigger_sources: dict[str, TriggerSource] = conn.hass.data[TRIGGERS] + trigger_sources: dict[str, TriggerSource] = conn.hass.data.get(TRIGGERS, {}) + if not trigger_sources: + return for (aid, iid), ev in events.items(): if aid in conn.devices: device_id = conn.devices[aid]