Skip to content

Commit 9f0eb6f

Browse files
abmantisbramkragten
authored andcommitted
Support target triggers in automation relation extraction (#160369)
1 parent da19cc0 commit 9f0eb6f

File tree

2 files changed

+240
-6
lines changed

2 files changed

+240
-6
lines changed

homeassistant/components/automation/__init__.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections.abc import Callable, Mapping
88
from dataclasses import dataclass
99
import logging
10-
from typing import Any, Protocol, cast
10+
from typing import Any, Literal, Protocol, cast
1111

1212
from propcache.api import cached_property
1313
import voluptuous as vol
@@ -16,7 +16,10 @@
1616
from homeassistant.components.blueprint import CONF_USE_BLUEPRINT
1717
from homeassistant.components.labs import async_listen as async_labs_listen
1818
from homeassistant.const import (
19+
ATTR_AREA_ID,
1920
ATTR_ENTITY_ID,
21+
ATTR_FLOOR_ID,
22+
ATTR_LABEL_ID,
2023
ATTR_MODE,
2124
ATTR_NAME,
2225
CONF_ACTIONS,
@@ -30,6 +33,7 @@
3033
CONF_OPTIONS,
3134
CONF_PATH,
3235
CONF_PLATFORM,
36+
CONF_TARGET,
3337
CONF_TRIGGERS,
3438
CONF_VARIABLES,
3539
CONF_ZONE,
@@ -588,20 +592,32 @@ def is_on(self) -> bool:
588592
"""Return True if entity is on."""
589593
return self._async_detach_triggers is not None or self._is_enabled
590594

591-
@property
595+
@cached_property
592596
def referenced_labels(self) -> set[str]:
593597
"""Return a set of referenced labels."""
594-
return self.action_script.referenced_labels
598+
referenced = self.action_script.referenced_labels
595599

596-
@property
600+
for conf in self._trigger_config:
601+
referenced |= set(_get_targets_from_trigger_config(conf, ATTR_LABEL_ID))
602+
return referenced
603+
604+
@cached_property
597605
def referenced_floors(self) -> set[str]:
598606
"""Return a set of referenced floors."""
599-
return self.action_script.referenced_floors
607+
referenced = self.action_script.referenced_floors
608+
609+
for conf in self._trigger_config:
610+
referenced |= set(_get_targets_from_trigger_config(conf, ATTR_FLOOR_ID))
611+
return referenced
600612

601613
@cached_property
602614
def referenced_areas(self) -> set[str]:
603615
"""Return a set of referenced areas."""
604-
return self.action_script.referenced_areas
616+
referenced = self.action_script.referenced_areas
617+
618+
for conf in self._trigger_config:
619+
referenced |= set(_get_targets_from_trigger_config(conf, ATTR_AREA_ID))
620+
return referenced
605621

606622
@property
607623
def referenced_blueprint(self) -> str | None:
@@ -1209,6 +1225,9 @@ def _trigger_extract_devices(trigger_conf: dict) -> list[str]:
12091225
if trigger_conf[CONF_PLATFORM] == "tag" and CONF_DEVICE_ID in trigger_conf:
12101226
return trigger_conf[CONF_DEVICE_ID] # type: ignore[no-any-return]
12111227

1228+
if target_devices := _get_targets_from_trigger_config(trigger_conf, CONF_DEVICE_ID):
1229+
return target_devices
1230+
12121231
return []
12131232

12141233

@@ -1239,9 +1258,28 @@ def _trigger_extract_entities(trigger_conf: dict) -> list[str]:
12391258
):
12401259
return [trigger_conf[CONF_EVENT_DATA][CONF_ENTITY_ID]]
12411260

1261+
if target_entities := _get_targets_from_trigger_config(
1262+
trigger_conf, CONF_ENTITY_ID
1263+
):
1264+
return target_entities
1265+
12421266
return []
12431267

12441268

1269+
@callback
1270+
def _get_targets_from_trigger_config(
1271+
config: dict,
1272+
target: Literal["entity_id", "device_id", "area_id", "floor_id", "label_id"],
1273+
) -> list[str]:
1274+
"""Extract targets from a target config."""
1275+
if not (target_conf := config.get(CONF_TARGET)):
1276+
return []
1277+
if not (targets := target_conf.get(target)):
1278+
return []
1279+
1280+
return [targets] if isinstance(targets, str) else targets
1281+
1282+
12451283
@websocket_api.websocket_command({"type": "automation/config", "entity_id": str})
12461284
def websocket_config(
12471285
hass: HomeAssistant,

tests/components/automation/test_init.py

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,202 @@ async def test_extraction_functions(
22322232
assert automation.blueprint_in_automation(hass, "automation.test3") is None
22332233

22342234

2235+
async def test_extraction_functions_with_targets(
2236+
hass: HomeAssistant,
2237+
device_registry: dr.DeviceRegistry,
2238+
hass_ws_client: WebSocketGenerator,
2239+
) -> None:
2240+
"""Test extraction functions with targets in triggers.
2241+
2242+
This test verifies that targets specified in trigger configurations
2243+
(using new-style triggers that support target) are properly extracted for
2244+
entity, device, area, floor, and label references.
2245+
"""
2246+
config_entry = MockConfigEntry(domain="fake_integration", data={})
2247+
config_entry.mock_state(hass, ConfigEntryState.LOADED)
2248+
config_entry.add_to_hass(hass)
2249+
2250+
trigger_device = device_registry.async_get_or_create(
2251+
config_entry_id=config_entry.entry_id,
2252+
connections={(dr.CONNECTION_NETWORK_MAC, "00:00:00:00:00:01")},
2253+
)
2254+
2255+
await async_setup_component(hass, "homeassistant", {})
2256+
await async_setup_component(
2257+
hass, "scene", {"scene": {"name": "test", "entities": {}}}
2258+
)
2259+
await hass.async_block_till_done()
2260+
2261+
# Enable the new_triggers_conditions feature flag to allow new-style triggers
2262+
assert await async_setup_component(hass, "labs", {})
2263+
ws_client = await hass_ws_client(hass)
2264+
await ws_client.send_json_auto_id(
2265+
{
2266+
"type": "labs/update",
2267+
"domain": "automation",
2268+
"preview_feature": "new_triggers_conditions",
2269+
"enabled": True,
2270+
}
2271+
)
2272+
msg = await ws_client.receive_json()
2273+
assert msg["success"]
2274+
await hass.async_block_till_done()
2275+
2276+
assert await async_setup_component(
2277+
hass,
2278+
DOMAIN,
2279+
{
2280+
DOMAIN: [
2281+
{
2282+
"alias": "test1",
2283+
"triggers": [
2284+
# Single entity_id in target
2285+
{
2286+
"trigger": "scene.activated",
2287+
"target": {"entity_id": "scene.target_entity"},
2288+
},
2289+
# Multiple entity_ids in target
2290+
{
2291+
"trigger": "scene.activated",
2292+
"target": {
2293+
"entity_id": [
2294+
"scene.target_entity_list1",
2295+
"scene.target_entity_list2",
2296+
]
2297+
},
2298+
},
2299+
# Single device_id in target
2300+
{
2301+
"trigger": "scene.activated",
2302+
"target": {"device_id": trigger_device.id},
2303+
},
2304+
# Multiple device_ids in target
2305+
{
2306+
"trigger": "scene.activated",
2307+
"target": {
2308+
"device_id": [
2309+
"target-device-1",
2310+
"target-device-2",
2311+
]
2312+
},
2313+
},
2314+
# Single area_id in target
2315+
{
2316+
"trigger": "scene.activated",
2317+
"target": {"area_id": "area-target-single"},
2318+
},
2319+
# Multiple area_ids in target
2320+
{
2321+
"trigger": "scene.activated",
2322+
"target": {"area_id": ["area-target-1", "area-target-2"]},
2323+
},
2324+
# Single floor_id in target
2325+
{
2326+
"trigger": "scene.activated",
2327+
"target": {"floor_id": "floor-target-single"},
2328+
},
2329+
# Multiple floor_ids in target
2330+
{
2331+
"trigger": "scene.activated",
2332+
"target": {
2333+
"floor_id": ["floor-target-1", "floor-target-2"]
2334+
},
2335+
},
2336+
# Single label_id in target
2337+
{
2338+
"trigger": "scene.activated",
2339+
"target": {"label_id": "label-target-single"},
2340+
},
2341+
# Multiple label_ids in target
2342+
{
2343+
"trigger": "scene.activated",
2344+
"target": {
2345+
"label_id": ["label-target-1", "label-target-2"]
2346+
},
2347+
},
2348+
# Combined targets
2349+
{
2350+
"trigger": "scene.activated",
2351+
"target": {
2352+
"entity_id": "scene.combined_entity",
2353+
"device_id": "combined-device",
2354+
"area_id": "combined-area",
2355+
"floor_id": "combined-floor",
2356+
"label_id": "combined-label",
2357+
},
2358+
},
2359+
],
2360+
"conditions": [],
2361+
"actions": [
2362+
{
2363+
"action": "test.script",
2364+
"data": {"entity_id": "light.action_entity"},
2365+
},
2366+
],
2367+
},
2368+
]
2369+
},
2370+
)
2371+
2372+
# Test entity extraction from trigger targets
2373+
assert set(automation.entities_in_automation(hass, "automation.test1")) == {
2374+
"scene.target_entity",
2375+
"scene.target_entity_list1",
2376+
"scene.target_entity_list2",
2377+
"scene.combined_entity",
2378+
"light.action_entity",
2379+
}
2380+
2381+
# Test device extraction from trigger targets
2382+
assert set(automation.devices_in_automation(hass, "automation.test1")) == {
2383+
trigger_device.id,
2384+
"target-device-1",
2385+
"target-device-2",
2386+
"combined-device",
2387+
}
2388+
2389+
# Test area extraction from trigger targets
2390+
assert set(automation.areas_in_automation(hass, "automation.test1")) == {
2391+
"area-target-single",
2392+
"area-target-1",
2393+
"area-target-2",
2394+
"combined-area",
2395+
}
2396+
2397+
# Test floor extraction from trigger targets
2398+
assert set(automation.floors_in_automation(hass, "automation.test1")) == {
2399+
"floor-target-single",
2400+
"floor-target-1",
2401+
"floor-target-2",
2402+
"combined-floor",
2403+
}
2404+
2405+
# Test label extraction from trigger targets
2406+
assert set(automation.labels_in_automation(hass, "automation.test1")) == {
2407+
"label-target-single",
2408+
"label-target-1",
2409+
"label-target-2",
2410+
"combined-label",
2411+
}
2412+
2413+
# Test automations_with_* functions
2414+
assert set(automation.automations_with_entity(hass, "scene.target_entity")) == {
2415+
"automation.test1"
2416+
}
2417+
assert set(automation.automations_with_device(hass, trigger_device.id)) == {
2418+
"automation.test1"
2419+
}
2420+
assert set(automation.automations_with_area(hass, "area-target-single")) == {
2421+
"automation.test1"
2422+
}
2423+
assert set(automation.automations_with_floor(hass, "floor-target-single")) == {
2424+
"automation.test1"
2425+
}
2426+
assert set(automation.automations_with_label(hass, "label-target-single")) == {
2427+
"automation.test1"
2428+
}
2429+
2430+
22352431
async def test_logbook_humanify_automation_triggered_event(hass: HomeAssistant) -> None:
22362432
"""Test humanifying Automation Trigger event."""
22372433
hass.config.components.add("recorder")

0 commit comments

Comments
 (0)