From b8143a79442e971440e3d90d12201957542ad6ac Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Feb 2024 10:46:06 -0600 Subject: [PATCH] Improve performance of _async_when_setup (#110791) * Improve performance of _async_when_setup Use an event filter to avoid creating a task until the filter matches * Improve performance of _async_when_setup Use an event filter to avoid creating a task until the filter matches --- homeassistant/setup.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 0cd4d3ea103..47dcd7fcc57 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -522,12 +522,19 @@ def _async_when_setup( listener() await when_setup() - async def _loaded_event(event: core.Event) -> None: - """Call the callback if we loaded the expected component.""" - if event.data[ATTR_COMPONENT] == component: - await _matched_event(event) + @callback + def _async_is_component_filter(event: core.Event) -> bool: + """Check if the event is for the component.""" + event_comp: str = event.data[ATTR_COMPONENT] + return event_comp == component - listeners.append(hass.bus.async_listen(EVENT_COMPONENT_LOADED, _loaded_event)) + listeners.append( + hass.bus.async_listen( + EVENT_COMPONENT_LOADED, + _matched_event, + event_filter=_async_is_component_filter, + ) + ) if start_event: listeners.append( hass.bus.async_listen(EVENT_HOMEASSISTANT_START, _matched_event)