1 """Provide functionality to wake word."""
3 from __future__
import annotations
5 from abc
import abstractmethod
7 from collections.abc
import AsyncIterable
9 from typing
import final
11 import voluptuous
as vol
24 from .const
import DOMAIN
25 from .models
import DetectionResult, WakeWord
28 "async_default_entity",
29 "async_get_wake_word_detection_entity",
33 "WakeWordDetectionEntity",
36 _LOGGER = logging.getLogger(__name__)
38 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
39 DATA_COMPONENT: HassKey[EntityComponent[WakeWordDetectionEntity]] =
HassKey(DOMAIN)
41 TIMEOUT_FETCH_WAKE_WORDS = 10
46 """Return the entity id of the default engine."""
47 return next(iter(hass.states.async_entity_ids(DOMAIN)),
None)
52 hass: HomeAssistant, entity_id: str
53 ) -> WakeWordDetectionEntity |
None:
54 """Return wake word entity."""
55 return hass.data[DATA_COMPONENT].
get_entity(entity_id)
58 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
59 """Set up wake word."""
60 websocket_api.async_register_command(hass, websocket_entity_info)
62 component = hass.data[DATA_COMPONENT] = EntityComponent[WakeWordDetectionEntity](
65 component.register_shutdown()
71 """Set up a config entry."""
76 """Unload a config entry."""
81 """Represent a single wake word provider."""
83 _attr_entity_category = EntityCategory.DIAGNOSTIC
84 _attr_should_poll =
False
85 __last_detected: str |
None =
None
90 """Return the state of the entity."""
95 """Return a list of supported wake words."""
99 self, stream: AsyncIterable[tuple[bytes, int]], wake_word_id: str |
None
100 ) -> DetectionResult |
None:
101 """Try to detect wake word(s) in an audio stream with timestamps.
103 Audio must be 16Khz sample rate with 16-bit mono PCM samples.
107 self, stream: AsyncIterable[tuple[bytes, int]], wake_word_id: str |
None
108 ) -> DetectionResult |
None:
109 """Try to detect wake word(s) in an audio stream with timestamps.
111 Audio must be 16Khz sample rate with 16-bit mono PCM samples.
114 if result
is not None:
122 """Call when the entity is added to hass."""
127 and state.state
is not None
128 and state.state
not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
133 @websocket_api.websocket_command(
{
"type": "wake_word/info",
vol.Required("entity_id"): cv.entity_domain(DOMAIN),
136 @websocket_api.async_response
140 """Get info about wake word entity."""
141 entity = hass.data[DATA_COMPONENT].
get_entity(msg[
"entity_id"])
144 connection.send_error(
145 msg[
"id"], websocket_api.ERR_NOT_FOUND,
"Entity not found"
150 async
with asyncio.timeout(TIMEOUT_FETCH_WAKE_WORDS):
151 wake_words = await entity.get_supported_wake_words()
153 connection.send_error(
154 msg[
"id"], websocket_api.ERR_TIMEOUT,
"Timeout fetching wake words"
158 connection.send_result(
160 {
"wake_words": wake_words},
162
DetectionResult|None _async_process_audio_stream(self, AsyncIterable[tuple[bytes, int]] stream, str|None wake_word_id)
list[WakeWord] get_supported_wake_words(self)
DetectionResult|None async_process_audio_stream(self, AsyncIterable[tuple[bytes, int]] stream, str|None wake_word_id)
None async_internal_added_to_hass(self)
None async_write_ha_state(self)
State|None async_get_last_state(self)
CalendarEntity get_entity(HomeAssistant hass, str entity_id)
None websocket_entity_info(HomeAssistant hass, websocket_api.ActiveConnection connection, dict msg)
str|None async_default_entity(HomeAssistant hass)
WakeWordDetectionEntity|None async_get_wake_word_detection_entity(HomeAssistant hass, str entity_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)