diff --git a/homeassistant/components/arcam_fmj/device_trigger.py b/homeassistant/components/arcam_fmj/device_trigger.py index 25ad5a1133f..383f28d7a20 100644 --- a/homeassistant/components/arcam_fmj/device_trigger.py +++ b/homeassistant/components/arcam_fmj/device_trigger.py @@ -56,7 +56,7 @@ async def async_attach_trigger( automation_info: dict, ) -> CALLBACK_TYPE: """Attach a trigger.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} job = HassJob(action) if config[CONF_TYPE] == "turn_on": @@ -69,9 +69,9 @@ async def async_attach_trigger( job, { "trigger": { + **trigger_data, **config, "description": f"{DOMAIN} - {entity_id}", - "id": trigger_id, } }, event.context, diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index eb77880687d..1733f272229 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -475,8 +475,8 @@ class AutomationEntity(ToggleEntity, RestoreEntity): automation_trace.set_trigger_description(trigger_description) # Add initial variables as the trigger step - if "trigger" in variables and "id" in variables["trigger"]: - trigger_path = f"trigger/{variables['trigger']['id']}" + if "trigger" in variables and "idx" in variables["trigger"]: + trigger_path = f"trigger/{variables['trigger']['idx']}" else: trigger_path = "trigger" trace_element = TraceElement(variables, trigger_path) diff --git a/homeassistant/components/geo_location/trigger.py b/homeassistant/components/geo_location/trigger.py index 90621e7062c..c5e35ece593 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} source = config.get(CONF_SOURCE).lower() zone_entity_id = config.get(CONF_ZONE) trigger_event = config.get(CONF_EVENT) @@ -78,6 +78,7 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "geo_location", "source": source, "entity_id": event.data.get("entity_id"), @@ -86,7 +87,6 @@ async def async_attach_trigger(hass, config, action, automation_info): "zone": zone_state, "event": trigger_event, "description": f"geo_location - {source}", - "id": trigger_id, } }, event.context, diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index ec44c861835..6b4cf520560 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -31,7 +31,7 @@ async def async_attach_trigger( hass, config, action, automation_info, *, platform_type="event" ): """Listen for events based on configuration.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} variables = None if automation_info: variables = automation_info.get("variables") @@ -93,10 +93,10 @@ async def async_attach_trigger( job, { "trigger": { + **trigger_data, "platform": platform_type, "event": event, "description": f"event '{event.event_type}'", - "id": trigger_id, } }, event.context, diff --git a/homeassistant/components/homeassistant/triggers/homeassistant.py b/homeassistant/components/homeassistant/triggers/homeassistant.py index 3593e27b530..ea1a985139f 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} event = config.get(CONF_EVENT) job = HassJob(action) @@ -33,10 +33,10 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "homeassistant", "event": event, "description": "Home Assistant stopping", - "id": trigger_id, } }, event.context, @@ -51,10 +51,10 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "homeassistant", "event": event, "description": "Home Assistant starting", - "id": trigger_id, } }, ) diff --git a/homeassistant/components/homeassistant/triggers/numeric_state.py b/homeassistant/components/homeassistant/triggers/numeric_state.py index 4ab92c36205..366f937a192 100644 --- a/homeassistant/components/homeassistant/triggers/numeric_state.py +++ b/homeassistant/components/homeassistant/triggers/numeric_state.py @@ -78,7 +78,7 @@ async def async_attach_trigger( attribute = config.get(CONF_ATTRIBUTE) job = HassJob(action) - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} _variables = {} if automation_info: _variables = automation_info.get("variables") or {} @@ -132,6 +132,7 @@ async def async_attach_trigger( job, { "trigger": { + **trigger_data, "platform": platform_type, "entity_id": entity_id, "below": below, @@ -140,7 +141,6 @@ async def async_attach_trigger( "to_state": to_s, "for": time_delta if not time_delta else period[entity_id], "description": f"numeric state of {entity_id}", - "id": trigger_id, } }, to_s.context, diff --git a/homeassistant/components/homeassistant/triggers/state.py b/homeassistant/components/homeassistant/triggers/state.py index edcf6b09a78..2c96b6be944 100644 --- a/homeassistant/components/homeassistant/triggers/state.py +++ b/homeassistant/components/homeassistant/triggers/state.py @@ -87,7 +87,7 @@ async def async_attach_trigger( attribute = config.get(CONF_ATTRIBUTE) job = HassJob(action) - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} _variables = {} if automation_info: _variables = automation_info.get("variables") or {} @@ -134,6 +134,7 @@ async def async_attach_trigger( job, { "trigger": { + **trigger_data, "platform": platform_type, "entity_id": entity, "from_state": from_s, @@ -141,7 +142,6 @@ async def async_attach_trigger( "for": time_delta if not time_delta else period[entity], "attribute": attribute, "description": f"state of {entity}", - "id": trigger_id, } }, event.context, diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index 3d7c612cc55..ff78e4c43c8 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} entities = {} removes = [] job = HassJob(action) @@ -51,11 +51,11 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "time", "now": now, "description": description, "entity_id": entity_id, - "id": trigger_id, } }, ) diff --git a/homeassistant/components/homeassistant/triggers/time_pattern.py b/homeassistant/components/homeassistant/triggers/time_pattern.py index bd56ea89663..0380e01c239 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) @@ -76,10 +76,10 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "time_pattern", "now": now, "description": "time pattern", - "id": trigger_id, } }, ) diff --git a/homeassistant/components/homekit_controller/device_trigger.py b/homeassistant/components/homekit_controller/device_trigger.py index d94552df52d..818b75e47d3 100644 --- a/homeassistant/components/homekit_controller/device_trigger.py +++ b/homeassistant/components/homekit_controller/device_trigger.py @@ -76,13 +76,15 @@ class TriggerSource: automation_info: dict, ) -> CALLBACK_TYPE: """Attach a trigger.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = ( + automation_info.get("trigger_data", {}) if automation_info else {} + ) def event_handler(char): if config[CONF_SUBTYPE] != HK_TO_HA_INPUT_EVENT_VALUES[char["value"]]: return self._hass.async_create_task( - action({"trigger": {**config, "id": trigger_id}}) + action({"trigger": {**trigger_data, **config}}) ) trigger = self._triggers[config[CONF_TYPE], config[CONF_SUBTYPE]] diff --git a/homeassistant/components/kodi/device_trigger.py b/homeassistant/components/kodi/device_trigger.py index 855d7f7dba9..584e465b3a6 100644 --- a/homeassistant/components/kodi/device_trigger.py +++ b/homeassistant/components/kodi/device_trigger.py @@ -67,7 +67,7 @@ def _attach_trigger( event_type, automation_info: dict, ): - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} job = HassJob(action) @callback @@ -75,7 +75,7 @@ def _attach_trigger( if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]: hass.async_run_hass_job( job, - {"trigger": {**config, "description": event_type, "id": trigger_id}}, + {"trigger": {**trigger_data, **config, "description": event_type}}, event.context, ) diff --git a/homeassistant/components/litejet/trigger.py b/homeassistant/components/litejet/trigger.py index 5bbd8e2f912..3a9930c5e70 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} number = config.get(CONF_NUMBER) held_more_than = config.get(CONF_HELD_MORE_THAN) held_less_than = config.get(CONF_HELD_LESS_THAN) @@ -46,12 +46,12 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, CONF_PLATFORM: "litejet", CONF_NUMBER: number, CONF_HELD_MORE_THAN: held_more_than, CONF_HELD_LESS_THAN: held_less_than, "description": f"litejet switch #{number}", - "id": trigger_id, } }, ) diff --git a/homeassistant/components/mqtt/trigger.py b/homeassistant/components/mqtt/trigger.py index ae184c9182a..3ee23356c3f 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} topic = config[CONF_TOPIC] wanted_payload = config.get(CONF_PAYLOAD) value_template = config.get(CONF_VALUE_TEMPLATE) @@ -74,12 +74,12 @@ async def async_attach_trigger(hass, config, action, automation_info): if wanted_payload is None or wanted_payload == payload: data = { + **trigger_data, "platform": "mqtt", "topic": mqttmsg.topic, "payload": mqttmsg.payload, "qos": mqttmsg.qos, "description": f"mqtt topic {mqttmsg.topic}", - "id": trigger_id, } with suppress(ValueError): diff --git a/homeassistant/components/philips_js/device_trigger.py b/homeassistant/components/philips_js/device_trigger.py index 0c45debd384..51efa643310 100644 --- a/homeassistant/components/philips_js/device_trigger.py +++ b/homeassistant/components/philips_js/device_trigger.py @@ -45,16 +45,16 @@ async def async_attach_trigger( automation_info: dict, ) -> CALLBACK_TYPE | None: """Attach a trigger.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} registry: DeviceRegistry = await async_get_registry(hass) if config[CONF_TYPE] == TRIGGER_TYPE_TURN_ON: variables = { "trigger": { + **trigger_data, "platform": "device", "domain": DOMAIN, "device_id": config[CONF_DEVICE_ID], "description": f"philips_js '{config[CONF_TYPE]}' event", - "id": trigger_id, } } diff --git a/homeassistant/components/sun/trigger.py b/homeassistant/components/sun/trigger.py index 2a46f665e3c..b612934bfad 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} event = config.get(CONF_EVENT) offset = config.get(CONF_OFFSET) description = event @@ -41,11 +41,11 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": "sun", "event": event, "offset": offset, "description": description, - "id": trigger_id, } }, ) diff --git a/homeassistant/components/tag/trigger.py b/homeassistant/components/tag/trigger.py index e46e737986e..1984505f3a6 100644 --- a/homeassistant/components/tag/trigger.py +++ b/homeassistant/components/tag/trigger.py @@ -18,7 +18,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( async def async_attach_trigger(hass, config, action, automation_info): """Listen for tag_scanned events based on configuration.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} tag_ids = set(config[TAG_ID]) device_ids = set(config[DEVICE_ID]) if DEVICE_ID in config else None @@ -35,10 +35,10 @@ async def async_attach_trigger(hass, config, action, automation_info): job, { "trigger": { + **trigger_data, "platform": DOMAIN, "event": event, "description": "Tag scanned", - "id": trigger_id, } }, event.context, diff --git a/homeassistant/components/template/trigger.py b/homeassistant/components/template/trigger.py index c4751055962..6db25da76ab 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} value_template = config.get(CONF_VALUE_TEMPLATE) value_template.hass = hass time_delta = config.get(CONF_FOR) @@ -99,9 +99,9 @@ async def async_attach_trigger( "to_state": to_s, } trigger_variables = { + **trigger_data, "for": time_delta, "description": description, - "id": trigger_id, } @callback diff --git a/homeassistant/components/webhook/trigger.py b/homeassistant/components/webhook/trigger.py index 687a72108da..6bb8a61eeec 100644 --- a/homeassistant/components/webhook/trigger.py +++ b/homeassistant/components/webhook/trigger.py @@ -20,7 +20,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( ) -async def _handle_webhook(job, trigger_id, hass, webhook_id, request): +async def _handle_webhook(job, trigger_data, hass, webhook_id, request): """Handle incoming webhook.""" result = {"platform": "webhook", "webhook_id": webhook_id} @@ -31,20 +31,20 @@ async def _handle_webhook(job, trigger_id, hass, webhook_id, request): result["query"] = request.query result["description"] = "webhook" - result["id"] = trigger_id + result.update(**trigger_data) hass.async_run_hass_job(job, {"trigger": result}) async def async_attach_trigger(hass, config, action, automation_info): """Trigger based on incoming webhooks.""" - trigger_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} webhook_id = config.get(CONF_WEBHOOK_ID) job = HassJob(action) hass.components.webhook.async_register( automation_info["domain"], automation_info["name"], webhook_id, - partial(_handle_webhook, job, trigger_id), + partial(_handle_webhook, job, trigger_data), ) @callback diff --git a/homeassistant/components/zone/trigger.py b/homeassistant/components/zone/trigger.py index 64c904defea..eb084fe1874 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_id = automation_info.get("trigger_id") if automation_info else None + trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} entity_id = config.get(CONF_ENTITY_ID) zone_entity_id = config.get(CONF_ZONE) event = config.get(CONF_EVENT) @@ -74,6 +74,7 @@ async def async_attach_trigger( job, { "trigger": { + **trigger_data, "platform": platform_type, "entity_id": entity, "from_state": from_s, @@ -81,7 +82,6 @@ async def async_attach_trigger( "zone": zone_state, "event": event, "description": description, - "id": trigger_id, } }, to_s.context, diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 64c5373d8f5..b5a82c3c020 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -75,7 +75,9 @@ async def async_initialize_triggers( for idx, conf in enumerate(trigger_config): platform = await _async_get_trigger_platform(hass, conf) trigger_id = conf.get(CONF_ID, f"{idx}") - info = {**info, "trigger_id": trigger_id} + trigger_idx = f"{idx}" + trigger_data = {"id": trigger_id, "idx": trigger_idx} + info = {**info, "trigger_data": trigger_data} triggers.append(platform.async_attach_trigger(hass, conf, action, info)) attach_results = await asyncio.gather(*triggers, return_exceptions=True)