mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use hass httpx client for ElevenLabs component (#126793)
This commit is contained in:
parent
33d0343089
commit
616c0ebaa4
@ -12,6 +12,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.const import CONF_API_KEY, Platform
|
from homeassistant.const import CONF_API_KEY, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryError
|
from homeassistant.exceptions import ConfigEntryError
|
||||||
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
|
|
||||||
from .const import CONF_MODEL
|
from .const import CONF_MODEL
|
||||||
|
|
||||||
@ -41,7 +42,10 @@ type EleventLabsConfigEntry = ConfigEntry[ElevenLabsData]
|
|||||||
async def async_setup_entry(hass: HomeAssistant, entry: EleventLabsConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: EleventLabsConfigEntry) -> bool:
|
||||||
"""Set up ElevenLabs text-to-speech from a config entry."""
|
"""Set up ElevenLabs text-to-speech from a config entry."""
|
||||||
entry.add_update_listener(update_listener)
|
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]
|
model_id = entry.options[CONF_MODEL]
|
||||||
try:
|
try:
|
||||||
model = await get_model_by_id(client, model_id)
|
model = await get_model_by_id(client, model_id)
|
||||||
|
@ -17,6 +17,8 @@ from homeassistant.config_entries import (
|
|||||||
OptionsFlowWithConfigEntry,
|
OptionsFlowWithConfigEntry,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_API_KEY
|
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 (
|
from homeassistant.helpers.selector import (
|
||||||
SelectOptionDict,
|
SelectOptionDict,
|
||||||
SelectSelector,
|
SelectSelector,
|
||||||
@ -47,9 +49,12 @@ USER_STEP_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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."""
|
"""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
|
voices = (await client.voices.get_all()).voices
|
||||||
models = await client.models.get_all()
|
models = await client.models.get_all()
|
||||||
voices_dict = {
|
voices_dict = {
|
||||||
@ -77,7 +82,7 @@ class ElevenLabsConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
try:
|
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:
|
except ApiError:
|
||||||
errors["base"] = "invalid_api_key"
|
errors["base"] = "invalid_api_key"
|
||||||
else:
|
else:
|
||||||
@ -116,7 +121,7 @@ class ElevenLabsOptionsFlow(OptionsFlowWithConfigEntry):
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the options."""
|
"""Manage the options."""
|
||||||
if not self.voices or not self.models:
|
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
|
assert self.models and self.voices
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user