Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion homeassistant/components/arcam_fmj/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +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
job = HassJob(action)

if config[CONF_TYPE] == "turn_on":
Expand All @@ -66,7 +67,13 @@ def _handle_event(event: Event):
if event.data[ATTR_ENTITY_ID] == entity_id:
hass.async_run_hass_job(
job,
{"trigger": {**config, "description": f"{DOMAIN} - {entity_id}"}},
{
"trigger": {
**config,
"description": f"{DOMAIN} - {entity_id}",
"id": trigger_id,
}
},
event.context,
)

Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,11 @@ async def async_trigger(self, run_variables, context=None, skip_condition=False)
automation_trace.set_trigger_description(trigger_description)

# Add initial variables as the trigger step
trace_element = TraceElement(variables, "trigger")
if "trigger" in variables and "id" in variables["trigger"]:
trigger_path = f"trigger/{variables['trigger']['id']}"
else:
trigger_path = "trigger"
Comment on lines +422 to +425
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note; this is a breaking change and requires a frontend update.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases do we not have a trigger_Id? It's making things quite complicated

Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the automation is force started from the frontend it was not triggered by any of the triggers

trace_element = TraceElement(variables, trigger_path)
trace_append_element(trace_element)

if (
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/geo_location/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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
source = config.get(CONF_SOURCE).lower()
zone_entity_id = config.get(CONF_ZONE)
trigger_event = config.get(CONF_EVENT)
Expand Down Expand Up @@ -74,6 +75,7 @@ def state_change_listener(event):
"zone": zone_state,
"event": trigger_event,
"description": f"geo_location - {source}",
"id": trigger_id,
}
},
event.context,
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/homeassistant/triggers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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
variables = None
if automation_info:
variables = automation_info.get("variables")
Expand Down Expand Up @@ -95,6 +96,7 @@ def handle_event(event):
"platform": platform_type,
"event": event,
"description": f"event '{event.event_type}'",
"id": trigger_id,
}
},
event.context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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
event = config.get(CONF_EVENT)
job = HassJob(action)

Expand All @@ -34,6 +35,7 @@ def hass_shutdown(event):
"platform": "homeassistant",
"event": event,
"description": "Home Assistant stopping",
"id": trigger_id,
}
},
event.context,
Expand All @@ -51,6 +53,7 @@ def hass_shutdown(event):
"platform": "homeassistant",
"event": event,
"description": "Home Assistant starting",
"id": trigger_id,
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +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
_variables = {}
if automation_info:
_variables = automation_info.get("variables") or {}
Expand Down Expand Up @@ -139,6 +140,7 @@ def call_action():
"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,
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/homeassistant/triggers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +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
_variables = {}
if automation_info:
_variables = automation_info.get("variables") or {}
Expand Down Expand Up @@ -140,6 +141,7 @@ def call_action():
"for": time_delta if not time_delta else period[entity],
"attribute": attribute,
"description": f"state of {entity}",
"id": trigger_id,
}
},
event.context,
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/homeassistant/triggers/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

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
entities = {}
removes = []
job = HassJob(action)
Expand All @@ -54,6 +55,7 @@ def time_automation_listener(description, now, *, entity_id=None):
"now": now,
"description": description,
"entity_id": entity_id,
"id": trigger_id,
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __call__(self, value):

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
hours = config.get(CONF_HOURS)
minutes = config.get(CONF_MINUTES)
seconds = config.get(CONF_SECONDS)
Expand All @@ -78,6 +79,7 @@ def time_automation_listener(now):
"platform": "time_pattern",
"now": now,
"description": "time pattern",
"id": trigger_id,
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ 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

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}))
self._hass.async_create_task(
action({"trigger": {**config, "id": trigger_id}})
)

trigger = self._triggers[config[CONF_TYPE], config[CONF_SUBTYPE]]
iid = trigger["characteristic"]
Expand Down
13 changes: 9 additions & 4 deletions homeassistant/components/kodi/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,21 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> list[dict]:

@callback
def _attach_trigger(
hass: HomeAssistant, config: ConfigType, action: AutomationActionType, event_type
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
event_type,
automation_info: dict,
):
trigger_id = automation_info.get("trigger_id") if automation_info else None
job = HassJob(action)

@callback
def _handle_event(event: Event):
if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]:
hass.async_run_hass_job(
job,
{"trigger": {**config, "description": event_type}},
{"trigger": {**config, "description": event_type, "id": trigger_id}},
event.context,
)

Expand All @@ -85,9 +90,9 @@ async def async_attach_trigger(
) -> CALLBACK_TYPE:
"""Attach a trigger."""
if config[CONF_TYPE] == "turn_on":
return _attach_trigger(hass, config, action, EVENT_TURN_ON)
return _attach_trigger(hass, config, action, EVENT_TURN_ON, automation_info)

if config[CONF_TYPE] == "turn_off":
return _attach_trigger(hass, config, action, EVENT_TURN_OFF)
return _attach_trigger(hass, config, action, EVENT_TURN_OFF, automation_info)

return lambda: None
2 changes: 2 additions & 0 deletions homeassistant/components/litejet/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

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
number = config.get(CONF_NUMBER)
held_more_than = config.get(CONF_HELD_MORE_THAN)
held_less_than = config.get(CONF_HELD_LESS_THAN)
Expand All @@ -50,6 +51,7 @@ def call_action():
CONF_HELD_MORE_THAN: held_more_than,
CONF_HELD_LESS_THAN: held_less_than,
"description": f"litejet switch #{number}",
"id": trigger_id,
}
},
)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/mqtt/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

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
topic = config[CONF_TOPIC]
wanted_payload = config.get(CONF_PAYLOAD)
value_template = config.get(CONF_VALUE_TEMPLATE)
Expand Down Expand Up @@ -78,6 +79,7 @@ def mqtt_automation_listener(mqttmsg):
"payload": mqttmsg.payload,
"qos": mqttmsg.qos,
"description": f"mqtt topic {mqttmsg.topic}",
"id": trigger_id,
}

with suppress(ValueError):
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/philips_js/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ 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
registry: DeviceRegistry = await async_get_registry(hass)
if config[CONF_TYPE] == TRIGGER_TYPE_TURN_ON:
variables = {
Expand All @@ -53,6 +54,7 @@ async def async_attach_trigger(
"domain": DOMAIN,
"device_id": config[CONF_DEVICE_ID],
"description": f"philips_js '{config[CONF_TYPE]}' event",
"id": trigger_id,
}
}

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/sun/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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
event = config.get(CONF_EVENT)
offset = config.get(CONF_OFFSET)
description = event
Expand All @@ -44,6 +45,7 @@ def call_action():
"event": event,
"offset": offset,
"description": description,
"id": trigger_id,
}
},
)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/tag/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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
tag_ids = set(config[TAG_ID])
device_ids = set(config[DEVICE_ID]) if DEVICE_ID in config else None

Expand All @@ -37,6 +38,7 @@ async def handle_event(event):
"platform": DOMAIN,
"event": event,
"description": "Tag scanned",
"id": trigger_id,
}
},
event.context,
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/template/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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
value_template = config.get(CONF_VALUE_TEMPLATE)
value_template.hass = hass
time_delta = config.get(CONF_FOR)
Expand Down Expand Up @@ -100,6 +101,7 @@ def template_listener(event, updates):
trigger_variables = {
"for": time_delta,
"description": description,
"id": trigger_id,
}

@callback
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/webhook/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)


async def _handle_webhook(job, hass, webhook_id, request):
async def _handle_webhook(job, trigger_id, hass, webhook_id, request):
"""Handle incoming webhook."""
result = {"platform": "webhook", "webhook_id": webhook_id}

Expand All @@ -28,18 +28,20 @@ async def _handle_webhook(job, hass, webhook_id, request):

result["query"] = request.query
result["description"] = "webhook"
result["id"] = trigger_id
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
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),
partial(_handle_webhook, job, trigger_id),
)

@callback
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/zone/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +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
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)
event = config.get(CONF_EVENT)
Expand Down Expand Up @@ -80,6 +81,7 @@ def zone_automation_listener(zone_event):
"zone": zone_state,
"event": event,
"description": description,
"id": trigger_id,
}
},
to_s.context,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/helpers/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ async def async_initialize_triggers(
}

triggers = []
for conf in trigger_config:
for idx, conf in enumerate(trigger_config):
platform = await _async_get_trigger_platform(hass, conf)
info = {**info, "trigger_id": f"{idx}"}
triggers.append(platform.async_attach_trigger(hass, conf, action, info))

attach_results = await asyncio.gather(*triggers, return_exceptions=True)
Expand Down
Loading