Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hass httpx client for ElevenLabs component #126793

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/elevenlabs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from homeassistant.helpers.httpx_client import get_async_client

from .const import CONF_MODEL

Expand Down Expand Up @@ -41,7 +42,10 @@ class ElevenLabsData:
async def async_setup_entry(hass: HomeAssistant, entry: EleventLabsConfigEntry) -> bool:
"""Set up ElevenLabs text-to-speech from a config entry."""
entry.add_update_listener(update_listener)
client = AsyncElevenLabs(api_key=entry.data[CONF_API_KEY])
httpx_client = get_async_client(hass)
client = AsyncElevenLabs(
api_key=entry.data[CONF_API_KEY], httpx_client=httpx_client
)
model_id = entry.options[CONF_MODEL]
try:
model = await get_model_by_id(client, model_id)
Expand Down
13 changes: 9 additions & 4 deletions homeassistant/components/elevenlabs/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
OptionsFlowWithConfigEntry,
)
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.helpers.selector import (
SelectOptionDict,
SelectSelector,
Expand Down Expand Up @@ -47,9 +49,12 @@
_LOGGER = logging.getLogger(__name__)


async def get_voices_models(api_key: str) -> tuple[dict[str, str], dict[str, str]]:
async def get_voices_models(
hass: HomeAssistant, api_key: str
) -> tuple[dict[str, str], dict[str, str]]:
"""Get available voices and models as dicts."""
client = AsyncElevenLabs(api_key=api_key)
httpx_client = get_async_client(hass)
client = AsyncElevenLabs(api_key=api_key, httpx_client=httpx_client)
voices = (await client.voices.get_all()).voices
models = await client.models.get_all()
voices_dict = {
Expand Down Expand Up @@ -77,7 +82,7 @@ async def async_step_user(
errors: dict[str, str] = {}
if user_input is not None:
try:
voices, _ = await get_voices_models(user_input[CONF_API_KEY])
voices, _ = await get_voices_models(self.hass, user_input[CONF_API_KEY])
except ApiError:
errors["base"] = "invalid_api_key"
else:
Expand Down Expand Up @@ -116,7 +121,7 @@ async def async_step_init(
) -> ConfigFlowResult:
"""Manage the options."""
if not self.voices or not self.models:
self.voices, self.models = await get_voices_models(self.api_key)
self.voices, self.models = await get_voices_models(self.hass, self.api_key)

assert self.models and self.voices

Expand Down