Skip to content

Commit c23abc0

Browse files
committed
Add entites_id filter on get_states websocket command
The get_states endpoint currently returns the entire list of states, which can be overwhelm small devices unable to parse hundreds of kB of JSON. This commit adds an entity_ids filter on this endpoint.
1 parent 28ef0a3 commit c23abc0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

homeassistant/components/websocket_api/commands.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,23 @@ def _async_get_allowed_states(
325325

326326

327327
@callback
328-
@decorators.websocket_command({vol.Required("type"): "get_states"})
328+
@decorators.websocket_command(
329+
{
330+
vol.Required("type"): "get_states",
331+
vol.Optional("entity_ids"): cv.entity_ids,
332+
}
333+
)
329334
def handle_get_states(
330335
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
331336
) -> None:
332337
"""Handle get states command."""
333338
states = _async_get_allowed_states(hass, connection)
334339

340+
# filter states if entity_ids is set
341+
entity_ids = set(msg.get("entity_ids", []))
342+
if entity_ids:
343+
states = [state for state in states if state.entity_id in entity_ids]
344+
335345
try:
336346
serialized_states = [state.as_dict_json for state in states]
337347
except (ValueError, TypeError):

tests/components/websocket_api/test_commands.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,3 +2916,25 @@ def auto_off_listener(event):
29162916

29172917
await websocket_client.close()
29182918
await hass.async_block_till_done()
2919+
2920+
2921+
async def test_get_states_entity_id_filter(
2922+
hass: HomeAssistant, websocket_client: MockHAClientWebSocket
2923+
) -> None:
2924+
"""Test get_states command."""
2925+
hass.states.async_set("greeting.hello", "world")
2926+
hass.states.async_set("greeting.bye", "universe")
2927+
2928+
await websocket_client.send_json(
2929+
{
2930+
"id": 5,
2931+
"type": "get_states",
2932+
"entity_ids": ["greeting.bye", "greeting.missing"],
2933+
}
2934+
)
2935+
2936+
msg = await websocket_client.receive_json()
2937+
assert msg["id"] == 5
2938+
assert msg["type"] == const.TYPE_RESULT
2939+
assert msg["success"]
2940+
assert msg["result"] == [hass.states.get("greeting.bye").as_dict()]

0 commit comments

Comments
 (0)