Fix race setting up homekit controller triggers (#83166)

fixes https://github.com/home-assistant/core/issues/83165
This commit is contained in:
J. Nick Koston 2022-12-02 13:49:14 -10:00 committed by GitHub
parent 144f1b918b
commit cc105e5c27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -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(

View File

@ -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]