Move AutomationActionType to helpers.trigger (#76790)

This commit is contained in:
Marc Mueller 2022-08-15 14:37:11 +02:00 committed by GitHub
parent 4239757c2c
commit c93d9d9a90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 46 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
import logging import logging
from typing import Any, Protocol, TypedDict, cast from typing import Any, cast
import voluptuous as vol import voluptuous as vol
from voluptuous.humanize import humanize_error from voluptuous.humanize import humanize_error
@ -72,8 +72,13 @@ from homeassistant.helpers.trace import (
trace_get, trace_get,
trace_path, trace_path,
) )
from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.helpers.trigger import (
from homeassistant.helpers.typing import ConfigType, TemplateVarsType TriggerActionType,
TriggerData,
TriggerInfo,
async_initialize_triggers,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.util.dt import parse_datetime from homeassistant.util.dt import parse_datetime
@ -112,32 +117,11 @@ SERVICE_TRIGGER = "trigger"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class AutomationActionType(Protocol): # AutomationActionType, AutomationTriggerData,
"""Protocol type for automation action callback.""" # and AutomationTriggerInfo are deprecated as of 2022.9.
AutomationActionType = TriggerActionType
async def __call__( AutomationTriggerData = TriggerData
self, AutomationTriggerInfo = TriggerInfo
run_variables: dict[str, Any],
context: Context | None = None,
) -> None:
"""Define action callback type."""
class AutomationTriggerData(TypedDict):
"""Automation trigger data."""
id: str
idx: str
class AutomationTriggerInfo(TypedDict):
"""Information about automation trigger."""
domain: str
name: str
home_assistant_start: bool
variables: TemplateVarsType
trigger_data: AutomationTriggerData
@bind_hass @bind_hass

View File

@ -1,10 +1,6 @@
"""Provides device triggers for binary sensors.""" """Provides device triggers for binary sensors."""
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.device_automation.const import ( from homeassistant.components.device_automation.const import (
CONF_TURNED_OFF, CONF_TURNED_OFF,
@ -15,6 +11,7 @@ from homeassistant.const import CONF_ENTITY_ID, CONF_FOR, CONF_TYPE
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.entity import get_device_class from homeassistant.helpers.entity import get_device_class
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, BinarySensorDeviceClass from . import DOMAIN, BinarySensorDeviceClass
@ -263,8 +260,8 @@ TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger( async def async_attach_trigger(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
action: AutomationActionType, action: TriggerActionType,
automation_info: AutomationTriggerInfo, trigger_info: TriggerInfo,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration.""" """Listen for state changes based on configuration."""
trigger_type = config[CONF_TYPE] trigger_type = config[CONF_TYPE]
@ -283,7 +280,7 @@ async def async_attach_trigger(
state_config = await state_trigger.async_validate_trigger_config(hass, state_config) state_config = await state_trigger.async_validate_trigger_config(hass, state_config)
return await state_trigger.async_attach_trigger( return await state_trigger.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device" hass, state_config, action, trigger_info, platform_type="device"
) )

View File

@ -5,7 +5,7 @@ import asyncio
from collections.abc import Callable from collections.abc import Callable
import functools import functools
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any, Protocol, TypedDict
import voluptuous as vol import voluptuous as vol
@ -27,6 +27,34 @@ _PLATFORM_ALIASES = {
} }
class TriggerActionType(Protocol):
"""Protocol type for trigger action callback."""
async def __call__(
self,
run_variables: dict[str, Any],
context: Context | None = None,
) -> None:
"""Define action callback type."""
class TriggerData(TypedDict):
"""Trigger data."""
id: str
idx: str
class TriggerInfo(TypedDict):
"""Information about trigger."""
domain: str
name: str
home_assistant_start: bool
variables: TemplateVarsType
trigger_data: TriggerData
async def _async_get_trigger_platform( async def _async_get_trigger_platform(
hass: HomeAssistant, config: ConfigType hass: HomeAssistant, config: ConfigType
) -> DeviceAutomationTriggerProtocol: ) -> DeviceAutomationTriggerProtocol:
@ -93,11 +121,6 @@ async def async_initialize_triggers(
variables: TemplateVarsType = None, variables: TemplateVarsType = None,
) -> CALLBACK_TYPE | None: ) -> CALLBACK_TYPE | None:
"""Initialize triggers.""" """Initialize triggers."""
from homeassistant.components.automation import ( # pylint:disable=[import-outside-toplevel]
AutomationTriggerData,
AutomationTriggerInfo,
)
triggers = [] triggers = []
for idx, conf in enumerate(trigger_config): for idx, conf in enumerate(trigger_config):
# Skip triggers that are not enabled # Skip triggers that are not enabled
@ -107,8 +130,8 @@ async def async_initialize_triggers(
platform = await _async_get_trigger_platform(hass, conf) platform = await _async_get_trigger_platform(hass, conf)
trigger_id = conf.get(CONF_ID, f"{idx}") trigger_id = conf.get(CONF_ID, f"{idx}")
trigger_idx = f"{idx}" trigger_idx = f"{idx}"
trigger_data = AutomationTriggerData(id=trigger_id, idx=trigger_idx) trigger_data = TriggerData(id=trigger_id, idx=trigger_idx)
info = AutomationTriggerInfo( info = TriggerInfo(
domain=domain, domain=domain,
name=name, name=name,
home_assistant_start=home_assistant_start, home_assistant_start=home_assistant_start,

View File

@ -337,8 +337,8 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = {
arg_types={ arg_types={
0: "HomeAssistant", 0: "HomeAssistant",
1: "ConfigType", 1: "ConfigType",
2: "AutomationActionType", # 2: "AutomationActionType", # AutomationActionType is deprecated -> TriggerActionType
3: "AutomationTriggerInfo", # 3: "AutomationTriggerInfo", # AutomationTriggerInfo is deprecated -> TriggerInfo
}, },
return_type="CALLBACK_TYPE", return_type="CALLBACK_TYPE",
), ),