Skip to content

Commit

Permalink
Extend search with support for integrations (home-assistant#121765)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Jul 12, 2024
1 parent 2e71c8d commit 726fcb4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion homeassistant/components/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ItemType(StrEnum):
ENTITY = "entity"
FLOOR = "floor"
GROUP = "group"
INTEGRATION = "integration"
LABEL = "label"
PERSON = "person"
SCENE = "scene"
Expand Down Expand Up @@ -545,6 +546,9 @@ def _async_resolve_up_device(self, device_id: str) -> dr.DeviceEntry | None:
self._async_resolve_up_area(device_entry.area_id)

self._add(ItemType.CONFIG_ENTRY, device_entry.config_entries)
for config_entry_id in device_entry.config_entries:
if entry := self.hass.config_entries.async_get_entry(config_entry_id):
self._add(ItemType.INTEGRATION, entry.domain)

return device_entry

Expand Down Expand Up @@ -573,10 +577,19 @@ def _async_resolve_up_entity(self, entity_id: str) -> er.RegistryEntry | None:
self._add(ItemType.DEVICE, entity_entry.device_id)

# Add config entry that provided this entity
self._add(ItemType.CONFIG_ENTRY, entity_entry.config_entry_id)
if entity_entry.config_entry_id:
self._add(ItemType.CONFIG_ENTRY, entity_entry.config_entry_id)

if entry := self.hass.config_entries.async_get_entry(
entity_entry.config_entry_id
):
# Add integration that provided this entity
self._add(ItemType.INTEGRATION, entry.domain)

elif source := self._entity_sources.get(entity_id):
# Add config entry that provided this entity
self._add(ItemType.CONFIG_ENTRY, source.get("config_entry"))
self._add(ItemType.INTEGRATION, source["domain"])

return entity_entry

Expand Down
27 changes: 27 additions & 0 deletions tests/components/search/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,14 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {wled_device.id},
ItemType.ENTITY: {wled_segment_1_entity.entity_id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"wled"},
}
assert search(ItemType.AUTOMATION, "automation.wled_device") == {
ItemType.AREA: {living_room_area.id},
ItemType.CONFIG_ENTRY: {wled_config_entry.entry_id},
ItemType.DEVICE: {wled_device.id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"wled"},
}
assert search(ItemType.AUTOMATION, "automation.floor") == {
ItemType.FLOOR: {first_floor.floor_id},
Expand All @@ -561,6 +563,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.GROUP: {"group.wled_hue"},
ItemType.INTEGRATION: {"hue", "wled"},
}
assert search(ItemType.AUTOMATION, "automation.scene") == {
ItemType.AREA: {bedroom_area.id, kitchen_area.id, living_room_area.id},
Expand All @@ -574,6 +577,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
scene_wled_hue_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
}
assert search(ItemType.AUTOMATION, "automation.script") == {
Expand All @@ -589,6 +593,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
script_scene_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {script_scene_entity.entity_id},
}
Expand All @@ -611,6 +616,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.GROUP: {"group.hue", "group.wled_hue"},
ItemType.INTEGRATION: {"hue"},
ItemType.SCENE: {"scene.scene_hue_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.device", "script.hue"},
}
Expand All @@ -624,6 +630,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.GROUP: {"group.wled", "group.wled_hue"},
ItemType.INTEGRATION: {"wled"},
ItemType.SCENE: {"scene.scene_wled_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.wled"},
}
Expand All @@ -639,6 +646,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.GROUP: {"group.wled", "group.wled_hue"},
ItemType.INTEGRATION: {"wled"},
ItemType.LABEL: {label_christmas.label_id},
ItemType.SCENE: {"scene.scene_wled_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.wled"},
Expand All @@ -652,6 +660,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.GROUP: {"group.hue", "group.wled_hue"},
ItemType.INTEGRATION: {"hue"},
ItemType.SCENE: {"scene.scene_hue_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.device", "script.hue"},
}
Expand All @@ -664,6 +673,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {wled_device.id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.GROUP: {"group.wled", "group.wled_hue"},
ItemType.INTEGRATION: {"wled"},
ItemType.SCENE: {"scene.scene_wled_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.wled"},
}
Expand All @@ -673,6 +683,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {wled_device.id},
ItemType.FLOOR: {second_floor.floor_id},
ItemType.GROUP: {"group.wled", "group.wled_hue"},
ItemType.INTEGRATION: {"wled"},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
}
assert search(ItemType.ENTITY, hue_segment_1_entity.entity_id) == {
Expand All @@ -681,6 +692,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {hue_device.id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.GROUP: {"group.hue", "group.wled_hue"},
ItemType.INTEGRATION: {"hue"},
ItemType.LABEL: {label_energy.label_id},
ItemType.SCENE: {"scene.scene_hue_seg_1", scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {"script.hue"},
Expand All @@ -691,6 +703,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {hue_device.id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.GROUP: {"group.hue", "group.wled_hue"},
ItemType.INTEGRATION: {"hue"},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
}
assert not search(ItemType.ENTITY, "automation.wled")
Expand Down Expand Up @@ -722,6 +735,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
}
assert search(ItemType.ENTITY, "light.wled_config_entry_source") == {
ItemType.CONFIG_ENTRY: {wled_config_entry.entry_id},
ItemType.INTEGRATION: {"wled"},
}

assert not search(ItemType.FLOOR, "unknown")
Expand Down Expand Up @@ -780,6 +794,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
wled_segment_2_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"wled"},
}
assert search(ItemType.GROUP, "group.hue") == {
ItemType.AREA: {kitchen_area.id},
Expand All @@ -790,6 +805,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
hue_segment_2_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"hue"},
}
assert search(ItemType.GROUP, "group.wled_hue") == {
ItemType.AREA: {bedroom_area.id, living_room_area.id, kitchen_area.id},
Expand All @@ -803,6 +819,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
hue_segment_2_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.SCRIPT: {"script.group"},
}

Expand Down Expand Up @@ -841,13 +858,15 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {wled_device.id},
ItemType.ENTITY: {wled_segment_1_entity.entity_id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"wled"},
}
assert search(ItemType.SCENE, "scene.scene_hue_seg_1") == {
ItemType.AREA: {kitchen_area.id},
ItemType.CONFIG_ENTRY: {hue_config_entry.entry_id},
ItemType.DEVICE: {hue_device.id},
ItemType.ENTITY: {hue_segment_1_entity.entity_id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"hue"},
}
assert search(ItemType.SCENE, scene_wled_hue_entity.entity_id) == {
ItemType.AREA: {bedroom_area.id, living_room_area.id, kitchen_area.id},
Expand All @@ -861,6 +880,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
hue_segment_2_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.LABEL: {label_other.label_id},
ItemType.SCRIPT: {script_scene_entity.entity_id},
}
Expand All @@ -880,20 +900,23 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
ItemType.DEVICE: {wled_device.id},
ItemType.ENTITY: {wled_segment_1_entity.entity_id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"wled"},
}
assert search(ItemType.SCRIPT, "script.hue") == {
ItemType.AREA: {kitchen_area.id},
ItemType.CONFIG_ENTRY: {hue_config_entry.entry_id},
ItemType.DEVICE: {hue_device.id},
ItemType.ENTITY: {hue_segment_1_entity.entity_id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"hue"},
}
assert search(ItemType.SCRIPT, "script.script_with_templated_services") == {}
assert search(ItemType.SCRIPT, "script.device") == {
ItemType.AREA: {kitchen_area.id},
ItemType.CONFIG_ENTRY: {hue_config_entry.entry_id},
ItemType.DEVICE: {hue_device.id},
ItemType.FLOOR: {first_floor.floor_id},
ItemType.INTEGRATION: {"hue"},
}
assert search(ItemType.SCRIPT, "script.floor") == {
ItemType.FLOOR: {first_floor.floor_id},
Expand All @@ -915,6 +938,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.GROUP: {"group.wled_hue"},
ItemType.INTEGRATION: {"hue", "wled"},
}
assert search(ItemType.SCRIPT, script_scene_entity.entity_id) == {
ItemType.AREA: {bedroom_area.id, kitchen_area.id, living_room_area.id},
Expand All @@ -928,6 +952,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
scene_wled_hue_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.LABEL: {label_other.label_id},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
}
Expand All @@ -944,6 +969,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
script_scene_entity.entity_id,
},
ItemType.FLOOR: {first_floor.floor_id, second_floor.floor_id},
ItemType.INTEGRATION: {"hue", "wled"},
ItemType.SCENE: {scene_wled_hue_entity.entity_id},
ItemType.SCRIPT: {script_scene_entity.entity_id},
}
Expand Down Expand Up @@ -981,6 +1007,7 @@ def search(item_type: ItemType, item_id: str) -> dict[str, set[str]]:
),
ItemType.CONFIG_ENTRY: [hue_config_entry.entry_id],
ItemType.FLOOR: [first_floor.floor_id],
ItemType.INTEGRATION: ["hue"],
ItemType.SCENE: unordered(
["scene.scene_hue_seg_1", scene_wled_hue_entity.entity_id]
),
Expand Down

0 comments on commit 726fcb4

Please sign in to comment.