diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index c3a669511bc..43ff31dfab8 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Callable import logging -from typing import Any, Protocol, TypedDict, cast +from typing import Any, cast import voluptuous as vol from voluptuous.humanize import humanize_error @@ -72,8 +72,13 @@ from homeassistant.helpers.trace import ( trace_get, trace_path, ) -from homeassistant.helpers.trigger import async_initialize_triggers -from homeassistant.helpers.typing import ConfigType, TemplateVarsType +from homeassistant.helpers.trigger import ( + TriggerActionType, + TriggerData, + TriggerInfo, + async_initialize_triggers, +) +from homeassistant.helpers.typing import ConfigType from homeassistant.loader import bind_hass from homeassistant.util.dt import parse_datetime @@ -112,32 +117,11 @@ SERVICE_TRIGGER = "trigger" _LOGGER = logging.getLogger(__name__) -class AutomationActionType(Protocol): - """Protocol type for automation action callback.""" - - async def __call__( - self, - 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 +# AutomationActionType, AutomationTriggerData, +# and AutomationTriggerInfo are deprecated as of 2022.9. +AutomationActionType = TriggerActionType +AutomationTriggerData = TriggerData +AutomationTriggerInfo = TriggerInfo @bind_hass diff --git a/homeassistant/components/binary_sensor/device_trigger.py b/homeassistant/components/binary_sensor/device_trigger.py index 12b620b8c4f..a6dfc762d45 100644 --- a/homeassistant/components/binary_sensor/device_trigger.py +++ b/homeassistant/components/binary_sensor/device_trigger.py @@ -1,10 +1,6 @@ """Provides device triggers for binary sensors.""" 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.const import ( 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.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers.entity import get_device_class +from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.typing import ConfigType from . import DOMAIN, BinarySensorDeviceClass @@ -263,8 +260,8 @@ TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, - action: AutomationActionType, - automation_info: AutomationTriggerInfo, + action: TriggerActionType, + trigger_info: TriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" 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) 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" ) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index e90c684365d..a48ccbbb7b9 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -5,7 +5,7 @@ import asyncio from collections.abc import Callable import functools import logging -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Protocol, TypedDict 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( hass: HomeAssistant, config: ConfigType ) -> DeviceAutomationTriggerProtocol: @@ -93,11 +121,6 @@ async def async_initialize_triggers( variables: TemplateVarsType = None, ) -> CALLBACK_TYPE | None: """Initialize triggers.""" - from homeassistant.components.automation import ( # pylint:disable=[import-outside-toplevel] - AutomationTriggerData, - AutomationTriggerInfo, - ) - triggers = [] for idx, conf in enumerate(trigger_config): # Skip triggers that are not enabled @@ -107,8 +130,8 @@ async def async_initialize_triggers( platform = await _async_get_trigger_platform(hass, conf) trigger_id = conf.get(CONF_ID, f"{idx}") trigger_idx = f"{idx}" - trigger_data = AutomationTriggerData(id=trigger_id, idx=trigger_idx) - info = AutomationTriggerInfo( + trigger_data = TriggerData(id=trigger_id, idx=trigger_idx) + info = TriggerInfo( domain=domain, name=name, home_assistant_start=home_assistant_start, diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index b6eecdadfee..158848ba4d4 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -337,8 +337,8 @@ _FUNCTION_MATCH: dict[str, list[TypeHintMatch]] = { arg_types={ 0: "HomeAssistant", 1: "ConfigType", - 2: "AutomationActionType", - 3: "AutomationTriggerInfo", + # 2: "AutomationActionType", # AutomationActionType is deprecated -> TriggerActionType + # 3: "AutomationTriggerInfo", # AutomationTriggerInfo is deprecated -> TriggerInfo }, return_type="CALLBACK_TYPE", ),