From b10fc89a6b212474f7c3fac807f436b55fe27d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 4 Sep 2021 03:25:51 +0300 Subject: [PATCH] Automation trigger info type hint improvements (#55402) * Make automation trigger info a TypedDict * zwave_js trigger type hint fixes * Remove redundant automation trigger info field presence checks * Use async_initialize_triggers in mqtt and tasmota device_trigger tests --- .../alarm_control_panel/device_trigger.py | 7 +- .../components/arcam_fmj/device_trigger.py | 9 +- .../components/automation/__init__.py | 19 ++- .../components/climate/device_trigger.py | 7 +- .../components/cover/device_trigger.py | 7 +- .../device_automation/toggle_entity.py | 7 +- .../device_tracker/device_trigger.py | 7 +- .../components/fan/device_trigger.py | 7 +- .../components/geo_location/trigger.py | 2 +- .../homeassistant/triggers/event.py | 13 +- .../homeassistant/triggers/homeassistant.py | 2 +- .../homeassistant/triggers/numeric_state.py | 6 +- .../homeassistant/triggers/state.py | 6 +- .../components/homeassistant/triggers/time.py | 2 +- .../homeassistant/triggers/time_pattern.py | 2 +- .../homekit_controller/device_trigger.py | 13 +- .../components/humidifier/device_trigger.py | 7 +- .../components/kodi/device_trigger.py | 11 +- .../components/light/device_trigger.py | 7 +- homeassistant/components/litejet/trigger.py | 2 +- .../components/lock/device_trigger.py | 7 +- .../lutron_caseta/device_trigger.py | 7 +- .../components/media_player/device_trigger.py | 7 +- .../components/mqtt/device_trigger.py | 9 +- homeassistant/components/mqtt/trigger.py | 2 +- .../components/nest/device_trigger.py | 7 +- .../components/netatmo/device_trigger.py | 7 +- .../components/philips_js/device_trigger.py | 9 +- .../components/remote/device_trigger.py | 7 +- .../components/select/device_trigger.py | 7 +- .../components/shelly/device_trigger.py | 7 +- homeassistant/components/sun/trigger.py | 2 +- .../components/switch/device_trigger.py | 7 +- homeassistant/components/tag/trigger.py | 9 +- .../components/tasmota/device_trigger.py | 11 +- homeassistant/components/template/trigger.py | 2 +- .../components/vacuum/device_trigger.py | 7 +- homeassistant/components/webhook/trigger.py | 2 +- homeassistant/components/zone/trigger.py | 2 +- .../components/zwave_js/device_trigger.py | 7 +- homeassistant/components/zwave_js/trigger.py | 16 +- .../zwave_js/triggers/value_updated.py | 13 +- homeassistant/helpers/trigger.py | 19 +-- .../integration/device_trigger.py | 7 +- tests/components/mqtt/test_device_trigger.py | 76 +++++---- .../components/tasmota/test_device_trigger.py | 155 ++++++++++-------- 46 files changed, 344 insertions(+), 210 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/device_trigger.py b/homeassistant/components/alarm_control_panel/device_trigger.py index 695ec0ebb4a..92c73b07bbd 100644 --- a/homeassistant/components/alarm_control_panel/device_trigger.py +++ b/homeassistant/components/alarm_control_panel/device_trigger.py @@ -11,7 +11,10 @@ from homeassistant.components.alarm_control_panel.const import ( SUPPORT_ALARM_ARM_NIGHT, SUPPORT_ALARM_ARM_VACATION, ) -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.const import ( @@ -129,7 +132,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "triggered": diff --git a/homeassistant/components/arcam_fmj/device_trigger.py b/homeassistant/components/arcam_fmj/device_trigger.py index 7bf7a06d851..ed9308a89c6 100644 --- a/homeassistant/components/arcam_fmj/device_trigger.py +++ b/homeassistant/components/arcam_fmj/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.const import ( ATTR_ENTITY_ID, @@ -57,10 +60,10 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] job = HassJob(action) if config[CONF_TYPE] == "turn_on": diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 5e1b53c535e..24090b79fa8 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -2,7 +2,7 @@ from __future__ import annotations import logging -from typing import Any, Awaitable, Callable, Dict, cast +from typing import Any, Awaitable, Callable, Dict, TypedDict, cast import voluptuous as vol from voluptuous.humanize import humanize_error @@ -106,6 +106,23 @@ _LOGGER = logging.getLogger(__name__) AutomationActionType = Callable[[HomeAssistant, TemplateVarsType], Awaitable[None]] +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 def is_on(hass, entity_id): """ diff --git a/homeassistant/components/climate/device_trigger.py b/homeassistant/components/climate/device_trigger.py index 4ff2e8fe477..ce4e08f9fd2 100644 --- a/homeassistant/components/climate/device_trigger.py +++ b/homeassistant/components/climate/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import ( numeric_state as numeric_state_trigger, @@ -112,7 +115,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" trigger_type = config[CONF_TYPE] diff --git a/homeassistant/components/cover/device_trigger.py b/homeassistant/components/cover/device_trigger.py index e7048032cba..f4a2f4443d1 100644 --- a/homeassistant/components/cover/device_trigger.py +++ b/homeassistant/components/cover/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import ( numeric_state as numeric_state_trigger, @@ -147,7 +150,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] in STATE_TRIGGER_TYPES: diff --git a/homeassistant/components/device_automation/toggle_entity.py b/homeassistant/components/device_automation/toggle_entity.py index 2e9576ee74a..5d08f8d9d31 100644 --- a/homeassistant/components/device_automation/toggle_entity.py +++ b/homeassistant/components/device_automation/toggle_entity.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation.const import ( CONF_IS_OFF, CONF_IS_ON, @@ -146,7 +149,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" trigger_type = config[CONF_TYPE] diff --git a/homeassistant/components/device_tracker/device_trigger.py b/homeassistant/components/device_tracker/device_trigger.py index 0b8fd6da7f4..49a52fa887e 100644 --- a/homeassistant/components/device_tracker/device_trigger.py +++ b/homeassistant/components/device_tracker/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any, Final import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.zone import DOMAIN as DOMAIN_ZONE, trigger as zone from homeassistant.const import ( @@ -72,7 +75,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "enters": diff --git a/homeassistant/components/fan/device_trigger.py b/homeassistant/components/fan/device_trigger.py index 38cfb33b42d..503aaaac52a 100644 --- a/homeassistant/components/fan/device_trigger.py +++ b/homeassistant/components/fan/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import toggle_entity from homeassistant.const import CONF_DOMAIN from homeassistant.core import CALLBACK_TYPE, HomeAssistant @@ -36,7 +39,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" return await toggle_entity.async_attach_trigger( diff --git a/homeassistant/components/geo_location/trigger.py b/homeassistant/components/geo_location/trigger.py index c5e35ece593..b77aecee14c 100644 --- a/homeassistant/components/geo_location/trigger.py +++ b/homeassistant/components/geo_location/trigger.py @@ -37,7 +37,7 @@ def source_match(state, source): async def async_attach_trigger(hass, config, action, automation_info): """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] source = config.get(CONF_SOURCE).lower() zone_entity_id = config.get(CONF_ZONE) trigger_event = config.get(CONF_EVENT) diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index 47dc5317bbd..b0d817478dc 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.const import CONF_EVENT_DATA, CONF_PLATFORM from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback from homeassistant.helpers import config_validation as cv, template @@ -35,15 +38,13 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict[str, Any], + automation_info: AutomationTriggerInfo, *, platform_type: str = "event", ) -> CALLBACK_TYPE: """Listen for events based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} - variables = None - if automation_info: - variables = automation_info.get("variables") + trigger_data = automation_info["trigger_data"] + variables = automation_info["variables"] template.attach(hass, config[CONF_EVENT_TYPE]) event_types = template.render_complex( diff --git a/homeassistant/components/homeassistant/triggers/homeassistant.py b/homeassistant/components/homeassistant/triggers/homeassistant.py index ea1a985139f..6f2ec75e313 100644 --- a/homeassistant/components/homeassistant/triggers/homeassistant.py +++ b/homeassistant/components/homeassistant/triggers/homeassistant.py @@ -20,7 +20,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger(hass, config, action, automation_info): """Listen for events based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] event = config.get(CONF_EVENT) job = HassJob(action) diff --git a/homeassistant/components/homeassistant/triggers/numeric_state.py b/homeassistant/components/homeassistant/triggers/numeric_state.py index f315addb272..3f280f581b3 100644 --- a/homeassistant/components/homeassistant/triggers/numeric_state.py +++ b/homeassistant/components/homeassistant/triggers/numeric_state.py @@ -78,10 +78,8 @@ async def async_attach_trigger( attribute = config.get(CONF_ATTRIBUTE) job = HassJob(action) - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} - _variables: dict = {} - if automation_info: - _variables = automation_info.get("variables") or {} + trigger_data = automation_info["trigger_data"] + _variables = automation_info["variables"] or {} if value_template is not None: value_template.hass = hass diff --git a/homeassistant/components/homeassistant/triggers/state.py b/homeassistant/components/homeassistant/triggers/state.py index 12c42a95978..f60071d633c 100644 --- a/homeassistant/components/homeassistant/triggers/state.py +++ b/homeassistant/components/homeassistant/triggers/state.py @@ -87,10 +87,8 @@ async def async_attach_trigger( attribute = config.get(CONF_ATTRIBUTE) job = HassJob(action) - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} - _variables: dict = {} - if automation_info: - _variables = automation_info.get("variables") or {} + trigger_data = automation_info["trigger_data"] + _variables = automation_info["variables"] or {} @callback def state_automation_listener(event: Event): diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index f661ae21a5b..6ca1998a5c3 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -39,7 +39,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger(hass, config, action, automation_info): """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] entities = {} removes = [] job = HassJob(action) diff --git a/homeassistant/components/homeassistant/triggers/time_pattern.py b/homeassistant/components/homeassistant/triggers/time_pattern.py index 0380e01c239..000d73b6cd1 100644 --- a/homeassistant/components/homeassistant/triggers/time_pattern.py +++ b/homeassistant/components/homeassistant/triggers/time_pattern.py @@ -57,7 +57,7 @@ TRIGGER_SCHEMA = vol.All( async def async_attach_trigger(hass, config, action, automation_info): """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) diff --git a/homeassistant/components/homekit_controller/device_trigger.py b/homeassistant/components/homekit_controller/device_trigger.py index 1972aadfeca..5bb7d634626 100644 --- a/homeassistant/components/homekit_controller/device_trigger.py +++ b/homeassistant/components/homekit_controller/device_trigger.py @@ -9,7 +9,10 @@ from aiohomekit.model.services import ServicesTypes from aiohomekit.utils import clamp_enum_to_char import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback @@ -75,12 +78,10 @@ class TriggerSource: self, config: TRIGGER_SCHEMA, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" - trigger_data = ( - automation_info.get("trigger_data", {}) if automation_info else {} - ) + trigger_data = automation_info["trigger_data"] def event_handler(char): if config[CONF_SUBTYPE] != HK_TO_HA_INPUT_EVENT_VALUES[char["value"]]: @@ -260,7 +261,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" device_id = config[CONF_DEVICE_ID] diff --git a/homeassistant/components/humidifier/device_trigger.py b/homeassistant/components/humidifier/device_trigger.py index 5c761e798ea..a049af9afec 100644 --- a/homeassistant/components/humidifier/device_trigger.py +++ b/homeassistant/components/humidifier/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import ( DEVICE_TRIGGER_BASE_SCHEMA, toggle_entity, @@ -80,7 +83,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" trigger_type = config[CONF_TYPE] diff --git a/homeassistant/components/kodi/device_trigger.py b/homeassistant/components/kodi/device_trigger.py index ac474413b54..68735bfa386 100644 --- a/homeassistant/components/kodi/device_trigger.py +++ b/homeassistant/components/kodi/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.const import ( ATTR_ENTITY_ID, @@ -69,9 +72,9 @@ def _attach_trigger( config: ConfigType, action: AutomationActionType, event_type, - automation_info: dict, + automation_info: AutomationTriggerInfo, ): - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] job = HassJob(action) @callback @@ -90,7 +93,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "turn_on": diff --git a/homeassistant/components/light/device_trigger.py b/homeassistant/components/light/device_trigger.py index 6cb6e8a34c1..6714ee4cf9c 100644 --- a/homeassistant/components/light/device_trigger.py +++ b/homeassistant/components/light/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import toggle_entity from homeassistant.const import CONF_DOMAIN from homeassistant.core import CALLBACK_TYPE, HomeAssistant @@ -22,7 +25,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" return await toggle_entity.async_attach_trigger( diff --git a/homeassistant/components/litejet/trigger.py b/homeassistant/components/litejet/trigger.py index 3a9930c5e70..5ff841a55c3 100644 --- a/homeassistant/components/litejet/trigger.py +++ b/homeassistant/components/litejet/trigger.py @@ -31,7 +31,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger(hass, config, action, automation_info): """Listen for events based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] number = config.get(CONF_NUMBER) held_more_than = config.get(CONF_HELD_MORE_THAN) held_less_than = config.get(CONF_HELD_LESS_THAN) diff --git a/homeassistant/components/lock/device_trigger.py b/homeassistant/components/lock/device_trigger.py index 393fd968437..cbdab7abb3d 100644 --- a/homeassistant/components/lock/device_trigger.py +++ b/homeassistant/components/lock/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.const import ( @@ -80,7 +83,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "jammed": diff --git a/homeassistant/components/lutron_caseta/device_trigger.py b/homeassistant/components/lutron_caseta/device_trigger.py index 8a7f321e158..4e378942bd8 100644 --- a/homeassistant/components/lutron_caseta/device_trigger.py +++ b/homeassistant/components/lutron_caseta/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -258,7 +261,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" device = get_button_device_by_dr_id(hass, config[CONF_DEVICE_ID]) diff --git a/homeassistant/components/media_player/device_trigger.py b/homeassistant/components/media_player/device_trigger.py index 532519616d2..9aa75ab935c 100644 --- a/homeassistant/components/media_player/device_trigger.py +++ b/homeassistant/components/media_player/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.const import ( @@ -80,7 +83,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "turned_on": diff --git a/homeassistant/components/mqtt/device_trigger.py b/homeassistant/components/mqtt/device_trigger.py index b4b586e14d2..6348156ef50 100644 --- a/homeassistant/components/mqtt/device_trigger.py +++ b/homeassistant/components/mqtt/device_trigger.py @@ -7,7 +7,10 @@ from typing import Any, Callable import attr import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.const import ( CONF_DEVICE, @@ -86,7 +89,7 @@ class TriggerInstance: """Attached trigger settings.""" action: AutomationActionType = attr.ib() - automation_info: dict = attr.ib() + automation_info: AutomationTriggerInfo = attr.ib() trigger: Trigger = attr.ib() remove: CALLBACK_TYPE | None = attr.ib(default=None) @@ -316,7 +319,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if DEVICE_TRIGGERS not in hass.data: diff --git a/homeassistant/components/mqtt/trigger.py b/homeassistant/components/mqtt/trigger.py index 3ee23356c3f..6be19a1b43a 100644 --- a/homeassistant/components/mqtt/trigger.py +++ b/homeassistant/components/mqtt/trigger.py @@ -37,7 +37,7 @@ _LOGGER = logging.getLogger(__name__) async def async_attach_trigger(hass, config, action, automation_info): """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] topic = config[CONF_TOPIC] wanted_payload = config.get(CONF_PAYLOAD) value_template = config.get(CONF_VALUE_TEMPLATE) diff --git a/homeassistant/components/nest/device_trigger.py b/homeassistant/components/nest/device_trigger.py index 619f6a3fe56..bcd5b6b96b3 100644 --- a/homeassistant/components/nest/device_trigger.py +++ b/homeassistant/components/nest/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -87,7 +90,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" event_config = event_trigger.TRIGGER_SCHEMA( diff --git a/homeassistant/components/netatmo/device_trigger.py b/homeassistant/components/netatmo/device_trigger.py index 777b905f5d7..a228e7632a5 100644 --- a/homeassistant/components/netatmo/device_trigger.py +++ b/homeassistant/components/netatmo/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -128,7 +131,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" device_registry = await hass.helpers.device_registry.async_get_registry() diff --git a/homeassistant/components/philips_js/device_trigger.py b/homeassistant/components/philips_js/device_trigger.py index 85b1a012860..09784dae63f 100644 --- a/homeassistant/components/philips_js/device_trigger.py +++ b/homeassistant/components/philips_js/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE from homeassistant.core import CALLBACK_TYPE, HomeAssistant @@ -46,10 +49,10 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE | None: """Attach a trigger.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] registry: DeviceRegistry = await async_get_registry(hass) if config[CONF_TYPE] == TRIGGER_TYPE_TURN_ON: variables = { diff --git a/homeassistant/components/remote/device_trigger.py b/homeassistant/components/remote/device_trigger.py index 40182cc0114..cf3a7427745 100644 --- a/homeassistant/components/remote/device_trigger.py +++ b/homeassistant/components/remote/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import toggle_entity from homeassistant.const import CONF_DOMAIN from homeassistant.core import CALLBACK_TYPE, HomeAssistant @@ -22,7 +25,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" return await toggle_entity.async_attach_trigger( diff --git a/homeassistant/components/select/device_trigger.py b/homeassistant/components/select/device_trigger.py index ded3ff4bc24..6dabacf34e5 100644 --- a/homeassistant/components/select/device_trigger.py +++ b/homeassistant/components/select/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers.state import ( CONF_FOR, @@ -64,7 +67,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" state_config = { diff --git a/homeassistant/components/shelly/device_trigger.py b/homeassistant/components/shelly/device_trigger.py index c44dd279230..eae2953e5b8 100644 --- a/homeassistant/components/shelly/device_trigger.py +++ b/homeassistant/components/shelly/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any, Final import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -111,7 +114,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" event_config = { diff --git a/homeassistant/components/sun/trigger.py b/homeassistant/components/sun/trigger.py index b612934bfad..266df1f6a3b 100644 --- a/homeassistant/components/sun/trigger.py +++ b/homeassistant/components/sun/trigger.py @@ -26,7 +26,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger(hass, config, action, automation_info): """Listen for events based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) description = event diff --git a/homeassistant/components/switch/device_trigger.py b/homeassistant/components/switch/device_trigger.py index b796a31134f..6e4cf2f810e 100644 --- a/homeassistant/components/switch/device_trigger.py +++ b/homeassistant/components/switch/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import toggle_entity from homeassistant.const import CONF_DOMAIN from homeassistant.core import CALLBACK_TYPE, HomeAssistant @@ -22,7 +25,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" return await toggle_entity.async_attach_trigger( diff --git a/homeassistant/components/tag/trigger.py b/homeassistant/components/tag/trigger.py index ba90f0a9396..b844ee260a2 100644 --- a/homeassistant/components/tag/trigger.py +++ b/homeassistant/components/tag/trigger.py @@ -1,7 +1,10 @@ """Support for tag triggers.""" import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.const import CONF_PLATFORM from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant from homeassistant.helpers import config_validation as cv @@ -22,10 +25,10 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Listen for tag_scanned events based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] tag_ids = set(config[TAG_ID]) device_ids = set(config[DEVICE_ID]) if DEVICE_ID in config else None diff --git a/homeassistant/components/tasmota/device_trigger.py b/homeassistant/components/tasmota/device_trigger.py index b3be1fbd2cc..27bcc4228ea 100644 --- a/homeassistant/components/tasmota/device_trigger.py +++ b/homeassistant/components/tasmota/device_trigger.py @@ -9,7 +9,10 @@ from hatasmota.models import DiscoveryHashType from hatasmota.trigger import TasmotaTrigger, TasmotaTriggerConfig import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import event as event_trigger from homeassistant.config_entries import ConfigEntry @@ -49,7 +52,7 @@ class TriggerInstance: """Attached trigger settings.""" action: AutomationActionType = attr.ib() - automation_info: dict = attr.ib() + automation_info: AutomationTriggerInfo = attr.ib() trigger: Trigger = attr.ib() remove: CALLBACK_TYPE | None = attr.ib(default=None) @@ -93,7 +96,7 @@ class Trigger: trigger_instances: list[TriggerInstance] = attr.ib(factory=list) async def add_trigger( - self, action: AutomationActionType, automation_info: dict + self, action: AutomationActionType, automation_info: AutomationTriggerInfo ) -> Callable[[], None]: """Add Tasmota trigger.""" instance = TriggerInstance(action, automation_info, self) @@ -289,7 +292,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: Callable, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a device trigger.""" if DEVICE_TRIGGERS not in hass.data: diff --git a/homeassistant/components/template/trigger.py b/homeassistant/components/template/trigger.py index 6db25da76ab..d2e50de53fd 100644 --- a/homeassistant/components/template/trigger.py +++ b/homeassistant/components/template/trigger.py @@ -31,7 +31,7 @@ async def async_attach_trigger( hass, config, action, automation_info, *, platform_type="template" ): """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] value_template = config.get(CONF_VALUE_TEMPLATE) value_template.hass = hass time_delta = config.get(CONF_FOR) diff --git a/homeassistant/components/vacuum/device_trigger.py b/homeassistant/components/vacuum/device_trigger.py index 9189568d2f4..f4fdbcf972e 100644 --- a/homeassistant/components/vacuum/device_trigger.py +++ b/homeassistant/components/vacuum/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.const import ( @@ -74,7 +77,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" if config[CONF_TYPE] == "cleaning": diff --git a/homeassistant/components/webhook/trigger.py b/homeassistant/components/webhook/trigger.py index 6bb8a61eeec..4e17c5e9e34 100644 --- a/homeassistant/components/webhook/trigger.py +++ b/homeassistant/components/webhook/trigger.py @@ -37,7 +37,7 @@ async def _handle_webhook(job, trigger_data, hass, webhook_id, request): async def async_attach_trigger(hass, config, action, automation_info): """Trigger based on incoming webhooks.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] webhook_id = config.get(CONF_WEBHOOK_ID) job = HassJob(action) hass.components.webhook.async_register( diff --git a/homeassistant/components/zone/trigger.py b/homeassistant/components/zone/trigger.py index eb084fe1874..ef054b39714 100644 --- a/homeassistant/components/zone/trigger.py +++ b/homeassistant/components/zone/trigger.py @@ -37,7 +37,7 @@ async def async_attach_trigger( hass, config, action, automation_info, *, platform_type: str = "zone" ) -> CALLBACK_TYPE: """Listen for state changes based on configuration.""" - trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} + trigger_data = automation_info["trigger_data"] entity_id = config.get(CONF_ENTITY_ID) zone_entity_id = config.get(CONF_ZONE) event = config.get(CONF_EVENT) diff --git a/homeassistant/components/zwave_js/device_trigger.py b/homeassistant/components/zwave_js/device_trigger.py index 7ed13ce2b98..d3deba0979a 100644 --- a/homeassistant/components/zwave_js/device_trigger.py +++ b/homeassistant/components/zwave_js/device_trigger.py @@ -6,7 +6,10 @@ from typing import Any import voluptuous as vol from zwave_js_server.const import CommandClass -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation.exceptions import ( InvalidDeviceAutomationConfig, @@ -358,7 +361,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" trigger_type = config[CONF_TYPE] diff --git a/homeassistant/components/zwave_js/trigger.py b/homeassistant/components/zwave_js/trigger.py index 69e770e3817..ca9bd7d24a2 100644 --- a/homeassistant/components/zwave_js/trigger.py +++ b/homeassistant/components/zwave_js/trigger.py @@ -2,10 +2,14 @@ from __future__ import annotations from types import ModuleType -from typing import Any, Callable, cast +from typing import cast +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.const import CONF_PLATFORM -from homeassistant.core import HomeAssistant +from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.helpers.typing import ConfigType from .triggers import value_updated @@ -40,14 +44,14 @@ async def async_validate_trigger_config( async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, - action: Callable, - automation_info: dict[str, Any], -) -> Callable: + action: AutomationActionType, + automation_info: AutomationTriggerInfo, +) -> CALLBACK_TYPE: """Attach trigger of specified platform.""" platform = _get_trigger_platform(config) assert hasattr(platform, "async_attach_trigger") return cast( - Callable, + CALLBACK_TYPE, await getattr(platform, "async_attach_trigger")( hass, config, action, automation_info ), diff --git a/homeassistant/components/zwave_js/triggers/value_updated.py b/homeassistant/components/zwave_js/triggers/value_updated.py index a2dbb84cf3b..fdf2589073e 100644 --- a/homeassistant/components/zwave_js/triggers/value_updated.py +++ b/homeassistant/components/zwave_js/triggers/value_updated.py @@ -3,7 +3,6 @@ from __future__ import annotations import functools import logging -from typing import Any, Callable import voluptuous as vol from zwave_js_server.const import CommandClass @@ -11,6 +10,10 @@ from zwave_js_server.event import Event from zwave_js_server.model.node import Node from zwave_js_server.model.value import Value, get_value_id +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.zwave_js.const import ( ATTR_COMMAND_CLASS, ATTR_COMMAND_CLASS_NAME, @@ -79,8 +82,8 @@ TRIGGER_SCHEMA = vol.All( async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, - action: Callable, - automation_info: dict[str, Any], + action: AutomationActionType, + automation_info: AutomationTriggerInfo, *, platform_type: str = PLATFORM_TYPE, ) -> CALLBACK_TYPE: @@ -110,9 +113,7 @@ async def async_attach_trigger( unsubs = [] job = HassJob(action) - trigger_data: dict = {} - if automation_info: - trigger_data = automation_info.get("trigger_data", {}) + trigger_data = automation_info["trigger_data"] @callback def async_on_value_updated( diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 29f344a6fa0..9d431cdb7b8 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio import logging -from types import MappingProxyType from typing import Any, Callable import voluptuous as vol @@ -11,7 +10,7 @@ import voluptuous as vol from homeassistant.const import CONF_ID, CONF_PLATFORM from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers.typing import ConfigType, TemplateVarsType from homeassistant.loader import IntegrationNotFound, async_get_integration _PLATFORM_ALIASES = { @@ -62,15 +61,9 @@ async def async_initialize_triggers( name: str, log_cb: Callable, home_assistant_start: bool = False, - variables: dict[str, Any] | MappingProxyType | None = None, + variables: TemplateVarsType = None, ) -> CALLBACK_TYPE | None: """Initialize triggers.""" - info = { - "domain": domain, - "name": name, - "home_assistant_start": home_assistant_start, - "variables": variables, - } triggers = [] for idx, conf in enumerate(trigger_config): @@ -78,7 +71,13 @@ async def async_initialize_triggers( trigger_id = conf.get(CONF_ID, f"{idx}") trigger_idx = f"{idx}" trigger_data = {"id": trigger_id, "idx": trigger_idx} - info = {**info, "trigger_data": trigger_data} + info = { + "domain": domain, + "name": name, + "home_assistant_start": home_assistant_start, + "variables": variables, + "trigger_data": trigger_data, + } triggers.append(platform.async_attach_trigger(hass, conf, action, info)) attach_results = await asyncio.gather(*triggers, return_exceptions=True) diff --git a/script/scaffold/templates/device_trigger/integration/device_trigger.py b/script/scaffold/templates/device_trigger/integration/device_trigger.py index 16dc43f8d59..45c6adb4dcf 100644 --- a/script/scaffold/templates/device_trigger/integration/device_trigger.py +++ b/script/scaffold/templates/device_trigger/integration/device_trigger.py @@ -5,7 +5,10 @@ from typing import Any import voluptuous as vol -from homeassistant.components.automation import AutomationActionType +from homeassistant.components.automation import ( + AutomationActionType, + AutomationTriggerInfo, +) from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state from homeassistant.const import ( @@ -71,7 +74,7 @@ async def async_attach_trigger( hass: HomeAssistant, config: ConfigType, action: AutomationActionType, - automation_info: dict, + automation_info: AutomationTriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" # TODO Implement your own logic to attach triggers. diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index 61c7f73b5fb..b0db6169373 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -4,9 +4,9 @@ import json import pytest import homeassistant.components.automation as automation -from homeassistant.components.mqtt import DOMAIN, debug_info -from homeassistant.components.mqtt.device_trigger import async_attach_trigger +from homeassistant.components.mqtt import _LOGGER, DOMAIN, debug_info from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.setup import async_setup_component from tests.common import ( @@ -697,18 +697,22 @@ async def test_attach_remove(hass, device_reg, mqtt_mock): def callback(trigger): calls.append(trigger["trigger"]["payload"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "bla1", - "type": "button_short_press", - "subtype": "button_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "bla1", + "type": "button_short_press", + "subtype": "button_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Fake short press. @@ -751,18 +755,22 @@ async def test_attach_remove_late(hass, device_reg, mqtt_mock): def callback(trigger): calls.append(trigger["trigger"]["payload"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "bla1", - "type": "button_short_press", - "subtype": "button_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "bla1", + "type": "button_short_press", + "subtype": "button_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) @@ -808,18 +816,22 @@ async def test_attach_remove_late2(hass, device_reg, mqtt_mock): def callback(trigger): calls.append(trigger["trigger"]["payload"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "bla1", - "type": "button_short_press", - "subtype": "button_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "bla1", + "type": "button_short_press", + "subtype": "button_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Remove the trigger diff --git a/tests/components/tasmota/test_device_trigger.py b/tests/components/tasmota/test_device_trigger.py index 8ef4f7df919..aba448bcbe5 100644 --- a/tests/components/tasmota/test_device_trigger.py +++ b/tests/components/tasmota/test_device_trigger.py @@ -1,15 +1,16 @@ -"""The tests for MQTT device triggers.""" +"""The tests for Tasmota device triggers.""" import copy import json -from unittest.mock import patch +from unittest.mock import Mock, patch from hatasmota.switch import TasmotaSwitchTriggerConfig import pytest import homeassistant.components.automation as automation +from homeassistant.components.tasmota import _LOGGER from homeassistant.components.tasmota.const import DEFAULT_PREFIX, DOMAIN -from homeassistant.components.tasmota.device_trigger import async_attach_trigger from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.setup import async_setup_component from .test_common import DEFAULT_CONFIG @@ -812,18 +813,22 @@ async def test_attach_remove(hass, device_reg, mqtt_mock, setup_tasmota): def callback(trigger, context): calls.append(trigger["trigger"]["description"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Fake short press. @@ -869,18 +874,22 @@ async def test_attach_remove_late(hass, device_reg, mqtt_mock, setup_tasmota): def callback(trigger, context): calls.append(trigger["trigger"]["description"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Fake short press. @@ -936,18 +945,22 @@ async def test_attach_remove_late2(hass, device_reg, mqtt_mock, setup_tasmota): def callback(trigger, context): calls.append(trigger["trigger"]["description"]) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Remove the trigger @@ -979,18 +992,22 @@ async def test_attach_remove_unknown1(hass, device_reg, mqtt_mock, setup_tasmota set(), {(dr.CONNECTION_NETWORK_MAC, mac)} ) - remove = await async_attach_trigger( + remove = await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, - None, - None, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], + Mock(), + DOMAIN, + "mock-name", + _LOGGER.log, ) # Remove the trigger @@ -1023,18 +1040,22 @@ async def test_attach_unknown_remove_device_from_registry( set(), {(dr.CONNECTION_NETWORK_MAC, mac)} ) - await async_attach_trigger( + await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, - None, - None, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], + Mock(), + DOMAIN, + "mock-name", + _LOGGER.log, ) # Remove the device @@ -1063,18 +1084,22 @@ async def test_attach_remove_config_entry(hass, device_reg, mqtt_mock, setup_tas def callback(trigger, context): calls.append(trigger["trigger"]["description"]) - await async_attach_trigger( + await async_initialize_triggers( hass, - { - "platform": "device", - "domain": DOMAIN, - "device_id": device_entry.id, - "discovery_id": "00000049A3BC_switch_1_TOGGLE", - "type": "button_short_press", - "subtype": "switch_1", - }, + [ + { + "platform": "device", + "domain": DOMAIN, + "device_id": device_entry.id, + "discovery_id": "00000049A3BC_switch_1_TOGGLE", + "type": "button_short_press", + "subtype": "switch_1", + }, + ], callback, - None, + DOMAIN, + "mock-name", + _LOGGER.log, ) # Fake short press.