hass-core/homeassistant/components/homeassistant/trigger.py
J. Nick Koston cfe14bca8f
Add a helper to import modules from the event loop (#113169)
* Add a helper to import modules in the event loop

Replaces the one used for triggers with a more generic helper
that can be reused and uses a future to avoid importing concurrently

* Add a helper to import modules in the event loop

Replaces the one used for triggers with a more generic helper
that can be reused and uses a future to avoid importing concurrently

* coverage

* make sure we do not retry

* coverage
2024-03-14 00:26:33 -04:00

45 lines
1.5 KiB
Python

"""Home Assistant trigger dispatcher."""
from typing import cast
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.importlib import async_import_module
from homeassistant.helpers.trigger import (
TriggerActionType,
TriggerInfo,
TriggerProtocol,
)
from homeassistant.helpers.typing import ConfigType
async def _async_get_trigger_platform(
hass: HomeAssistant, platform_name: str
) -> TriggerProtocol:
"""Get trigger platform from cache or import it."""
platform = await async_import_module(
hass, f"homeassistant.components.homeassistant.triggers.{platform_name}"
)
return cast(TriggerProtocol, platform)
async def async_validate_trigger_config(
hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate config."""
platform = await _async_get_trigger_platform(hass, config[CONF_PLATFORM])
if hasattr(platform, "async_validate_trigger_config"):
return await platform.async_validate_trigger_config(hass, config)
return platform.TRIGGER_SCHEMA(config) # type: ignore[no-any-return]
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach trigger of specified platform."""
platform = await _async_get_trigger_platform(hass, config[CONF_PLATFORM])
return await platform.async_attach_trigger(hass, config, action, trigger_info)