1 """Support for the Google Cloud STT service."""
3 from __future__
import annotations
5 from collections.abc
import AsyncGenerator, AsyncIterable
8 from google.api_core.exceptions
import GoogleAPIError, Unauthenticated
9 from google.cloud
import speech_v1
28 CONF_SERVICE_ACCOUNT_INFO,
35 _LOGGER = logging.getLogger(__name__)
40 config_entry: ConfigEntry,
41 async_add_entities: AddEntitiesCallback,
43 """Set up Google Cloud speech platform via config entry."""
44 service_account_info = config_entry.data[CONF_SERVICE_ACCOUNT_INFO]
45 client = speech_v1.SpeechAsyncClient.from_service_account_info(service_account_info)
50 """Google Cloud STT entity."""
55 client: speech_v1.SpeechAsyncClient,
57 """Init Google Cloud STT entity."""
61 identifiers={(DOMAIN, entry.entry_id)},
62 manufacturer=
"Google",
64 entry_type=dr.DeviceEntryType.SERVICE,
68 self.
_model_model = entry.options.get(CONF_STT_MODEL, DEFAULT_STT_MODEL)
72 """Return a list of supported languages."""
77 """Return a list of supported formats."""
78 return [AudioFormats.WAV, AudioFormats.OGG]
82 """Return a list of supported codecs."""
83 return [AudioCodecs.PCM, AudioCodecs.OPUS]
87 """Return a list of supported bitrates."""
88 return [AudioBitRates.BITRATE_16]
92 """Return a list of supported samplerates."""
93 return [AudioSampleRates.SAMPLERATE_16000]
97 """Return a list of supported channels."""
98 return [AudioChannels.CHANNEL_MONO]
101 self, metadata: SpeechMetadata, stream: AsyncIterable[bytes]
103 """Process an audio stream to STT service."""
104 streaming_config = speech_v1.StreamingRecognitionConfig(
105 config=speech_v1.RecognitionConfig(
107 speech_v1.RecognitionConfig.AudioEncoding.OGG_OPUS
108 if metadata.codec == AudioCodecs.OPUS
109 else speech_v1.RecognitionConfig.AudioEncoding.LINEAR16
111 sample_rate_hertz=metadata.sample_rate,
112 language_code=metadata.language,
117 async
def request_generator() -> (
118 AsyncGenerator[speech_v1.StreamingRecognizeRequest]
121 yield speech_v1.StreamingRecognizeRequest(streaming_config=streaming_config)
123 async
for audio_content
in stream:
124 yield speech_v1.StreamingRecognizeRequest(audio_content=audio_content)
127 responses = await self.
_client_client.streaming_recognize(
128 requests=request_generator(),
133 async
for response
in responses:
134 _LOGGER.debug(
"response: %s", response)
135 if not response.results:
137 result = response.results[0]
138 if not result.alternatives:
140 transcript += response.results[0].alternatives[0].transcript
141 except GoogleAPIError
as err:
142 _LOGGER.error(
"Error occurred during Google Cloud STT call: %s", err)
143 if isinstance(err, Unauthenticated):
144 self.
_entry_entry.async_start_reauth(self.
hasshass)
145 return SpeechResult(
None, SpeechResultState.ERROR)
147 return SpeechResult(transcript, SpeechResultState.SUCCESS)
SpeechResult async_process_audio_stream(self, SpeechMetadata metadata, AsyncIterable[bytes] stream)
list[AudioCodecs] supported_codecs(self)
list[AudioChannels] supported_channels(self)
list[AudioSampleRates] supported_sample_rates(self)
list[AudioFormats] supported_formats(self)
None __init__(self, ConfigEntry entry, speech_v1.SpeechAsyncClient client)
list[str] supported_languages(self)
list[AudioBitRates] supported_bit_rates(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)