Avoid creating tasks for checking integrations platforms (#110795)
* Avoid creating tasks for checking integrations platforms This is a followup to #110743 to avoid creating a task to check if the integration platform exists. We created tasks because we needed to await async_get_integrations but since its always called from EVENT_COMPONENT_LOADED firing, we can use the async_get_loaded_integration version which does not need to be awaited. This eliminates one task for every loaded component * there is no more race risk * reduce * coro or callback * reduce * tweak * race safe * fix type * fixes * use built-in helper to make it smaller * use built-in helper to make it smaller * use built-in helper to make it smaller * add coverage to ensure exceptions are logged * improve readability a bit * platforms
This commit is contained in:
parent
a656e14b20
commit
33ff6b5b6e
4 changed files with 152 additions and 91 deletions
|
@ -4,14 +4,21 @@ from __future__ import annotations
|
|||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from functools import partial
|
||||
import logging
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.const import EVENT_COMPONENT_LOADED
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.loader import Integration, async_get_integrations, bind_hass
|
||||
from homeassistant.core import Event, HassJob, HomeAssistant, callback
|
||||
from homeassistant.loader import (
|
||||
Integration,
|
||||
async_get_integrations,
|
||||
async_get_loaded_integration,
|
||||
bind_hass,
|
||||
)
|
||||
from homeassistant.setup import ATTR_COMPONENT
|
||||
from homeassistant.util.logging import catch_log_exception
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DATA_INTEGRATION_PLATFORMS = "integration_platforms"
|
||||
|
@ -22,31 +29,12 @@ class IntegrationPlatform:
|
|||
"""An integration platform."""
|
||||
|
||||
platform_name: str
|
||||
process_platform: Callable[[HomeAssistant, str, Any], Awaitable[None]]
|
||||
process_job: HassJob
|
||||
seen_components: set[str]
|
||||
|
||||
|
||||
async def _async_process_single_integration_platform_component(
|
||||
hass: HomeAssistant,
|
||||
component_name: str,
|
||||
platform: ModuleType,
|
||||
integration_platform: IntegrationPlatform,
|
||||
) -> None:
|
||||
"""Process a single integration platform."""
|
||||
if component_name in integration_platform.seen_components:
|
||||
return
|
||||
integration_platform.seen_components.add(component_name)
|
||||
try:
|
||||
await integration_platform.process_platform(hass, component_name, platform)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception(
|
||||
"Error processing platform %s.%s",
|
||||
component_name,
|
||||
integration_platform.platform_name,
|
||||
)
|
||||
|
||||
|
||||
def _get_platform_from_integration(
|
||||
@callback
|
||||
def _get_platform(
|
||||
integration: Integration | Exception, component_name: str, platform_name: str
|
||||
) -> ModuleType | None:
|
||||
"""Get a platform from an integration."""
|
||||
|
@ -71,36 +59,32 @@ def _get_platform_from_integration(
|
|||
return None
|
||||
|
||||
|
||||
async def _async_process_integration_platform_for_component(
|
||||
hass: HomeAssistant, component_name: str
|
||||
@callback
|
||||
def _async_process_integration_platforms_for_component(
|
||||
hass: HomeAssistant, integration_platforms: list[IntegrationPlatform], event: Event
|
||||
) -> None:
|
||||
"""Process integration platforms for a component."""
|
||||
integration_platforms: list[IntegrationPlatform] = hass.data[
|
||||
DATA_INTEGRATION_PLATFORMS
|
||||
]
|
||||
integrations = await async_get_integrations(hass, (component_name,))
|
||||
tasks = [
|
||||
asyncio.create_task(
|
||||
_async_process_single_integration_platform_component(
|
||||
hass,
|
||||
component_name,
|
||||
platform,
|
||||
integration_platform,
|
||||
),
|
||||
name=f"process integration platform {integration_platform.platform_name} for {component_name}",
|
||||
)
|
||||
for integration_platform in integration_platforms
|
||||
if component_name not in integration_platform.seen_components
|
||||
and (
|
||||
platform := _get_platform_from_integration(
|
||||
integrations[component_name],
|
||||
component_name,
|
||||
integration_platform.platform_name,
|
||||
component_name: str = event.data[ATTR_COMPONENT]
|
||||
if "." in component_name:
|
||||
return
|
||||
|
||||
integration = async_get_loaded_integration(hass, component_name)
|
||||
for integration_platform in integration_platforms:
|
||||
if component_name in integration_platform.seen_components or not (
|
||||
platform := _get_platform(
|
||||
integration, component_name, integration_platform.platform_name
|
||||
)
|
||||
):
|
||||
continue
|
||||
integration_platform.seen_components.add(component_name)
|
||||
hass.async_run_hass_job(
|
||||
integration_platform.process_job, hass, component_name, platform
|
||||
)
|
||||
]
|
||||
if tasks:
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
|
||||
def _format_err(name: str, platform_name: str, *args: Any) -> str:
|
||||
"""Format error message."""
|
||||
return f"Exception in {name} when processing platform '{platform_name}': {args}"
|
||||
|
||||
|
||||
@bind_hass
|
||||
|
@ -108,52 +92,44 @@ async def async_process_integration_platforms(
|
|||
hass: HomeAssistant,
|
||||
platform_name: str,
|
||||
# Any = platform.
|
||||
process_platform: Callable[[HomeAssistant, str, Any], Awaitable[None]],
|
||||
process_platform: Callable[[HomeAssistant, str, Any], Awaitable[None] | None],
|
||||
) -> None:
|
||||
"""Process a specific platform for all current and future loaded integrations."""
|
||||
if DATA_INTEGRATION_PLATFORMS not in hass.data:
|
||||
hass.data[DATA_INTEGRATION_PLATFORMS] = []
|
||||
|
||||
async def _async_component_loaded(event: Event) -> None:
|
||||
"""Handle a new component loaded."""
|
||||
await _async_process_integration_platform_for_component(
|
||||
hass, event.data[ATTR_COMPONENT]
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_component_loaded_filter(event: Event) -> bool:
|
||||
"""Handle integration platforms loaded."""
|
||||
return "." not in event.data[ATTR_COMPONENT]
|
||||
|
||||
integration_platforms: list[IntegrationPlatform] = []
|
||||
hass.data[DATA_INTEGRATION_PLATFORMS] = integration_platforms
|
||||
hass.bus.async_listen(
|
||||
EVENT_COMPONENT_LOADED,
|
||||
_async_component_loaded,
|
||||
event_filter=_async_component_loaded_filter,
|
||||
partial(
|
||||
_async_process_integration_platforms_for_component,
|
||||
hass,
|
||||
integration_platforms,
|
||||
),
|
||||
)
|
||||
else:
|
||||
integration_platforms = hass.data[DATA_INTEGRATION_PLATFORMS]
|
||||
|
||||
integration_platforms: list[IntegrationPlatform] = hass.data[
|
||||
DATA_INTEGRATION_PLATFORMS
|
||||
]
|
||||
integration_platform = IntegrationPlatform(platform_name, process_platform, set())
|
||||
top_level_components = {comp for comp in hass.config.components if "." not in comp}
|
||||
process_job = HassJob(
|
||||
catch_log_exception(
|
||||
process_platform,
|
||||
partial(_format_err, str(process_platform), platform_name),
|
||||
),
|
||||
f"process_platform {platform_name}",
|
||||
)
|
||||
integration_platform = IntegrationPlatform(
|
||||
platform_name, process_job, top_level_components
|
||||
)
|
||||
integration_platforms.append(integration_platform)
|
||||
if top_level_components := [
|
||||
comp for comp in hass.config.components if "." not in comp
|
||||
|
||||
if not top_level_components:
|
||||
return
|
||||
|
||||
integrations = await async_get_integrations(hass, top_level_components)
|
||||
if futures := [
|
||||
future
|
||||
for comp in top_level_components
|
||||
if (platform := _get_platform(integrations[comp], comp, platform_name))
|
||||
and (future := hass.async_run_hass_job(process_job, hass, comp, platform))
|
||||
]:
|
||||
integrations = await async_get_integrations(hass, top_level_components)
|
||||
tasks = [
|
||||
asyncio.create_task(
|
||||
_async_process_single_integration_platform_component(
|
||||
hass, comp, platform, integration_platform
|
||||
),
|
||||
name=f"process integration platform {platform_name} for {comp}",
|
||||
)
|
||||
for comp in top_level_components
|
||||
if comp not in integration_platform.seen_components
|
||||
and (
|
||||
platform := _get_platform_from_integration(
|
||||
integrations[comp], comp, platform_name
|
||||
)
|
||||
)
|
||||
]
|
||||
if tasks:
|
||||
await asyncio.gather(*tasks)
|
||||
await asyncio.gather(*futures)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue