2024-07-31 19:31:09 +00:00
|
|
|
"""The ElevenLabs text-to-speech integration."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2024-12-15 11:19:25 +00:00
|
|
|
from elevenlabs import AsyncElevenLabs, Model
|
2024-07-31 19:31:09 +00:00
|
|
|
from elevenlabs.core import ApiError
|
2024-12-29 13:26:59 +00:00
|
|
|
from httpx import ConnectError
|
2024-07-31 19:31:09 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_API_KEY, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-12-29 13:26:59 +00:00
|
|
|
from homeassistant.exceptions import (
|
|
|
|
ConfigEntryAuthFailed,
|
|
|
|
ConfigEntryError,
|
|
|
|
ConfigEntryNotReady,
|
|
|
|
)
|
2024-09-27 17:04:31 +00:00
|
|
|
from homeassistant.helpers.httpx_client import get_async_client
|
2024-07-31 19:31:09 +00:00
|
|
|
|
|
|
|
from .const import CONF_MODEL
|
|
|
|
|
|
|
|
PLATFORMS: list[Platform] = [Platform.TTS]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_model_by_id(client: AsyncElevenLabs, model_id: str) -> Model | None:
|
|
|
|
"""Get ElevenLabs model from their API by the model_id."""
|
|
|
|
models = await client.models.get_all()
|
|
|
|
for maybe_model in models:
|
|
|
|
if maybe_model.model_id == model_id:
|
|
|
|
return maybe_model
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(kw_only=True, slots=True)
|
|
|
|
class ElevenLabsData:
|
|
|
|
"""ElevenLabs data type."""
|
|
|
|
|
|
|
|
client: AsyncElevenLabs
|
|
|
|
model: Model
|
|
|
|
|
|
|
|
|
2024-12-22 18:26:15 +00:00
|
|
|
type ElevenLabsConfigEntry = ConfigEntry[ElevenLabsData]
|
2024-07-31 19:31:09 +00:00
|
|
|
|
|
|
|
|
2024-12-22 18:26:15 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ElevenLabsConfigEntry) -> bool:
|
2024-07-31 19:31:09 +00:00
|
|
|
"""Set up ElevenLabs text-to-speech from a config entry."""
|
|
|
|
entry.add_update_listener(update_listener)
|
2024-09-27 17:04:31 +00:00
|
|
|
httpx_client = get_async_client(hass)
|
|
|
|
client = AsyncElevenLabs(
|
|
|
|
api_key=entry.data[CONF_API_KEY], httpx_client=httpx_client
|
|
|
|
)
|
2024-07-31 19:31:09 +00:00
|
|
|
model_id = entry.options[CONF_MODEL]
|
|
|
|
try:
|
|
|
|
model = await get_model_by_id(client, model_id)
|
2024-12-29 13:26:59 +00:00
|
|
|
except ConnectError as err:
|
|
|
|
raise ConfigEntryNotReady("Failed to connect") from err
|
2024-07-31 19:31:09 +00:00
|
|
|
except ApiError as err:
|
2024-12-16 18:18:09 +00:00
|
|
|
raise ConfigEntryAuthFailed("Auth failed") from err
|
2024-07-31 19:31:09 +00:00
|
|
|
|
|
|
|
if model is None or (not model.languages):
|
|
|
|
raise ConfigEntryError("Model could not be resolved")
|
|
|
|
|
|
|
|
entry.runtime_data = ElevenLabsData(client=client, model=model)
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-12-22 18:26:15 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ElevenLabsConfigEntry) -> bool:
|
2024-07-31 19:31:09 +00:00
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
|
|
|
|
|
|
|
async def update_listener(
|
2024-12-22 18:26:15 +00:00
|
|
|
hass: HomeAssistant, config_entry: ElevenLabsConfigEntry
|
2024-07-31 19:31:09 +00:00
|
|
|
) -> None:
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|