From 6d9411b8a19f9a5f049329c9288c065eab42700e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 22 Feb 2023 11:59:53 +0100 Subject: [PATCH] Improve trigger platform typing (#88511) * Improve trigger platform typing * Tweak docstring * Revert "Tweak docstring" This reverts commit c31f790fc3c1e66a5d802f759f07dfe4049cf529. * Tweak docstring --- .../components/device_automation/trigger.py | 27 +++++----------- homeassistant/helpers/trigger.py | 32 +++++++++++++++---- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/device_automation/trigger.py b/homeassistant/components/device_automation/trigger.py index 80e96ddaba9..6bbbd6febce 100644 --- a/homeassistant/components/device_automation/trigger.py +++ b/homeassistant/components/device_automation/trigger.py @@ -7,7 +7,11 @@ import voluptuous as vol from homeassistant.const import CONF_DOMAIN from homeassistant.core import CALLBACK_TYPE, HomeAssistant -from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo +from homeassistant.helpers.trigger import ( + TriggerActionType, + TriggerInfo, + TriggerProtocol, +) from homeassistant.helpers.typing import ConfigType from . import ( @@ -20,28 +24,13 @@ from .helpers import async_validate_device_automation_config TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA) -class DeviceAutomationTriggerProtocol(Protocol): +class DeviceAutomationTriggerProtocol(TriggerProtocol, Protocol): """Define the format of device_trigger modules. - Each module must define either TRIGGER_SCHEMA or async_validate_trigger_config. + Each module must define either TRIGGER_SCHEMA or async_validate_trigger_config + from TriggerProtocol. """ - TRIGGER_SCHEMA: vol.Schema - - async def async_validate_trigger_config( - self, hass: HomeAssistant, config: ConfigType - ) -> ConfigType: - """Validate config.""" - - async def async_attach_trigger( - self, - hass: HomeAssistant, - config: ConfigType, - action: TriggerActionType, - trigger_info: TriggerInfo, - ) -> CALLBACK_TYPE: - """Attach a trigger.""" - async def async_get_trigger_capabilities( self, hass: HomeAssistant, config: ConfigType ) -> dict[str, vol.Schema]: diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 52c5ab0e15e..314c0e8939e 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -7,7 +7,7 @@ from collections.abc import Callable, Coroutine from dataclasses import dataclass, field import functools import logging -from typing import TYPE_CHECKING, Any, Protocol, TypedDict, cast +from typing import Any, Protocol, TypedDict, cast import voluptuous as vol @@ -31,11 +31,6 @@ from homeassistant.loader import IntegrationNotFound, async_get_integration from .typing import ConfigType, TemplateVarsType -if TYPE_CHECKING: - from homeassistant.components.device_automation.trigger import ( - DeviceAutomationTriggerProtocol, - ) - _PLATFORM_ALIASES = { "device": "device_automation", "event": "homeassistant", @@ -48,6 +43,29 @@ _PLATFORM_ALIASES = { DATA_PLUGGABLE_ACTIONS = "pluggable_actions" +class TriggerProtocol(Protocol): + """Define the format of trigger modules. + + Each module must define either TRIGGER_SCHEMA or async_validate_trigger_config. + """ + + TRIGGER_SCHEMA: vol.Schema + + async def async_validate_trigger_config( + self, hass: HomeAssistant, config: ConfigType + ) -> ConfigType: + """Validate config.""" + + async def async_attach_trigger( + self, + hass: HomeAssistant, + config: ConfigType, + action: TriggerActionType, + trigger_info: TriggerInfo, + ) -> CALLBACK_TYPE: + """Attach a trigger.""" + + class TriggerActionType(Protocol): """Protocol type for trigger action callback.""" @@ -195,7 +213,7 @@ class PluggableAction: async def _async_get_trigger_platform( hass: HomeAssistant, config: ConfigType -) -> DeviceAutomationTriggerProtocol: +) -> TriggerProtocol: platform_and_sub_type = config[CONF_PLATFORM].split(".") platform = platform_and_sub_type[0] platform = _PLATFORM_ALIASES.get(platform, platform)