mirror of
https://github.com/home-assistant/core.git
synced 2025-08-10 08:05:06 +02:00
Use TextToSpeechAsyncClient in Google Cloud TTS (#120847)
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
"""Support for the Google Cloud TTS service."""
|
"""Support for the Google Cloud TTS service."""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from google.api_core.exceptions import GoogleAPIError
|
||||||
from google.cloud import texttospeech
|
from google.cloud import texttospeech
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@@ -210,11 +210,11 @@ class GoogleCloudTTSProvider(Provider):
|
|||||||
self._text_type = text_type
|
self._text_type = text_type
|
||||||
|
|
||||||
if key_file:
|
if key_file:
|
||||||
self._client = texttospeech.TextToSpeechClient.from_service_account_json(
|
self._client = (
|
||||||
key_file
|
texttospeech.TextToSpeechAsyncClient.from_service_account_json(key_file)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._client = texttospeech.TextToSpeechClient()
|
self._client = texttospeech.TextToSpeechAsyncClient()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_languages(self):
|
def supported_languages(self):
|
||||||
@@ -261,45 +261,31 @@ class GoogleCloudTTSProvider(Provider):
|
|||||||
)
|
)
|
||||||
options = options_schema(options)
|
options = options_schema(options)
|
||||||
|
|
||||||
_encoding = options[CONF_ENCODING]
|
encoding = options[CONF_ENCODING]
|
||||||
_voice = options[CONF_VOICE]
|
voice = options[CONF_VOICE]
|
||||||
if _voice and not _voice.startswith(language):
|
if voice and not voice.startswith(language):
|
||||||
language = _voice[:5]
|
language = voice[:5]
|
||||||
|
|
||||||
try:
|
|
||||||
params = {options[CONF_TEXT_TYPE]: message}
|
|
||||||
synthesis_input = texttospeech.SynthesisInput(**params)
|
|
||||||
|
|
||||||
|
request = texttospeech.SynthesizeSpeechRequest(
|
||||||
|
input=texttospeech.SynthesisInput(**{options[CONF_TEXT_TYPE]: message}),
|
||||||
voice=texttospeech.VoiceSelectionParams(
|
voice=texttospeech.VoiceSelectionParams(
|
||||||
language_code=language,
|
language_code=language,
|
||||||
ssml_gender=texttospeech.SsmlVoiceGender[options[CONF_GENDER]],
|
ssml_gender=texttospeech.SsmlVoiceGender[options[CONF_GENDER]],
|
||||||
name=_voice,
|
name=voice,
|
||||||
)
|
),
|
||||||
|
|
||||||
audio_config=texttospeech.AudioConfig(
|
audio_config=texttospeech.AudioConfig(
|
||||||
audio_encoding=texttospeech.AudioEncoding[_encoding],
|
audio_encoding=texttospeech.AudioEncoding[encoding],
|
||||||
speaking_rate=options[CONF_SPEED],
|
speaking_rate=options[CONF_SPEED],
|
||||||
pitch=options[CONF_PITCH],
|
pitch=options[CONF_PITCH],
|
||||||
volume_gain_db=options[CONF_GAIN],
|
volume_gain_db=options[CONF_GAIN],
|
||||||
effects_profile_id=options[CONF_PROFILES],
|
effects_profile_id=options[CONF_PROFILES],
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
request = {
|
try:
|
||||||
"voice": voice,
|
response = await self._client.synthesize_speech(request, timeout=10)
|
||||||
"audio_config": audio_config,
|
except GoogleAPIError as err:
|
||||||
"input": synthesis_input,
|
_LOGGER.error("Error occurred during Google Cloud TTS call: %s", err)
|
||||||
}
|
|
||||||
|
|
||||||
async with asyncio.timeout(10):
|
|
||||||
assert self.hass
|
|
||||||
response = await self.hass.async_add_executor_job(
|
|
||||||
self._client.synthesize_speech, request
|
|
||||||
)
|
|
||||||
return _encoding, response.audio_content
|
|
||||||
|
|
||||||
except TimeoutError as ex:
|
|
||||||
_LOGGER.error("Timeout for Google Cloud TTS call: %s", ex)
|
|
||||||
except Exception:
|
|
||||||
_LOGGER.exception("Error occurred during Google Cloud TTS call")
|
|
||||||
|
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
return encoding, response.audio_content
|
||||||
|
Reference in New Issue
Block a user