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,
) -> 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,

View file

@ -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)

View file

@ -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,

View file

@ -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,

View file

@ -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,
}
},
)

View file

@ -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,

View file

@ -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,

View file

@ -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,
}
},
)

View file

@ -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,
}
},
)

View file

@ -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]]

View file

@ -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,
)

View file

@ -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,
}
},
)

View file

@ -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):

View file

@ -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,
}
}

View file

@ -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,
}
},
)

View file

@ -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,

View file

@ -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

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."""
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

View file

@ -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,

View file

@ -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)