Add WS endpoint config/entity_registry/get_entries (#85063)

This commit is contained in:
Erik Montnemery 2023-01-05 15:39:10 +01:00 committed by GitHub
parent 8ffeffd9d2
commit 377396ba16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 0 deletions

View File

@ -62,6 +62,7 @@ async def async_setup(hass: HomeAssistant) -> bool:
)
websocket_api.async_register_command(hass, websocket_list_entities)
websocket_api.async_register_command(hass, websocket_get_entity)
websocket_api.async_register_command(hass, websocket_get_entities)
websocket_api.async_register_command(hass, websocket_update_entity)
websocket_api.async_register_command(hass, websocket_remove_entity)
return True
@ -96,6 +97,33 @@ def websocket_get_entity(
)
@websocket_api.websocket_command(
{
vol.Required("type"): "config/entity_registry/get_entries",
vol.Required("entity_ids"): cv.entity_ids,
}
)
@callback
def websocket_get_entities(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle get entity registry entries command.
Async friendly.
"""
registry = er.async_get(hass)
entity_ids = msg["entity_ids"]
entries: dict[str, dict[str, Any] | None] = {}
for entity_id in entity_ids:
entry = registry.entities.get(entity_id)
entries[entity_id] = _entry_ext_dict(entry) if entry else None
connection.send_message(websocket_api.result_message(msg["id"], entries))
@require_admin
@websocket_api.websocket_command(
{

View File

@ -217,6 +217,89 @@ async def test_get_entity(hass, client):
}
async def test_get_entities(hass, client):
"""Test get entry."""
mock_registry(
hass,
{
"test_domain.name": RegistryEntry(
entity_id="test_domain.name",
unique_id="1234",
platform="test_platform",
name="Hello World",
),
"test_domain.no_name": RegistryEntry(
entity_id="test_domain.no_name",
unique_id="6789",
platform="test_platform",
),
},
)
await client.send_json(
{
"id": 5,
"type": "config/entity_registry/get_entries",
"entity_ids": [
"test_domain.name",
"test_domain.no_name",
"test_domain.no_such_entity",
],
}
)
msg = await client.receive_json()
assert msg["result"] == {
"test_domain.name": {
"aliases": [],
"area_id": None,
"capabilities": None,
"config_entry_id": None,
"device_class": None,
"device_id": None,
"disabled_by": None,
"entity_category": None,
"entity_id": "test_domain.name",
"has_entity_name": False,
"hidden_by": None,
"icon": None,
"id": ANY,
"name": "Hello World",
"options": {},
"original_device_class": None,
"original_icon": None,
"original_name": None,
"platform": "test_platform",
"translation_key": None,
"unique_id": "1234",
},
"test_domain.no_name": {
"aliases": [],
"area_id": None,
"capabilities": None,
"config_entry_id": None,
"device_class": None,
"device_id": None,
"disabled_by": None,
"entity_category": None,
"entity_id": "test_domain.no_name",
"has_entity_name": False,
"hidden_by": None,
"icon": None,
"id": ANY,
"name": None,
"options": {},
"original_device_class": None,
"original_icon": None,
"original_name": None,
"platform": "test_platform",
"translation_key": None,
"unique_id": "6789",
},
"test_domain.no_such_entity": None,
}
async def test_update_entity(hass, client):
"""Test updating entity."""
registry = mock_registry(