Allow fetching wake word entity info (#100893)

This commit is contained in:
Paulus Schoutsen 2023-09-25 22:33:04 -04:00 committed by GitHub
parent fa2d77407a
commit c5c5d9ed0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -3,9 +3,13 @@ from __future__ import annotations
from abc import abstractmethod from abc import abstractmethod
from collections.abc import AsyncIterable from collections.abc import AsyncIterable
import dataclasses
import logging import logging
from typing import final from typing import final
import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, EntityCategory from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, EntityCategory
from homeassistant.core import HomeAssistant, callback 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: 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 = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
component.register_shutdown() component.register_shutdown()
@ -120,3 +126,29 @@ class WakeWordDetectionEntity(RestoreEntity):
and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN) and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
): ):
self.__last_detected = state.state 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]},
)

View File

@ -22,6 +22,7 @@ from tests.common import (
mock_platform, mock_platform,
mock_restore_cache, mock_restore_cache,
) )
from tests.typing import WebSocketGenerator
TEST_DOMAIN = "test" TEST_DOMAIN = "test"
@ -259,3 +260,29 @@ async def test_entity_attributes(
) -> None: ) -> None:
"""Test that the provider entity attributes match expectations.""" """Test that the provider entity attributes match expectations."""
assert mock_provider_entity.entity_category == EntityCategory.DIAGNOSTIC 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"},
]
}