mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Fix service target devices by label (#127229)
* Fix service target devices by label * More explicit test
This commit is contained in:
parent
017b1cae26
commit
624834de9c
@ -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)
|
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)
|
selected.referenced_areas.update(selector.area_ids)
|
||||||
if selected.referenced_areas:
|
selected.referenced_devices.update(selector.device_ids)
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
if not selected.referenced_areas and not selected.referenced_devices:
|
if not selected.referenced_areas and not selected.referenced_devices:
|
||||||
return selected
|
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
|
# Add indirectly referenced by area
|
||||||
selected.indirectly_referenced.update(
|
selected.indirectly_referenced.update(
|
||||||
entry.entity_id
|
entry.entity_id
|
||||||
@ -595,10 +607,10 @@ def async_extract_referenced_entity_ids( # noqa: C901
|
|||||||
# or diagnostic entities.
|
# or diagnostic entities.
|
||||||
if entry.entity_category is None and entry.hidden_by is None
|
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(
|
selected.indirectly_referenced.update(
|
||||||
entry.entity_id
|
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)
|
for entry in entities.get_entries_for_device_id(device_id)
|
||||||
# Do not add entities which are hidden or which are config
|
# Do not add entities which are hidden or which are config
|
||||||
# or diagnostic entities.
|
# or diagnostic entities.
|
||||||
@ -610,11 +622,10 @@ def async_extract_referenced_entity_ids( # noqa: C901
|
|||||||
# by an area and the entity
|
# by an area and the entity
|
||||||
# has no explicitly set area
|
# has no explicitly set area
|
||||||
not entry.area_id
|
not entry.area_id
|
||||||
# The entity's device matches a targeted device
|
|
||||||
or device_id in selector.device_ids
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return selected
|
return selected
|
||||||
|
|
||||||
|
|
||||||
|
@ -347,6 +347,13 @@ def label_mock(hass: HomeAssistant) -> None:
|
|||||||
platform="test",
|
platform="test",
|
||||||
device_id=device_has_label1.id,
|
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_with_label1_and_label2_from_device = er.RegistryEntry(
|
||||||
entity_id="light.with_label1_and_label2_from_device",
|
entity_id="light.with_label1_and_label2_from_device",
|
||||||
unique_id="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,
|
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_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.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_labels_from_device.entity_id: entity_with_labels_from_device,
|
||||||
entity_with_my_label.entity_id: entity_with_my_label,
|
entity_with_my_label.entity_id: entity_with_my_label,
|
||||||
entity_with_no_labels.entity_id: entity_with_no_labels,
|
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 {
|
assert {
|
||||||
"light.with_label1_from_device",
|
"light.with_label1_from_device",
|
||||||
|
"light.with_label1_from_device_diff_area",
|
||||||
"light.with_labels_from_device",
|
"light.with_labels_from_device",
|
||||||
"light.with_label1_and_label2_from_device",
|
"light.with_label1_and_label2_from_device",
|
||||||
} == await service.async_extract_entity_ids(hass, call)
|
} == await service.async_extract_entity_ids(hass, call)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user