1 """Config flow for ElevenLabs text-to-speech integration."""
3 from __future__
import annotations
8 from elevenlabs.client
import AsyncElevenLabs
9 from elevenlabs.core
import ApiError
10 import voluptuous
as vol
30 CONF_OPTIMIZE_LATENCY,
34 CONF_USE_SPEAKER_BOOST,
37 DEFAULT_OPTIMIZE_LATENCY,
41 DEFAULT_USE_SPEAKER_BOOST,
45 USER_STEP_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
48 _LOGGER = logging.getLogger(__name__)
52 hass: HomeAssistant, api_key: str
53 ) -> tuple[dict[str, str], dict[str, str]]:
54 """Get available voices and models as dicts."""
56 client = AsyncElevenLabs(api_key=api_key, httpx_client=httpx_client)
57 voices = (await client.voices.get_all()).voices
58 models = await client.models.get_all()
60 voice.voice_id: voice.name
61 for voice
in sorted(voices, key=
lambda v: v.name
or "")
65 model.model_id: model.name
66 for model
in sorted(models, key=
lambda m: m.name
or "")
67 if model.name
and model.can_do_text_to_speech
69 return voices_dict, models_dict
73 """Handle a config flow for ElevenLabs text-to-speech."""
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle the initial step."""
81 errors: dict[str, str] = {}
82 if user_input
is not None:
86 errors[
"base"] =
"invalid_api_key"
91 options={CONF_MODEL: DEFAULT_MODEL, CONF_VOICE:
list(voices)[0]},
94 step_id=
"user", data_schema=USER_STEP_SCHEMA, errors=errors
99 config_entry: ConfigEntry,
101 """Create the options flow."""
106 """ElevenLabs options flow."""
108 def __init__(self, config_entry: ConfigEntry) ->
None:
109 """Initialize options flow."""
110 self.api_key: str = config_entry.data[CONF_API_KEY]
112 self.voices: dict[str, str] = {}
113 self.
modelsmodels: dict[str, str] = {}
114 self.
modelmodel: str |
None =
None
115 self.
voicevoice: str |
None =
None
118 self, user_input: dict[str, Any] |
None =
None
119 ) -> ConfigFlowResult:
120 """Manage the options."""
121 if not self.voices
or not self.
modelsmodels:
124 assert self.
modelsmodels
and self.voices
126 if user_input
is not None:
127 self.
modelmodel = user_input[CONF_MODEL]
128 self.
voicevoice = user_input[CONF_VOICE]
129 configure_voice = user_input.pop(CONF_CONFIGURE_VOICE)
144 """Elevenlabs options schema."""
154 for model_id, model_name
in self.
modelsmodels.items()
164 for voice_id, voice_name
in self.voices.items()
168 vol.Required(CONF_CONFIGURE_VOICE, default=
False): bool,
175 self, user_input: dict[str, Any] |
None =
None
176 ) -> ConfigFlowResult:
177 """Handle voice settings."""
178 assert self.voices
and self.
modelsmodels
179 if user_input
is not None:
180 user_input[CONF_MODEL] = self.
modelmodel
181 user_input[CONF_VOICE] = self.
voicevoice
187 step_id=
"voice_settings",
192 """Elevenlabs options voice schema."""
198 CONF_STABILITY, DEFAULT_STABILITY
202 vol.Range(min=0, max=1),
207 CONF_SIMILARITY, DEFAULT_SIMILARITY
211 vol.Range(min=0, max=1),
214 CONF_OPTIMIZE_LATENCY,
216 CONF_OPTIMIZE_LATENCY, DEFAULT_OPTIMIZE_LATENCY
218 ): vol.All(int, vol.Range(min=0, max=4)),
224 vol.Range(min=0, max=1),
227 CONF_USE_SPEAKER_BOOST,
229 CONF_USE_SPEAKER_BOOST, DEFAULT_USE_SPEAKER_BOOST
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
vol.Schema elevenlabs_config_options_voice_schema(self)
None __init__(self, ConfigEntry config_entry)
vol.Schema elevenlabs_config_option_schema(self)
ConfigFlowResult async_step_voice_settings(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
tuple[dict[str, str], dict[str, str]] get_voices_models(HomeAssistant hass, str api_key)
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)