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