From bbe804cef35d39c67b1aadbd740c2c6e3c2a7254 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 19 Feb 2025 10:06:03 +0100 Subject: [PATCH] Add WS command homeassistant/expose_entity/list_exposed --- .../homeassistant/exposed_entities.py | 27 ++++++++ .../homeassistant/test_exposed_entities.py | 63 ++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant/exposed_entities.py b/homeassistant/components/homeassistant/exposed_entities.py index 7bd9f9ab7bc..8dd00ee6a89 100644 --- a/homeassistant/components/homeassistant/exposed_entities.py +++ b/homeassistant/components/homeassistant/exposed_entities.py @@ -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( diff --git a/tests/components/homeassistant/test_exposed_entities.py b/tests/components/homeassistant/test_exposed_entities.py index 1f1955c2f82..0d8bccd0f17 100644 --- a/tests/components/homeassistant/test_exposed_entities.py +++ b/tests/components/homeassistant/test_exposed_entities.py @@ -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: