Fix service target devices by label (#127229)

* Fix service target devices by label

* More explicit test
This commit is contained in:
Artur Pragacz 2024-10-25 21:30:04 +02:00 committed by GitHub
parent 017b1cae26
commit 624834de9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 13 deletions

View File

@ -571,20 +571,32 @@ def async_extract_referenced_entity_ids( # noqa: C901
for area_entry in area_reg.areas.get_areas_for_floor(floor_id)
)
# Find devices for targeted areas
selected.referenced_devices.update(selector.device_ids)
selected.referenced_areas.update(selector.area_ids)
if selected.referenced_areas:
for area_id in selected.referenced_areas:
selected.referenced_devices.update(
device_entry.id
for device_entry in dev_reg.devices.get_devices_for_area_id(area_id)
)
selected.referenced_devices.update(selector.device_ids)
if not selected.referenced_areas and not selected.referenced_devices:
return selected
# Add indirectly referenced by device
selected.indirectly_referenced.update(
entry.entity_id
for device_id in selected.referenced_devices
for entry in entities.get_entries_for_device_id(device_id)
# Do not add entities which are hidden or which are config
# or diagnostic entities.
if (entry.entity_category is None and entry.hidden_by is None)
)
# Find devices for targeted areas
referenced_devices_by_area: set[str] = set()
if selected.referenced_areas:
for area_id in selected.referenced_areas:
referenced_devices_by_area.update(
device_entry.id
for device_entry in dev_reg.devices.get_devices_for_area_id(area_id)
)
selected.referenced_devices.update(referenced_devices_by_area)
# Add indirectly referenced by area
selected.indirectly_referenced.update(
entry.entity_id
@ -595,10 +607,10 @@ def async_extract_referenced_entity_ids( # noqa: C901
# or diagnostic entities.
if entry.entity_category is None and entry.hidden_by is None
)
# Add indirectly referenced by device
# Add indirectly referenced by area through device
selected.indirectly_referenced.update(
entry.entity_id
for device_id in selected.referenced_devices
for device_id in referenced_devices_by_area
for entry in entities.get_entries_for_device_id(device_id)
# Do not add entities which are hidden or which are config
# or diagnostic entities.
@ -610,11 +622,10 @@ def async_extract_referenced_entity_ids( # noqa: C901
# by an area and the entity
# has no explicitly set area
not entry.area_id
# The entity's device matches a targeted device
or device_id in selector.device_ids
)
)
)
return selected

View File

@ -347,6 +347,13 @@ def label_mock(hass: HomeAssistant) -> None:
platform="test",
device_id=device_has_label1.id,
)
entity_with_label1_from_device_and_different_area = er.RegistryEntry(
entity_id="light.with_label1_from_device_diff_area",
unique_id="with_label1_from_device_diff_area",
platform="test",
device_id=device_has_label1.id,
area_id=area_without_labels.id,
)
entity_with_label1_and_label2_from_device = er.RegistryEntry(
entity_id="light.with_label1_and_label2_from_device",
unique_id="with_label1_and_label2_from_device",
@ -373,6 +380,7 @@ def label_mock(hass: HomeAssistant) -> None:
config_entity_with_my_label.entity_id: config_entity_with_my_label,
entity_with_label1_and_label2_from_device.entity_id: entity_with_label1_and_label2_from_device,
entity_with_label1_from_device.entity_id: entity_with_label1_from_device,
entity_with_label1_from_device_and_different_area.entity_id: entity_with_label1_from_device_and_different_area,
entity_with_labels_from_device.entity_id: entity_with_labels_from_device,
entity_with_my_label.entity_id: entity_with_my_label,
entity_with_no_labels.entity_id: entity_with_no_labels,
@ -754,6 +762,7 @@ async def test_extract_entity_ids_from_labels(hass: HomeAssistant) -> None:
assert {
"light.with_label1_from_device",
"light.with_label1_from_device_diff_area",
"light.with_labels_from_device",
"light.with_label1_and_label2_from_device",
} == await service.async_extract_entity_ids(hass, call)