Small performance improvement to async_get_config_flows (#110666)

- Migrates to using a future instead of Event like we have done
  everywhere else
This commit is contained in:
J. Nick Koston 2024-02-16 07:32:26 -06:00 committed by GitHub
parent 35149a46fc
commit 95015fbb40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -240,20 +240,23 @@ async def async_get_custom_components(
hass: HomeAssistant, hass: HomeAssistant,
) -> dict[str, Integration]: ) -> dict[str, Integration]:
"""Return cached list of custom integrations.""" """Return cached list of custom integrations."""
if (reg_or_evt := hass.data.get(DATA_CUSTOM_COMPONENTS)) is None: comps_or_future: dict[str, Integration] | asyncio.Future[
evt = hass.data[DATA_CUSTOM_COMPONENTS] = asyncio.Event() dict[str, Integration]
] | None = hass.data.get(DATA_CUSTOM_COMPONENTS)
reg = await _async_get_custom_components(hass) if comps_or_future is None:
future = hass.data[DATA_CUSTOM_COMPONENTS] = hass.loop.create_future()
hass.data[DATA_CUSTOM_COMPONENTS] = reg comps = await _async_get_custom_components(hass)
evt.set()
return reg
if isinstance(reg_or_evt, asyncio.Event): hass.data[DATA_CUSTOM_COMPONENTS] = comps
await reg_or_evt.wait() future.set_result(comps)
return cast(dict[str, "Integration"], hass.data.get(DATA_CUSTOM_COMPONENTS)) return comps
return cast(dict[str, "Integration"], reg_or_evt) if isinstance(comps_or_future, asyncio.Future):
return await comps_or_future
return comps_or_future
async def async_get_config_flows( async def async_get_config_flows(
@ -271,12 +274,10 @@ async def async_get_config_flows(
flows.update(type_flows) flows.update(type_flows)
flows.update( flows.update(
[ integration.domain
integration.domain for integration in integrations.values()
for integration in integrations.values() if integration.config_flow
if integration.config_flow and (type_filter is None or integration.integration_type == type_filter)
and (type_filter is None or integration.integration_type == type_filter)
]
) )
return flows return flows