1 """Helper classes for Google Cloud integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 from google.cloud
import texttospeech
11 from google.oauth2.service_account
import Credentials
12 import voluptuous
as vol
41 client: texttospeech.TextToSpeechAsyncClient,
42 ) -> dict[str, list[str]]:
43 """Get TTS voice model names keyed by language."""
44 voices: dict[str, list[str]] = {}
45 list_voices_response = await client.list_voices()
46 for voice
in list_voices_response.voices:
47 language_code = voice.language_codes[0]
48 if language_code
not in voices:
49 voices[language_code] = []
50 voices[language_code].append(voice.name)
55 config_options: Mapping[str, Any],
56 voices: dict[str, list[str]],
57 from_config_flow: bool =
False,
59 """Return schema for TTS options with default values from config or constants."""
63 defaults = {}
if from_config_flow
else config_options
70 texttospeech.SsmlVoiceGender.NEUTRAL.name,
76 mode=SelectSelectorMode.DROPDOWN,
77 options=
list(texttospeech.SsmlVoiceGender.__members__),
83 default=defaults.get(CONF_VOICE, DEFAULT_VOICE),
86 mode=SelectSelectorMode.DROPDOWN,
87 options=[
"", *functools.reduce(operator.iadd, voices.values(), [])],
94 texttospeech.AudioEncoding.MP3.name,
100 mode=SelectSelectorMode.DROPDOWN,
101 options=
list(texttospeech.AudioEncoding.__members__),
107 default=defaults.get(CONF_SPEED, 1.0),
111 default=defaults.get(CONF_PITCH, 0),
115 default=defaults.get(CONF_GAIN, 0),
119 default=defaults.get(CONF_PROFILES, []),
122 mode=SelectSelectorMode.DROPDOWN,
125 "wearable-class-device",
126 "handset-class-device",
127 "headphone-class-device",
128 "small-bluetooth-speaker-class-device",
129 "medium-bluetooth-speaker-class-device",
130 "large-home-entertainment-class-device",
131 "large-automotive-class-device",
132 "telephony-class-application",
140 default=defaults.get(CONF_TEXT_TYPE,
"text"),
145 mode=SelectSelectorMode.DROPDOWN,
146 options=[
"text",
"ssml"],
155 """Return schema for TTS platform."""
158 vol.Optional(CONF_KEY_FILE): cv.string,
159 vol.Optional(CONF_LANG, default=DEFAULT_LANG): cv.matches_regex(
160 r"[a-z]{2,3}-[A-Z]{2}|"
163 vol.Optional(CONF_VOICE, default=DEFAULT_VOICE): cv.matches_regex(
164 r"[a-z]{2,3}-[A-Z]{2}-.*-[A-Z]|"
171 """Validate service account info.
174 info: The service account info in Google format.
177 ValueError: If the info is not in the expected format.
180 Credentials.from_service_account_info(info)
dict[str, list[str]] async_tts_voices(texttospeech.TextToSpeechAsyncClient client)
vol.Schema tts_options_schema(Mapping[str, Any] config_options, dict[str, list[str]] voices, bool from_config_flow=False)
vol.Schema tts_platform_schema()
None validate_service_account_info(Mapping[str, str] info)