Home Assistant Unofficial Reference 2024.12.1
stt.py
Go to the documentation of this file.
1 """Support for the cloud for speech to text service."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import AsyncIterable
6 import logging
7 
8 from hass_nabucasa import Cloud
9 from hass_nabucasa.voice import STT_LANGUAGES, VoiceError
10 
11 from homeassistant.components.stt import (
12  AudioBitRates,
13  AudioChannels,
14  AudioCodecs,
15  AudioFormats,
16  AudioSampleRates,
17  SpeechMetadata,
18  SpeechResult,
19  SpeechResultState,
20  SpeechToTextEntity,
21 )
22 from homeassistant.config_entries import ConfigEntry
23 from homeassistant.const import Platform
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.setup import async_when_setup
27 
28 from .assist_pipeline import async_migrate_cloud_pipeline_engine
29 from .client import CloudClient
30 from .const import DATA_CLOUD, DATA_PLATFORMS_SETUP, STT_ENTITY_UNIQUE_ID
31 
32 _LOGGER = logging.getLogger(__name__)
33 
34 
36  hass: HomeAssistant,
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up Home Assistant Cloud speech platform via config entry."""
41  stt_platform_loaded = hass.data[DATA_PLATFORMS_SETUP][Platform.STT]
42  stt_platform_loaded.set()
43  cloud = hass.data[DATA_CLOUD]
45 
46 
48  """Home Assistant Cloud speech API provider."""
49 
50  _attr_name = "Home Assistant Cloud"
51  _attr_unique_id = STT_ENTITY_UNIQUE_ID
52 
53  def __init__(self, cloud: Cloud[CloudClient]) -> None:
54  """Initialize cloud Speech to text entity."""
55  self.cloudcloud = cloud
56 
57  @property
58  def supported_languages(self) -> list[str]:
59  """Return a list of supported languages."""
60  return STT_LANGUAGES
61 
62  @property
63  def supported_formats(self) -> list[AudioFormats]:
64  """Return a list of supported formats."""
65  return [AudioFormats.WAV, AudioFormats.OGG]
66 
67  @property
68  def supported_codecs(self) -> list[AudioCodecs]:
69  """Return a list of supported codecs."""
70  return [AudioCodecs.PCM, AudioCodecs.OPUS]
71 
72  @property
73  def supported_bit_rates(self) -> list[AudioBitRates]:
74  """Return a list of supported bitrates."""
75  return [AudioBitRates.BITRATE_16]
76 
77  @property
78  def supported_sample_rates(self) -> list[AudioSampleRates]:
79  """Return a list of supported samplerates."""
80  return [AudioSampleRates.SAMPLERATE_16000]
81 
82  @property
83  def supported_channels(self) -> list[AudioChannels]:
84  """Return a list of supported channels."""
85  return [AudioChannels.CHANNEL_MONO]
86 
87  async def async_added_to_hass(self) -> None:
88  """Run when entity is about to be added to hass."""
89 
90  async def pipeline_setup(hass: HomeAssistant, _comp: str) -> None:
91  """When assist_pipeline is set up."""
92  assert self.platformplatform.config_entry
93  self.platformplatform.config_entry.async_create_task(
94  hass,
96  self.hasshass, platform=Platform.STT, engine_id=self.entity_identity_id
97  ),
98  )
99 
100  async_when_setup(self.hasshass, "assist_pipeline", pipeline_setup)
101 
103  self, metadata: SpeechMetadata, stream: AsyncIterable[bytes]
104  ) -> SpeechResult:
105  """Process an audio stream to STT service."""
106  content_type = (
107  f"audio/{metadata.format!s}; codecs=audio/{metadata.codec!s};"
108  " samplerate=16000"
109  )
110 
111  # Process STT
112  try:
113  result = await self.cloudcloud.voice.process_stt(
114  stream=stream,
115  content_type=content_type,
116  language=metadata.language,
117  )
118  except VoiceError as err:
119  _LOGGER.error("Voice error: %s", err)
120  return SpeechResult(None, SpeechResultState.ERROR)
121 
122  # Return Speech as Text
123  return SpeechResult(
124  result.text,
125  SpeechResultState.SUCCESS if result.success else SpeechResultState.ERROR,
126  )
list[AudioBitRates] supported_bit_rates(self)
Definition: stt.py:73
None __init__(self, Cloud[CloudClient] cloud)
Definition: stt.py:53
SpeechResult async_process_audio_stream(self, SpeechMetadata metadata, AsyncIterable[bytes] stream)
Definition: stt.py:104
list[AudioSampleRates] supported_sample_rates(self)
Definition: stt.py:78
list[AudioFormats] supported_formats(self)
Definition: stt.py:63
list[AudioChannels] supported_channels(self)
Definition: stt.py:83
None async_migrate_cloud_pipeline_engine(HomeAssistant hass, Platform platform, str engine_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: stt.py:39
None async_when_setup(core.HomeAssistant hass, str component, Callable[[core.HomeAssistant, str], Awaitable[None]] when_setup_cb)
Definition: setup.py:587