-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
/
Copy pathhelpers.py
183 lines (166 loc) · 6.07 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Helper classes for Google Cloud integration."""
from __future__ import annotations
from collections.abc import Mapping
import functools
import operator
from typing import Any
from google.cloud import texttospeech
from google.oauth2.service_account import Credentials
import voluptuous as vol
from homeassistant.components.tts import CONF_LANG
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.selector import (
NumberSelector,
NumberSelectorConfig,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
)
from .const import (
CONF_ENCODING,
CONF_GAIN,
CONF_GENDER,
CONF_KEY_FILE,
CONF_PITCH,
CONF_PROFILES,
CONF_SPEED,
CONF_TEXT_TYPE,
CONF_VOICE,
DEFAULT_GAIN,
DEFAULT_LANG,
DEFAULT_PITCH,
DEFAULT_SPEED,
)
DEFAULT_VOICE = ""
async def async_tts_voices(
client: texttospeech.TextToSpeechAsyncClient,
) -> dict[str, list[str]]:
"""Get TTS voice model names keyed by language."""
voices: dict[str, list[str]] = {}
list_voices_response = await client.list_voices()
for voice in list_voices_response.voices:
language_code = voice.language_codes[0]
if language_code not in voices:
voices[language_code] = []
voices[language_code].append(voice.name)
return voices
def tts_options_schema(
config_options: Mapping[str, Any],
voices: dict[str, list[str]],
from_config_flow: bool = False,
) -> vol.Schema:
"""Return schema for TTS options with default values from config or constants."""
# If we are called from the config flow we want the defaults to be from constants
# to allow clearing the current value (passed as suggested_value) in the UI.
# If we aren't called from the config flow we want the defaults to be from the config.
defaults = {} if from_config_flow else config_options
return vol.Schema(
{
vol.Optional(
CONF_GENDER,
default=defaults.get(
CONF_GENDER,
texttospeech.SsmlVoiceGender.NEUTRAL.name, # type: ignore[attr-defined]
),
): vol.All(
vol.Upper,
SelectSelector(
SelectSelectorConfig(
mode=SelectSelectorMode.DROPDOWN,
options=list(texttospeech.SsmlVoiceGender.__members__),
)
),
),
vol.Optional(
CONF_VOICE,
default=defaults.get(CONF_VOICE, DEFAULT_VOICE),
): SelectSelector(
SelectSelectorConfig(
mode=SelectSelectorMode.DROPDOWN,
options=["", *functools.reduce(operator.iadd, voices.values(), [])],
)
),
vol.Optional(
CONF_ENCODING,
default=defaults.get(
CONF_ENCODING,
texttospeech.AudioEncoding.MP3.name, # type: ignore[attr-defined]
),
): vol.All(
vol.Upper,
SelectSelector(
SelectSelectorConfig(
mode=SelectSelectorMode.DROPDOWN,
options=list(texttospeech.AudioEncoding.__members__),
)
),
),
vol.Optional(
CONF_SPEED,
default=defaults.get(CONF_SPEED, DEFAULT_SPEED),
): NumberSelector(NumberSelectorConfig(min=0.25, max=4.0, step=0.01)),
vol.Optional(
CONF_PITCH,
default=defaults.get(CONF_PITCH, DEFAULT_PITCH),
): NumberSelector(NumberSelectorConfig(min=-20.0, max=20.0, step=0.1)),
vol.Optional(
CONF_GAIN,
default=defaults.get(CONF_GAIN, DEFAULT_GAIN),
): NumberSelector(NumberSelectorConfig(min=-96.0, max=16.0, step=0.1)),
vol.Optional(
CONF_PROFILES,
default=defaults.get(CONF_PROFILES, []),
): SelectSelector(
SelectSelectorConfig(
mode=SelectSelectorMode.DROPDOWN,
options=[
# https://cloud.google.com/text-to-speech/docs/audio-profiles
"wearable-class-device",
"handset-class-device",
"headphone-class-device",
"small-bluetooth-speaker-class-device",
"medium-bluetooth-speaker-class-device",
"large-home-entertainment-class-device",
"large-automotive-class-device",
"telephony-class-application",
],
multiple=True,
sort=False,
)
),
vol.Optional(
CONF_TEXT_TYPE,
default=defaults.get(CONF_TEXT_TYPE, "text"),
): vol.All(
vol.Lower,
SelectSelector(
SelectSelectorConfig(
mode=SelectSelectorMode.DROPDOWN,
options=["text", "ssml"],
)
),
),
}
)
def tts_platform_schema() -> vol.Schema:
"""Return schema for TTS platform."""
return vol.Schema(
{
vol.Optional(CONF_KEY_FILE): cv.string,
vol.Optional(CONF_LANG, default=DEFAULT_LANG): cv.matches_regex(
r"[a-z]{2,3}-[A-Z]{2}|"
),
**tts_options_schema({}, {}).schema,
vol.Optional(CONF_VOICE, default=DEFAULT_VOICE): cv.matches_regex(
r"[a-z]{2,3}-[A-Z]{2}-.*-[A-Z]|"
),
}
)
def validate_service_account_info(info: Mapping[str, str]) -> None:
"""Validate service account info.
Args:
info: The service account info in Google format.
Raises:
ValueError: If the info is not in the expected format.
"""
Credentials.from_service_account_info(info) # type:ignore[no-untyped-call]