diff --git a/homeassistant/components/wake_word/__init__.py b/homeassistant/components/wake_word/__init__.py index eeed7b8029b..9ce9cca75ff 100644 --- a/homeassistant/components/wake_word/__init__.py +++ b/homeassistant/components/wake_word/__init__.py @@ -3,9 +3,13 @@ from __future__ import annotations from abc import abstractmethod from collections.abc import AsyncIterable +import dataclasses import logging from typing import final +import voluptuous as vol + +from homeassistant.components import websocket_api from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, EntityCategory from homeassistant.core import HomeAssistant, callback @@ -49,7 +53,9 @@ def async_get_wake_word_detection_entity( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up STT.""" + """Set up wake word.""" + websocket_api.async_register_command(hass, websocket_entity_info) + component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass) component.register_shutdown() @@ -120,3 +126,29 @@ class WakeWordDetectionEntity(RestoreEntity): and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN) ): self.__last_detected = state.state + + +@websocket_api.websocket_command( + { + "type": "wake_word/info", + vol.Required("entity_id"): cv.entity_domain(DOMAIN), + } +) +@callback +def websocket_entity_info( + hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict +) -> None: + """Get info about wake word entity.""" + component: EntityComponent[WakeWordDetectionEntity] = hass.data[DOMAIN] + entity = component.get_entity(msg["entity_id"]) + + if entity is None: + connection.send_error( + msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + ) + return + + connection.send_result( + msg["id"], + {"wake_words": [dataclasses.asdict(ww) for ww in entity.supported_wake_words]}, + ) diff --git a/tests/components/wake_word/test_init.py b/tests/components/wake_word/test_init.py index 7f3e8f011ee..1e03632d083 100644 --- a/tests/components/wake_word/test_init.py +++ b/tests/components/wake_word/test_init.py @@ -22,6 +22,7 @@ from tests.common import ( mock_platform, mock_restore_cache, ) +from tests.typing import WebSocketGenerator TEST_DOMAIN = "test" @@ -259,3 +260,29 @@ async def test_entity_attributes( ) -> None: """Test that the provider entity attributes match expectations.""" assert mock_provider_entity.entity_category == EntityCategory.DIAGNOSTIC + + +async def test_list_wake_words( + hass: HomeAssistant, + setup: MockProviderEntity, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test that the list_wake_words websocket command works.""" + client = await hass_ws_client(hass) + await client.send_json( + { + "id": 5, + "type": "wake_word/info", + "entity_id": setup.entity_id, + } + ) + + msg = await client.receive_json() + + assert msg["success"] + assert msg["result"] == { + "wake_words": [ + {"ww_id": "test_ww", "name": "Test Wake Word"}, + {"ww_id": "test_ww_2", "name": "Test Wake Word 2"}, + ] + }