From 4ab85e728d9458e42e33629174db2649aef8b5ed Mon Sep 17 00:00:00 2001 From: Simon Sorg Date: Wed, 25 Sep 2024 22:33:38 +0000 Subject: [PATCH] Use hass httpx client for ElevenLabs component --- homeassistant/components/elevenlabs/__init__.py | 6 +++++- homeassistant/components/elevenlabs/config_flow.py | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/elevenlabs/__init__.py b/homeassistant/components/elevenlabs/__init__.py index 99cddd783e25ef..7da4802e98ae74 100644 --- a/homeassistant/components/elevenlabs/__init__.py +++ b/homeassistant/components/elevenlabs/__init__.py @@ -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 @@ -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) diff --git a/homeassistant/components/elevenlabs/config_flow.py b/homeassistant/components/elevenlabs/config_flow.py index 6eec35d0583a35..b596ec05b00c52 100644 --- a/homeassistant/components/elevenlabs/config_flow.py +++ b/homeassistant/components/elevenlabs/config_flow.py @@ -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, @@ -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 = { @@ -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: @@ -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