Correct trace path for trigger with custom id (#51847)

This commit is contained in:
Erik Montnemery 2021-06-14 17:09:20 +02:00 committed by GitHub
parent a989677bef
commit 4c9f12b9c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 46 additions and 42 deletions

View file

@ -56,7 +56,7 @@ async def async_attach_trigger(
automation_info: dict, automation_info: dict,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Attach a trigger.""" """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) job = HassJob(action)
if config[CONF_TYPE] == "turn_on": if config[CONF_TYPE] == "turn_on":
@ -69,9 +69,9 @@ async def async_attach_trigger(
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
**config, **config,
"description": f"{DOMAIN} - {entity_id}", "description": f"{DOMAIN} - {entity_id}",
"id": trigger_id,
} }
}, },
event.context, event.context,

View file

@ -475,8 +475,8 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
automation_trace.set_trigger_description(trigger_description) automation_trace.set_trigger_description(trigger_description)
# Add initial variables as the trigger step # Add initial variables as the trigger step
if "trigger" in variables and "id" in variables["trigger"]: if "trigger" in variables and "idx" in variables["trigger"]:
trigger_path = f"trigger/{variables['trigger']['id']}" trigger_path = f"trigger/{variables['trigger']['idx']}"
else: else:
trigger_path = "trigger" trigger_path = "trigger"
trace_element = TraceElement(variables, trigger_path) trace_element = TraceElement(variables, trigger_path)

View file

@ -37,7 +37,7 @@ def source_match(state, source):
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration.""" """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() source = config.get(CONF_SOURCE).lower()
zone_entity_id = config.get(CONF_ZONE) zone_entity_id = config.get(CONF_ZONE)
trigger_event = config.get(CONF_EVENT) trigger_event = config.get(CONF_EVENT)
@ -78,6 +78,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "geo_location", "platform": "geo_location",
"source": source, "source": source,
"entity_id": event.data.get("entity_id"), "entity_id": event.data.get("entity_id"),
@ -86,7 +87,6 @@ async def async_attach_trigger(hass, config, action, automation_info):
"zone": zone_state, "zone": zone_state,
"event": trigger_event, "event": trigger_event,
"description": f"geo_location - {source}", "description": f"geo_location - {source}",
"id": trigger_id,
} }
}, },
event.context, event.context,

View file

@ -31,7 +31,7 @@ async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type="event" hass, config, action, automation_info, *, platform_type="event"
): ):
"""Listen for events based on configuration.""" """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 variables = None
if automation_info: if automation_info:
variables = automation_info.get("variables") variables = automation_info.get("variables")
@ -93,10 +93,10 @@ async def async_attach_trigger(
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": platform_type, "platform": platform_type,
"event": event, "event": event,
"description": f"event '{event.event_type}'", "description": f"event '{event.event_type}'",
"id": trigger_id,
} }
}, },
event.context, event.context,

View file

@ -20,7 +20,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration.""" """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) event = config.get(CONF_EVENT)
job = HassJob(action) job = HassJob(action)
@ -33,10 +33,10 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "homeassistant", "platform": "homeassistant",
"event": event, "event": event,
"description": "Home Assistant stopping", "description": "Home Assistant stopping",
"id": trigger_id,
} }
}, },
event.context, event.context,
@ -51,10 +51,10 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "homeassistant", "platform": "homeassistant",
"event": event, "event": event,
"description": "Home Assistant starting", "description": "Home Assistant starting",
"id": trigger_id,
} }
}, },
) )

View file

@ -78,7 +78,7 @@ async def async_attach_trigger(
attribute = config.get(CONF_ATTRIBUTE) attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action) 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 = {} _variables = {}
if automation_info: if automation_info:
_variables = automation_info.get("variables") or {} _variables = automation_info.get("variables") or {}
@ -132,6 +132,7 @@ async def async_attach_trigger(
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": platform_type, "platform": platform_type,
"entity_id": entity_id, "entity_id": entity_id,
"below": below, "below": below,
@ -140,7 +141,6 @@ async def async_attach_trigger(
"to_state": to_s, "to_state": to_s,
"for": time_delta if not time_delta else period[entity_id], "for": time_delta if not time_delta else period[entity_id],
"description": f"numeric state of {entity_id}", "description": f"numeric state of {entity_id}",
"id": trigger_id,
} }
}, },
to_s.context, to_s.context,

View file

@ -87,7 +87,7 @@ async def async_attach_trigger(
attribute = config.get(CONF_ATTRIBUTE) attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action) 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 = {} _variables = {}
if automation_info: if automation_info:
_variables = automation_info.get("variables") or {} _variables = automation_info.get("variables") or {}
@ -134,6 +134,7 @@ async def async_attach_trigger(
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": platform_type, "platform": platform_type,
"entity_id": entity, "entity_id": entity,
"from_state": from_s, "from_state": from_s,
@ -141,7 +142,6 @@ async def async_attach_trigger(
"for": time_delta if not time_delta else period[entity], "for": time_delta if not time_delta else period[entity],
"attribute": attribute, "attribute": attribute,
"description": f"state of {entity}", "description": f"state of {entity}",
"id": trigger_id,
} }
}, },
event.context, event.context,

View file

@ -39,7 +39,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration.""" """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 = {} entities = {}
removes = [] removes = []
job = HassJob(action) job = HassJob(action)
@ -51,11 +51,11 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "time", "platform": "time",
"now": now, "now": now,
"description": description, "description": description,
"entity_id": entity_id, "entity_id": entity_id,
"id": trigger_id,
} }
}, },
) )

View file

@ -57,7 +57,7 @@ TRIGGER_SCHEMA = vol.All(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration.""" """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) hours = config.get(CONF_HOURS)
minutes = config.get(CONF_MINUTES) minutes = config.get(CONF_MINUTES)
seconds = config.get(CONF_SECONDS) seconds = config.get(CONF_SECONDS)
@ -76,10 +76,10 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "time_pattern", "platform": "time_pattern",
"now": now, "now": now,
"description": "time pattern", "description": "time pattern",
"id": trigger_id,
} }
}, },
) )

View file

@ -76,13 +76,15 @@ class TriggerSource:
automation_info: dict, automation_info: dict,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Attach a trigger.""" """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): def event_handler(char):
if config[CONF_SUBTYPE] != HK_TO_HA_INPUT_EVENT_VALUES[char["value"]]: if config[CONF_SUBTYPE] != HK_TO_HA_INPUT_EVENT_VALUES[char["value"]]:
return return
self._hass.async_create_task( 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]] trigger = self._triggers[config[CONF_TYPE], config[CONF_SUBTYPE]]

View file

@ -67,7 +67,7 @@ def _attach_trigger(
event_type, event_type,
automation_info: dict, 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) job = HassJob(action)
@callback @callback
@ -75,7 +75,7 @@ def _attach_trigger(
if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]: if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]:
hass.async_run_hass_job( hass.async_run_hass_job(
job, job,
{"trigger": {**config, "description": event_type, "id": trigger_id}}, {"trigger": {**trigger_data, **config, "description": event_type}},
event.context, event.context,
) )

View file

@ -31,7 +31,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration.""" """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) number = config.get(CONF_NUMBER)
held_more_than = config.get(CONF_HELD_MORE_THAN) held_more_than = config.get(CONF_HELD_MORE_THAN)
held_less_than = config.get(CONF_HELD_LESS_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, job,
{ {
"trigger": { "trigger": {
**trigger_data,
CONF_PLATFORM: "litejet", CONF_PLATFORM: "litejet",
CONF_NUMBER: number, CONF_NUMBER: number,
CONF_HELD_MORE_THAN: held_more_than, CONF_HELD_MORE_THAN: held_more_than,
CONF_HELD_LESS_THAN: held_less_than, CONF_HELD_LESS_THAN: held_less_than,
"description": f"litejet switch #{number}", "description": f"litejet switch #{number}",
"id": trigger_id,
} }
}, },
) )

View file

@ -37,7 +37,7 @@ _LOGGER = logging.getLogger(__name__)
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration.""" """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] topic = config[CONF_TOPIC]
wanted_payload = config.get(CONF_PAYLOAD) wanted_payload = config.get(CONF_PAYLOAD)
value_template = config.get(CONF_VALUE_TEMPLATE) 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: if wanted_payload is None or wanted_payload == payload:
data = { data = {
**trigger_data,
"platform": "mqtt", "platform": "mqtt",
"topic": mqttmsg.topic, "topic": mqttmsg.topic,
"payload": mqttmsg.payload, "payload": mqttmsg.payload,
"qos": mqttmsg.qos, "qos": mqttmsg.qos,
"description": f"mqtt topic {mqttmsg.topic}", "description": f"mqtt topic {mqttmsg.topic}",
"id": trigger_id,
} }
with suppress(ValueError): with suppress(ValueError):

View file

@ -45,16 +45,16 @@ async def async_attach_trigger(
automation_info: dict, automation_info: dict,
) -> CALLBACK_TYPE | None: ) -> CALLBACK_TYPE | None:
"""Attach a trigger.""" """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) registry: DeviceRegistry = await async_get_registry(hass)
if config[CONF_TYPE] == TRIGGER_TYPE_TURN_ON: if config[CONF_TYPE] == TRIGGER_TYPE_TURN_ON:
variables = { variables = {
"trigger": { "trigger": {
**trigger_data,
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": config[CONF_DEVICE_ID], "device_id": config[CONF_DEVICE_ID],
"description": f"philips_js '{config[CONF_TYPE]}' event", "description": f"philips_js '{config[CONF_TYPE]}' event",
"id": trigger_id,
} }
} }

View file

@ -26,7 +26,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration.""" """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) event = config.get(CONF_EVENT)
offset = config.get(CONF_OFFSET) offset = config.get(CONF_OFFSET)
description = event description = event
@ -41,11 +41,11 @@ async def async_attach_trigger(hass, config, action, automation_info):
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": "sun", "platform": "sun",
"event": event, "event": event,
"offset": offset, "offset": offset,
"description": description, "description": description,
"id": trigger_id,
} }
}, },
) )

View file

@ -18,7 +18,7 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for tag_scanned events based on configuration.""" """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]) tag_ids = set(config[TAG_ID])
device_ids = set(config[DEVICE_ID]) if DEVICE_ID in config else None 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, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": DOMAIN, "platform": DOMAIN,
"event": event, "event": event,
"description": "Tag scanned", "description": "Tag scanned",
"id": trigger_id,
} }
}, },
event.context, event.context,

View file

@ -31,7 +31,7 @@ async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type="template" hass, config, action, automation_info, *, platform_type="template"
): ):
"""Listen for state changes based on configuration.""" """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 = config.get(CONF_VALUE_TEMPLATE)
value_template.hass = hass value_template.hass = hass
time_delta = config.get(CONF_FOR) time_delta = config.get(CONF_FOR)
@ -99,9 +99,9 @@ async def async_attach_trigger(
"to_state": to_s, "to_state": to_s,
} }
trigger_variables = { trigger_variables = {
**trigger_data,
"for": time_delta, "for": time_delta,
"description": description, "description": description,
"id": trigger_id,
} }
@callback @callback

View file

@ -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.""" """Handle incoming webhook."""
result = {"platform": "webhook", "webhook_id": webhook_id} 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["query"] = request.query
result["description"] = "webhook" result["description"] = "webhook"
result["id"] = trigger_id result.update(**trigger_data)
hass.async_run_hass_job(job, {"trigger": result}) hass.async_run_hass_job(job, {"trigger": result})
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(hass, config, action, automation_info):
"""Trigger based on incoming webhooks.""" """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) webhook_id = config.get(CONF_WEBHOOK_ID)
job = HassJob(action) job = HassJob(action)
hass.components.webhook.async_register( hass.components.webhook.async_register(
automation_info["domain"], automation_info["domain"],
automation_info["name"], automation_info["name"],
webhook_id, webhook_id,
partial(_handle_webhook, job, trigger_id), partial(_handle_webhook, job, trigger_data),
) )
@callback @callback

View file

@ -37,7 +37,7 @@ async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type: str = "zone" hass, config, action, automation_info, *, platform_type: str = "zone"
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration.""" """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) entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE) zone_entity_id = config.get(CONF_ZONE)
event = config.get(CONF_EVENT) event = config.get(CONF_EVENT)
@ -74,6 +74,7 @@ async def async_attach_trigger(
job, job,
{ {
"trigger": { "trigger": {
**trigger_data,
"platform": platform_type, "platform": platform_type,
"entity_id": entity, "entity_id": entity,
"from_state": from_s, "from_state": from_s,
@ -81,7 +82,6 @@ async def async_attach_trigger(
"zone": zone_state, "zone": zone_state,
"event": event, "event": event,
"description": description, "description": description,
"id": trigger_id,
} }
}, },
to_s.context, to_s.context,

View file

@ -75,7 +75,9 @@ async def async_initialize_triggers(
for idx, conf in enumerate(trigger_config): for idx, conf in enumerate(trigger_config):
platform = await _async_get_trigger_platform(hass, conf) platform = await _async_get_trigger_platform(hass, conf)
trigger_id = conf.get(CONF_ID, f"{idx}") 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)) triggers.append(platform.async_attach_trigger(hass, conf, action, info))
attach_results = await asyncio.gather(*triggers, return_exceptions=True) attach_results = await asyncio.gather(*triggers, return_exceptions=True)