From 34eb4aa2d0efc8410c66828f3e40ac00ddfdf0ca Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 15 Mar 2022 16:42:22 +0100 Subject: [PATCH] Exclude hidden entities from targets (#68149) --- homeassistant/helpers/service.py | 14 +++++++------- tests/helpers/test_service.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 82c98c37b84..da562fcded6 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -359,7 +359,7 @@ def async_extract_referenced_entity_ids( if area_id not in area_reg.areas: selected.missing_areas.add(area_id) - # Find devices for this area + # Find devices for targeted areas selected.referenced_devices.update(selector.device_ids) for device_entry in dev_reg.devices.values(): if device_entry.area_id in selector.area_ids: @@ -369,20 +369,20 @@ def async_extract_referenced_entity_ids( return selected for ent_entry in ent_reg.entities.values(): - # Do not add config or diagnostic entities referenced by areas or devices - - if ent_entry.entity_category is not None: + # Do not add entities which are hidden or which are config or diagnostic entities + if ent_entry.entity_category is not None or ent_entry.hidden_by is not None: continue if ( - # when area matches the target area + # The entity's area matches a targeted area ent_entry.area_id in selector.area_ids - # when device matches a referenced devices with no explicitly set area + # The entity's device matches a device referenced by an area and the entity + # has no explicitly set area or ( not ent_entry.area_id and ent_entry.device_id in selected.referenced_devices ) - # when device matches target device + # The entity's device matches a targeted device or ent_entry.device_id in selector.device_ids ): selected.indirectly_referenced.add(ent_entry.entity_id) diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 54507d9b3bd..bbf4a72430c 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -121,6 +121,13 @@ def area_mock(hass): area_id="own-area", entity_category="config", ) + hidden_entity_in_own_area = ent_reg.RegistryEntry( + entity_id="light.hidden_in_own_area", + unique_id="hidden-in-own-area-id", + platform="test", + area_id="own-area", + hidden_by=ent_reg.RegistryEntryHider.USER, + ) entity_in_area = ent_reg.RegistryEntry( entity_id="light.in_area", unique_id="in-area-id", @@ -134,6 +141,13 @@ def area_mock(hass): device_id=device_in_area.id, entity_category="config", ) + hidden_entity_in_area = ent_reg.RegistryEntry( + entity_id="light.hidden_in_area", + unique_id="hidden-in-area-id", + platform="test", + device_id=device_in_area.id, + hidden_by=ent_reg.RegistryEntryHider.USER, + ) entity_in_other_area = ent_reg.RegistryEntry( entity_id="light.in_other_area", unique_id="in-area-a-id", @@ -161,6 +175,13 @@ def area_mock(hass): device_id=device_no_area.id, entity_category="config", ) + hidden_entity_no_area = ent_reg.RegistryEntry( + entity_id="light.hidden_no_area", + unique_id="hidden-no-area-id", + platform="test", + device_id=device_no_area.id, + hidden_by=ent_reg.RegistryEntryHider.USER, + ) entity_diff_area = ent_reg.RegistryEntry( entity_id="light.diff_area", unique_id="diff-area-id", @@ -186,12 +207,15 @@ def area_mock(hass): { entity_in_own_area.entity_id: entity_in_own_area, config_entity_in_own_area.entity_id: config_entity_in_own_area, + hidden_entity_in_own_area.entity_id: hidden_entity_in_own_area, entity_in_area.entity_id: entity_in_area, config_entity_in_area.entity_id: config_entity_in_area, + hidden_entity_in_area.entity_id: hidden_entity_in_area, entity_in_other_area.entity_id: entity_in_other_area, entity_assigned_to_area.entity_id: entity_assigned_to_area, entity_no_area.entity_id: entity_no_area, config_entity_no_area.entity_id: config_entity_no_area, + hidden_entity_no_area.entity_id: hidden_entity_no_area, entity_diff_area.entity_id: entity_diff_area, entity_in_area_a.entity_id: entity_in_area_a, entity_in_area_b.entity_id: entity_in_area_b,