Skip to content

Commit

Permalink
Use hass httpx client for ElevenLabs component (#126793)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorgfresser authored Sep 27, 2024
1 parent 33d0343 commit 616c0eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 616c0eb

Please sign in to comment.