Reduce overhead to fetch integrations (#93767)

We call this path over and over during startup and most
of the time the integration is already loaded. We want
that case to be the short path
This commit is contained in:
J. Nick Koston 2023-05-29 19:58:51 -05:00 committed by GitHub
parent 1ea202a5bc
commit 53fe74e055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -891,14 +891,15 @@ async def async_get_integrations(
results: dict[str, Integration | Exception] = {}
needed: dict[str, asyncio.Future[None]] = {}
in_progress: dict[str, asyncio.Future[None]] = {}
if TYPE_CHECKING:
cache = cast(dict[str, Integration | asyncio.Future[None]], cache)
for domain in domains:
int_or_fut: Integration | asyncio.Future[None] | None = cache.get(
domain, _UNDEF
)
if isinstance(int_or_fut, asyncio.Future):
in_progress[domain] = int_or_fut
int_or_fut = cache.get(domain, _UNDEF)
# Integration is never subclassed, so we can check for type
if type(int_or_fut) is Integration: # pylint: disable=unidiomatic-typecheck
results[domain] = int_or_fut
elif int_or_fut is not _UNDEF:
results[domain] = cast(Integration, int_or_fut)
in_progress[domain] = cast(asyncio.Future[None], int_or_fut)
elif "." in domain:
results[domain] = ValueError(f"Invalid domain {domain}")
else:
@ -915,8 +916,10 @@ async def async_get_integrations(
else:
results[domain] = cast(Integration, int_or_fut)
if not needed:
return results
# First we look for custom components
if needed:
# Instead of using resolve_from_root we use the cache of custom
# components to find the integration.
custom = await async_get_custom_components(hass)