Fix missing device & entity references in automations (#71103)
parent
2a9f043039
commit
63679d3d29
|
@ -17,6 +17,7 @@ from homeassistant.const import (
|
|||
CONF_CONDITION,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_EVENT_DATA,
|
||||
CONF_ID,
|
||||
CONF_MODE,
|
||||
CONF_PLATFORM,
|
||||
|
@ -360,9 +361,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
|
|||
referenced |= condition.async_extract_devices(conf)
|
||||
|
||||
for conf in self._trigger_config:
|
||||
device = _trigger_extract_device(conf)
|
||||
if device is not None:
|
||||
referenced.add(device)
|
||||
referenced |= set(_trigger_extract_device(conf))
|
||||
|
||||
self._referenced_devices = referenced
|
||||
return referenced
|
||||
|
@ -764,12 +763,22 @@ async def _async_process_if(hass, name, config, p_config):
|
|||
|
||||
|
||||
@callback
|
||||
def _trigger_extract_device(trigger_conf: dict) -> str | None:
|
||||
def _trigger_extract_device(trigger_conf: dict) -> list[str]:
|
||||
"""Extract devices from a trigger config."""
|
||||
if trigger_conf[CONF_PLATFORM] != "device":
|
||||
return None
|
||||
if trigger_conf[CONF_PLATFORM] == "device":
|
||||
return [trigger_conf[CONF_DEVICE_ID]]
|
||||
|
||||
return trigger_conf[CONF_DEVICE_ID]
|
||||
if (
|
||||
trigger_conf[CONF_PLATFORM] == "event"
|
||||
and CONF_EVENT_DATA in trigger_conf
|
||||
and CONF_DEVICE_ID in trigger_conf[CONF_EVENT_DATA]
|
||||
):
|
||||
return [trigger_conf[CONF_EVENT_DATA][CONF_DEVICE_ID]]
|
||||
|
||||
if trigger_conf[CONF_PLATFORM] == "tag" and CONF_DEVICE_ID in trigger_conf:
|
||||
return trigger_conf[CONF_DEVICE_ID]
|
||||
|
||||
return []
|
||||
|
||||
|
||||
@callback
|
||||
|
@ -778,6 +787,9 @@ def _trigger_extract_entities(trigger_conf: dict) -> list[str]:
|
|||
if trigger_conf[CONF_PLATFORM] in ("state", "numeric_state"):
|
||||
return trigger_conf[CONF_ENTITY_ID]
|
||||
|
||||
if trigger_conf[CONF_PLATFORM] == "calendar":
|
||||
return [trigger_conf[CONF_ENTITY_ID]]
|
||||
|
||||
if trigger_conf[CONF_PLATFORM] == "zone":
|
||||
return trigger_conf[CONF_ENTITY_ID] + [trigger_conf[CONF_ZONE]]
|
||||
|
||||
|
@ -787,4 +799,11 @@ def _trigger_extract_entities(trigger_conf: dict) -> list[str]:
|
|||
if trigger_conf[CONF_PLATFORM] == "sun":
|
||||
return ["sun.sun"]
|
||||
|
||||
if (
|
||||
trigger_conf[CONF_PLATFORM] == "event"
|
||||
and CONF_EVENT_DATA in trigger_conf
|
||||
and CONF_ENTITY_ID in trigger_conf[CONF_EVENT_DATA]
|
||||
):
|
||||
return [trigger_conf[CONF_EVENT_DATA][CONF_ENTITY_ID]]
|
||||
|
||||
return []
|
||||
|
|
|
@ -1079,6 +1079,7 @@ async def test_automation_restore_last_triggered_with_initial_state(hass):
|
|||
|
||||
async def test_extraction_functions(hass):
|
||||
"""Test extraction functions."""
|
||||
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
|
@ -1086,7 +1087,24 @@ async def test_extraction_functions(hass):
|
|||
DOMAIN: [
|
||||
{
|
||||
"alias": "test1",
|
||||
"trigger": {"platform": "state", "entity_id": "sensor.trigger_1"},
|
||||
"trigger": [
|
||||
{"platform": "state", "entity_id": "sensor.trigger_state"},
|
||||
{
|
||||
"platform": "numeric_state",
|
||||
"entity_id": "sensor.trigger_numeric_state",
|
||||
"above": 10,
|
||||
},
|
||||
{
|
||||
"platform": "calendar",
|
||||
"entity_id": "calendar.trigger_calendar",
|
||||
"event": "start",
|
||||
},
|
||||
{
|
||||
"platform": "event",
|
||||
"event_type": "state_changed",
|
||||
"event_data": {"entity_id": "sensor.trigger_event"},
|
||||
},
|
||||
],
|
||||
"condition": {
|
||||
"condition": "state",
|
||||
"entity_id": "light.condition_state",
|
||||
|
@ -1111,13 +1129,30 @@ async def test_extraction_functions(hass):
|
|||
},
|
||||
{
|
||||
"alias": "test2",
|
||||
"trigger": {
|
||||
"platform": "device",
|
||||
"domain": "light",
|
||||
"type": "turned_on",
|
||||
"entity_id": "light.trigger_2",
|
||||
"device_id": "trigger-device-2",
|
||||
},
|
||||
"trigger": [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": "light",
|
||||
"type": "turned_on",
|
||||
"entity_id": "light.trigger_2",
|
||||
"device_id": "trigger-device-2",
|
||||
},
|
||||
{
|
||||
"platform": "tag",
|
||||
"tag_id": "1234",
|
||||
"device_id": "device-trigger-tag1",
|
||||
},
|
||||
{
|
||||
"platform": "tag",
|
||||
"tag_id": "1234",
|
||||
"device_id": ["device-trigger-tag2", "device-trigger-tag3"],
|
||||
},
|
||||
{
|
||||
"platform": "event",
|
||||
"event_type": "esphome.button_pressed",
|
||||
"event_data": {"device_id": "device-trigger-event"},
|
||||
},
|
||||
],
|
||||
"condition": {
|
||||
"condition": "device",
|
||||
"device_id": "condition-device",
|
||||
|
@ -1159,7 +1194,10 @@ async def test_extraction_functions(hass):
|
|||
"automation.test2",
|
||||
}
|
||||
assert set(automation.entities_in_automation(hass, "automation.test1")) == {
|
||||
"sensor.trigger_1",
|
||||
"calendar.trigger_calendar",
|
||||
"sensor.trigger_state",
|
||||
"sensor.trigger_numeric_state",
|
||||
"sensor.trigger_event",
|
||||
"light.condition_state",
|
||||
"light.in_both",
|
||||
"light.in_first",
|
||||
|
@ -1173,6 +1211,10 @@ async def test_extraction_functions(hass):
|
|||
"condition-device",
|
||||
"device-in-both",
|
||||
"device-in-last",
|
||||
"device-trigger-event",
|
||||
"device-trigger-tag1",
|
||||
"device-trigger-tag2",
|
||||
"device-trigger-tag3",
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue