Add WS command homeassistant/expose_entity/list_exposed

This commit is contained in:
Erik 2025-02-19 10:06:03 +01:00
parent 81c909e8ce
commit bbe804cef3
2 changed files with 88 additions and 2 deletions

View File

@ -124,6 +124,9 @@ class ExposedEntities:
websocket_api.async_register_command(self._hass, ws_expose_new_entities_get)
websocket_api.async_register_command(self._hass, ws_expose_new_entities_set)
websocket_api.async_register_command(self._hass, ws_list_exposed_entities)
websocket_api.async_register_command(
self._hass, ws_list_entities_exposed_to_assistant
)
await self._async_load_data()
@callback
@ -452,6 +455,30 @@ def ws_list_exposed_entities(
connection.send_result(msg["id"], {"exposed_entities": result})
@callback
@websocket_api.require_admin
@websocket_api.websocket_command(
{
vol.Required("type"): "homeassistant/expose_entity/list_exposed",
vol.Required("assistant"): vol.In(KNOWN_ASSISTANTS),
}
)
def ws_list_entities_exposed_to_assistant(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""List entities which are exposed to an assistant."""
exposed_entities = hass.data[DATA_EXPOSED_ENTITIES]
assistant = msg.get("assistant")
entity_registry = er.async_get(hass)
result = [
entity_id
for entity_id in chain(exposed_entities.entities, entity_registry.entities)
if assistant in (entity_settings := async_get_entity_settings(hass, entity_id))
and entity_settings[assistant]["should_expose"]
]
connection.send_result(msg["id"], {"exposed_entities": result})
@callback
@websocket_api.require_admin
@websocket_api.websocket_command(

View File

@ -485,12 +485,12 @@ async def test_should_expose_hidden_categorized(
assert async_should_expose(hass, "cloud.alexa", "lock.test_unique3") is False
async def test_list_exposed_entities(
async def test_exposed_entities_list(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test list exposed entities."""
"""Test homeassistant/expose_entity/list."""
ws_client = await hass_ws_client(hass)
assert await async_setup_component(hass, "homeassistant", {})
await hass.async_block_till_done()
@ -539,6 +539,65 @@ async def test_list_exposed_entities(
}
async def test_exposed_entities_list_exposed(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test homeassistant/expose_entity/list_exposed."""
ws_client = await hass_ws_client(hass)
assert await async_setup_component(hass, "homeassistant", {})
await hass.async_block_till_done()
entry1 = entity_registry.async_get_or_create("test", "test", "unique1")
entry2 = entity_registry.async_get_or_create("test", "test", "unique2")
# Expose 1 to Alexa
await ws_client.send_json_auto_id(
{
"type": "homeassistant/expose_entity",
"assistants": ["cloud.alexa"],
"entity_ids": [entry1.entity_id],
"should_expose": True,
}
)
response = await ws_client.receive_json()
assert response["success"]
# Expose 2 to Google
await ws_client.send_json_auto_id(
{
"type": "homeassistant/expose_entity",
"assistants": ["cloud.google_assistant"],
"entity_ids": [entry2.entity_id],
"should_expose": True,
}
)
response = await ws_client.receive_json()
assert response["success"]
# List with filter
await ws_client.send_json_auto_id(
{
"type": "homeassistant/expose_entity/list_exposed",
"assistant": "cloud.alexa",
}
)
response = await ws_client.receive_json()
assert response["success"]
assert response["result"] == {"exposed_entities": ["test.test_unique1"]}
await ws_client.send_json_auto_id(
{
"type": "homeassistant/expose_entity/list_exposed",
"assistant": "cloud.google_assistant",
}
)
response = await ws_client.receive_json()
assert response["success"]
assert response["result"] == {"exposed_entities": ["test.test_unique2"]}
async def test_listeners(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None: