diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index c743e1f83fd..c3a669511bc 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -1,9 +1,9 @@ """Allow to set up simple automation rules via the config file.""" from __future__ import annotations -from collections.abc import Awaitable, Callable +from collections.abc import Callable import logging -from typing import Any, TypedDict, cast +from typing import Any, Protocol, TypedDict, cast import voluptuous as vol from voluptuous.humanize import humanize_error @@ -110,7 +110,17 @@ ATTR_VARIABLES = "variables" SERVICE_TRIGGER = "trigger" _LOGGER = logging.getLogger(__name__) -AutomationActionType = Callable[[HomeAssistant, TemplateVarsType], Awaitable[None]] + + +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): @@ -437,7 +447,12 @@ class AutomationEntity(ToggleEntity, RestoreEntity): else: await self.async_disable() - async def async_trigger(self, run_variables, context=None, skip_condition=False): + async def async_trigger( + self, + run_variables: dict[str, Any], + context: Context | None = None, + skip_condition: bool = False, + ) -> None: """Trigger automation. This method is a coroutine. @@ -462,7 +477,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity): this = None if state := self.hass.states.get(self.entity_id): this = state.as_dict() - variables = {"this": this, **(run_variables or {})} + variables: dict[str, Any] = {"this": this, **(run_variables or {})} if self._variables: try: variables = self._variables.async_render(self.hass, variables) diff --git a/homeassistant/components/calendar/trigger.py b/homeassistant/components/calendar/trigger.py index bb6e874b47f..7845037f896 100644 --- a/homeassistant/components/calendar/trigger.py +++ b/homeassistant/components/calendar/trigger.py @@ -1,6 +1,7 @@ """Offer calendar automation rules.""" from __future__ import annotations +from collections.abc import Coroutine import datetime import logging from typing import Any @@ -47,7 +48,7 @@ class CalendarEventListener: def __init__( self, hass: HomeAssistant, - job: HassJob, + job: HassJob[..., Coroutine[Any, Any, None]], trigger_data: dict[str, Any], entity: CalendarEntity, event_type: str, diff --git a/homeassistant/components/homekit/type_triggers.py b/homeassistant/components/homekit/type_triggers.py index 4b3a7e73cac..776fe6f3110 100644 --- a/homeassistant/components/homekit/type_triggers.py +++ b/homeassistant/components/homekit/type_triggers.py @@ -66,7 +66,7 @@ class DeviceTriggerAccessory(HomeAccessory): async def async_trigger( self, - run_variables: dict, + run_variables: dict[str, Any], context: Context | None = None, skip_condition: bool = False, ) -> None: diff --git a/homeassistant/components/philips_js/__init__.py b/homeassistant/components/philips_js/__init__.py index 24b3f9a91e0..154df3ed214 100644 --- a/homeassistant/components/philips_js/__init__.py +++ b/homeassistant/components/philips_js/__init__.py @@ -2,7 +2,7 @@ from __future__ import annotations import asyncio -from collections.abc import Callable, Mapping +from collections.abc import Callable, Coroutine, Mapping from datetime import timedelta import logging from typing import Any @@ -84,7 +84,9 @@ class PluggableAction: def __init__(self, update: Callable[[], None]) -> None: """Initialize.""" self._update = update - self._actions: dict[Any, tuple[HassJob, dict[str, Any]]] = {} + self._actions: dict[ + Any, tuple[HassJob[..., Coroutine[Any, Any, None]], dict[str, Any]] + ] = {} def __bool__(self): """Return if we have something attached.""" diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index d9b2acb1836..32161e6bad6 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -1,7 +1,7 @@ """Support for LG webOS Smart TV.""" from __future__ import annotations -from collections.abc import Callable +from collections.abc import Callable, Coroutine from contextlib import suppress import logging from typing import Any @@ -170,7 +170,10 @@ class PluggableAction: def __init__(self) -> None: """Initialize.""" - self._actions: dict[Callable[[], None], tuple[HassJob, dict[str, Any]]] = {} + self._actions: dict[ + Callable[[], None], + tuple[HassJob[..., Coroutine[Any, Any, None]], dict[str, Any]], + ] = {} def __bool__(self) -> bool: """Return if we have something attached."""