mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
Add a flag to find disabled entities and devices with the LLM search tool (#194229)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
b2e7711bce
commit
c5a6728011
@@ -71,19 +71,46 @@ def _entity_item(
|
||||
)
|
||||
|
||||
|
||||
def _entities(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
def _disabled_entities(
|
||||
hass: HomeAssistant, ent_reg: er.EntityRegistry, include: Callable[[str], bool]
|
||||
) -> Iterator[_Item]:
|
||||
"""Yield the disabled entities of the included domains.
|
||||
|
||||
Disabled entities have no state, so they come from the entity registry.
|
||||
"""
|
||||
for entry in ent_reg.entities.values():
|
||||
if (
|
||||
entry.disabled
|
||||
and include(entry.domain)
|
||||
and hass.states.get(entry.entity_id) is None
|
||||
):
|
||||
name = er.async_get_full_entity_name(hass, entry) or None
|
||||
yield (
|
||||
entry.entity_id,
|
||||
name,
|
||||
_set(name),
|
||||
er.async_get_entity_aliases(hass, entry),
|
||||
(entry.entity_id,),
|
||||
)
|
||||
|
||||
|
||||
def _entities(hass: HomeAssistant, include_disabled: bool) -> Iterator[_Item]:
|
||||
"""Yield the entities that are not automations, scripts or scenes."""
|
||||
ent_reg = er.async_get(hass)
|
||||
for state in hass.states.async_all():
|
||||
if state.domain not in _OWN_TYPE_DOMAINS:
|
||||
yield _entity_item(hass, ent_reg, state)
|
||||
if include_disabled:
|
||||
yield from _disabled_entities(
|
||||
hass, ent_reg, lambda domain: domain not in _OWN_TYPE_DOMAINS
|
||||
)
|
||||
|
||||
|
||||
def _devices(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
"""Yield the enabled devices, including child devices."""
|
||||
def _devices(hass: HomeAssistant, include_disabled: bool) -> Iterator[_Item]:
|
||||
"""Yield the devices, including child devices."""
|
||||
dev_reg = dr.async_get(hass)
|
||||
for device in chain(dev_reg.devices, dev_reg.child_devices):
|
||||
if device.disabled:
|
||||
if device.disabled and not include_disabled:
|
||||
continue
|
||||
yield (
|
||||
device.id,
|
||||
@@ -97,32 +124,36 @@ def _devices(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
)
|
||||
|
||||
|
||||
def _areas(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
def _areas(hass: HomeAssistant, include_disabled: bool) -> Iterator[_Item]:
|
||||
"""Yield the areas."""
|
||||
for area in ar.async_get(hass).async_list_areas():
|
||||
yield area.id, area.name, (area.name,), area.aliases, ()
|
||||
|
||||
|
||||
def _floors(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
def _floors(hass: HomeAssistant, include_disabled: bool) -> Iterator[_Item]:
|
||||
"""Yield the floors."""
|
||||
for floor in fr.async_get(hass).async_list_floors():
|
||||
yield floor.floor_id, floor.name, (floor.name,), floor.aliases, ()
|
||||
|
||||
|
||||
def _labels(hass: HomeAssistant) -> Iterator[_Item]:
|
||||
def _labels(hass: HomeAssistant, include_disabled: bool) -> Iterator[_Item]:
|
||||
"""Yield the labels."""
|
||||
for label in lr.async_get(hass).async_list_labels():
|
||||
yield label.label_id, label.name, (label.name,), (), ()
|
||||
|
||||
|
||||
def _domain_entities(domain: str, hass: HomeAssistant) -> Iterator[_Item]:
|
||||
def _domain_entities(
|
||||
domain: str, hass: HomeAssistant, include_disabled: bool
|
||||
) -> Iterator[_Item]:
|
||||
"""Yield the entities of a domain."""
|
||||
ent_reg = er.async_get(hass)
|
||||
for state in hass.states.async_all(domain):
|
||||
yield _entity_item(hass, ent_reg, state)
|
||||
if include_disabled:
|
||||
yield from _disabled_entities(hass, ent_reg, domain.__eq__)
|
||||
|
||||
|
||||
_ITEMS: dict[str, Callable[[HomeAssistant], Iterator[_Item]]] = {
|
||||
_ITEMS: dict[str, Callable[[HomeAssistant, bool], Iterator[_Item]]] = {
|
||||
"entity": _entities,
|
||||
"device": _devices,
|
||||
"area": _areas,
|
||||
@@ -183,6 +214,11 @@ class SearchTool(Tool):
|
||||
),
|
||||
default=0,
|
||||
): probatio.All(probatio.Coerce(int), probatio.Range(min=0)),
|
||||
probatio.Optional(
|
||||
"include_disabled",
|
||||
description="Also return disabled entities and devices.",
|
||||
default=False,
|
||||
): probatio.All(probatio.Boolean(), bool),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -195,6 +231,7 @@ class SearchTool(Tool):
|
||||
query: str = args["query"].casefold()
|
||||
item_type: str | None = args.get("type")
|
||||
offset: int = args["offset"]
|
||||
include_disabled: bool = args["include_disabled"]
|
||||
|
||||
if offset and item_type is None:
|
||||
return ToolResult(
|
||||
@@ -205,7 +242,9 @@ class SearchTool(Tool):
|
||||
result: JsonObjectType = {}
|
||||
has_more = False
|
||||
for type_ in [item_type] if item_type else _ITEMS:
|
||||
total, results = _search(_ITEMS[type_](hass), query, offset)
|
||||
total, results = _search(
|
||||
_ITEMS[type_](hass, include_disabled), query, offset
|
||||
)
|
||||
if not total:
|
||||
continue
|
||||
group: JsonObjectType = {"total": total, "results": results}
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
dict({
|
||||
'additionalProperties': False,
|
||||
'properties': dict({
|
||||
'include_disabled': dict({
|
||||
'default': False,
|
||||
'description': 'Also return disabled entities and devices.',
|
||||
'type': 'boolean',
|
||||
}),
|
||||
'offset': dict({
|
||||
'default': 0,
|
||||
'description': 'Number of results to skip. Requires type. Use the next_offset of the previous result.',
|
||||
@@ -182,6 +187,45 @@
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_search[disabled_device]
|
||||
dict({
|
||||
'device': dict({
|
||||
'results': list([
|
||||
list([
|
||||
'<disabled device>',
|
||||
'Kitchen disabled',
|
||||
]),
|
||||
]),
|
||||
'total': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_search[disabled_entity]
|
||||
dict({
|
||||
'entity': dict({
|
||||
'results': list([
|
||||
list([
|
||||
'sensor.kitchen_oven',
|
||||
'Kitchen oven',
|
||||
]),
|
||||
]),
|
||||
'total': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_search[disabled_script]
|
||||
dict({
|
||||
'script': dict({
|
||||
'results': list([
|
||||
list([
|
||||
'script.evening_news',
|
||||
'Evening news',
|
||||
]),
|
||||
]),
|
||||
'total': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_search[entity_id]
|
||||
dict({
|
||||
'automation': dict({
|
||||
|
||||
@@ -123,11 +123,28 @@ async def setup_items(
|
||||
entity_registry.async_update_entity(
|
||||
"automation.kitchen_lights", aliases=["Cooking lights"]
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
"sensor",
|
||||
"test",
|
||||
"oven",
|
||||
suggested_object_id="kitchen_oven",
|
||||
original_name="Kitchen oven",
|
||||
disabled_by=er.RegistryEntryDisabler.USER,
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
"script",
|
||||
"test",
|
||||
"evening_news",
|
||||
suggested_object_id="evening_news",
|
||||
original_name="Evening news",
|
||||
disabled_by=er.RegistryEntryDisabler.USER,
|
||||
)
|
||||
|
||||
return {
|
||||
search_item_ids["device"]: "<sensor device>",
|
||||
search_item_ids["child_device"]: "<child device>",
|
||||
renamed_device.id: "<hub device>",
|
||||
disabled_device.id: "<disabled device>",
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +199,16 @@ def test_parameters_openapi(snapshot: SnapshotAssertion) -> None:
|
||||
pytest.param({"query": "counter"}, id="partial_alias"),
|
||||
pytest.param({"query": "cooking lights"}, id="automation_alias"),
|
||||
pytest.param({"query": "kitchen_lights"}, id="entity_id"),
|
||||
pytest.param(
|
||||
{"query": "kitchen oven", "include_disabled": True}, id="disabled_entity"
|
||||
),
|
||||
pytest.param(
|
||||
{"query": "kitchen disabled", "include_disabled": True},
|
||||
id="disabled_device",
|
||||
),
|
||||
pytest.param(
|
||||
{"query": "evening news", "include_disabled": True}, id="disabled_script"
|
||||
),
|
||||
pytest.param({"query": "nothing"}, id="no_match"),
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user