1 """Support for the ElevenLabs text-to-speech service."""
3 from __future__
import annotations
6 from types
import MappingProxyType
9 from elevenlabs.client
import AsyncElevenLabs
10 from elevenlabs.core
import ApiError
11 from elevenlabs.types
import Model, Voice
as ElevenLabsVoice, VoiceSettings
24 from .
import EleventLabsConfigEntry
26 CONF_OPTIMIZE_LATENCY,
30 CONF_USE_SPEAKER_BOOST,
32 DEFAULT_OPTIMIZE_LATENCY,
36 DEFAULT_USE_SPEAKER_BOOST,
40 _LOGGER = logging.getLogger(__name__)
44 """Return voice settings."""
46 stability=options.get(CONF_STABILITY, DEFAULT_STABILITY),
47 similarity_boost=options.get(CONF_SIMILARITY, DEFAULT_SIMILARITY),
48 style=options.get(CONF_STYLE, DEFAULT_STYLE),
49 use_speaker_boost=options.get(
50 CONF_USE_SPEAKER_BOOST, DEFAULT_USE_SPEAKER_BOOST
57 config_entry: EleventLabsConfigEntry,
58 async_add_entities: AddEntitiesCallback,
60 """Set up ElevenLabs tts platform via config entry."""
61 client = config_entry.runtime_data.client
62 voices = (await client.voices.get_all()).voices
63 default_voice_id = config_entry.options[CONF_VOICE]
69 config_entry.runtime_data.model,
72 config_entry.entry_id,
75 config_entry.options.get(
76 CONF_OPTIMIZE_LATENCY, DEFAULT_OPTIMIZE_LATENCY
84 """The ElevenLabs API entity."""
86 _attr_supported_options = [ATTR_VOICE]
90 client: AsyncElevenLabs,
92 voices: list[ElevenLabsVoice],
93 default_voice_id: str,
96 voice_settings: VoiceSettings,
99 """Init ElevenLabs TTS service."""
104 (Voice(v.voice_id, v.name)
for v
in voices
if v.name),
105 key=
lambda v: v.name,
109 idx
for idx, v
in enumerate(self.
_voices_voices)
if v.voice_id == default_voice_id
112 self.
_voices_voices.insert(0, self.
_voices_voices.pop(voice_indices[0]))
120 identifiers={(DOMAIN, entry_id)},
121 manufacturer=
"ElevenLabs",
123 entry_type=DeviceEntryType.SERVICE,
126 lang.language_id
for lang
in self.
_model_model.languages
or []
131 """Return a list of supported voices for a language."""
135 self, message: str, language: str, options: dict[str, Any]
137 """Load tts audio file from the engine."""
138 _LOGGER.debug(
"Getting TTS audio for %s", message)
139 _LOGGER.debug(
"Options: %s", options)
142 audio = await self.
_client_client.generate(
145 optimize_streaming_latency=self.
_latency_latency,
147 model=self.
_model_model.model_id,
149 bytes_combined = b
"".join([byte_seg async
for byte_seg
in audio])
150 except ApiError
as exc:
152 "Error during processing of TTS request %s", exc, exc_info=
True
155 return "mp3", bytes_combined
None __init__(self, AsyncElevenLabs client, Model model, list[ElevenLabsVoice] voices, str default_voice_id, str entry_id, str title, VoiceSettings voice_settings, int latency=0)
list[Voice] async_get_supported_voices(self, str language)
_attr_supported_languages
TtsAudioType async_get_tts_audio(self, str message, str language, dict[str, Any] options)
list[str] supported_languages(self)
None async_setup_entry(HomeAssistant hass, EleventLabsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
VoiceSettings to_voice_settings(MappingProxyType[str, Any] options)