diff --git a/homeassistant/components/homeassistant/exposed_entities.py b/homeassistant/components/homeassistant/exposed_entities.py index 7bd9f9ab7bc..0c815502669 100644 --- a/homeassistant/components/homeassistant/exposed_entities.py +++ b/homeassistant/components/homeassistant/exposed_entities.py @@ -432,6 +432,7 @@ def ws_expose_entity( @websocket_api.websocket_command( { vol.Required("type"): "homeassistant/expose_entity/list", + vol.Optional("assistant"): vol.In(KNOWN_ASSISTANTS), } ) def ws_list_exposed_entities( @@ -441,10 +442,18 @@ def ws_list_exposed_entities( result: dict[str, Any] = {} exposed_entities = hass.data[DATA_EXPOSED_ENTITIES] + required_assistant = msg.get("assistant") entity_registry = er.async_get(hass) for entity_id in chain(exposed_entities.entities, entity_registry.entities): - result[entity_id] = {} entity_settings = async_get_entity_settings(hass, entity_id) + if required_assistant and ( + (required_assistant not in entity_settings) + or (not entity_settings[required_assistant].get("should_expose")) + ): + # Not exposed to required assistant + continue + + result[entity_id] = {} for assistant, settings in entity_settings.items(): if "should_expose" not in settings: continue diff --git a/tests/components/homeassistant/test_exposed_entities.py b/tests/components/homeassistant/test_exposed_entities.py index 1f1955c2f82..0c57aad58ea 100644 --- a/tests/components/homeassistant/test_exposed_entities.py +++ b/tests/components/homeassistant/test_exposed_entities.py @@ -539,6 +539,70 @@ async def test_list_exposed_entities( } +async def test_list_exposed_entities_with_filter( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test list exposed entities with filter.""" + 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", "assistant": "cloud.alexa"} + ) + response = await ws_client.receive_json() + assert response["success"] + assert response["result"] == { + "exposed_entities": { + "test.test_unique1": {"cloud.alexa": True}, + }, + } + + await ws_client.send_json_auto_id( + { + "type": "homeassistant/expose_entity/list", + "assistant": "cloud.google_assistant", + } + ) + response = await ws_client.receive_json() + assert response["success"] + assert response["result"] == { + "exposed_entities": { + "test.test_unique2": {"cloud.google_assistant": True}, + }, + } + + async def test_listeners( hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: