@@ -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+
22352431async 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