Skip to content

Commit 9e371fd

Browse files
authored
Use hass httpx client for Anthropic (home-assistant#162518)
1 parent 9fa5a84 commit 9e371fd

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

homeassistant/components/anthropic/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
from functools import partial
6-
75
import anthropic
86

97
from homeassistant.config_entries import ConfigEntry, ConfigSubentry
@@ -16,6 +14,7 @@
1614
entity_registry as er,
1715
issue_registry as ir,
1816
)
17+
from homeassistant.helpers.httpx_client import get_async_client
1918
from homeassistant.helpers.typing import ConfigType
2019

2120
from .const import (
@@ -42,8 +41,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4241

4342
async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> bool:
4443
"""Set up Anthropic from a config entry."""
45-
client = await hass.async_add_executor_job(
46-
partial(anthropic.AsyncAnthropic, api_key=entry.data[CONF_API_KEY])
44+
client = anthropic.AsyncAnthropic(
45+
api_key=entry.data[CONF_API_KEY], http_client=get_async_client(hass)
4746
)
4847
try:
4948
await client.models.list(timeout=10.0)

homeassistant/components/anthropic/config_flow.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
from functools import partial
65
import json
76
import logging
87
import re
@@ -30,6 +29,7 @@
3029
)
3130
from homeassistant.core import HomeAssistant, callback
3231
from homeassistant.helpers import llm
32+
from homeassistant.helpers.httpx_client import get_async_client
3333
from homeassistant.helpers.selector import (
3434
NumberSelector,
3535
NumberSelectorConfig,
@@ -89,8 +89,8 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
8989
9090
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
9191
"""
92-
client = await hass.async_add_executor_job(
93-
partial(anthropic.AsyncAnthropic, api_key=data[CONF_API_KEY])
92+
client = anthropic.AsyncAnthropic(
93+
api_key=data[CONF_API_KEY], http_client=get_async_client(hass)
9494
)
9595
await client.models.list(timeout=10.0)
9696

@@ -457,11 +457,9 @@ async def async_step_model(
457457

458458
async def _get_model_list(self) -> list[SelectOptionDict]:
459459
"""Get list of available models."""
460-
client = await self.hass.async_add_executor_job(
461-
partial(
462-
anthropic.AsyncAnthropic,
463-
api_key=self._get_entry().data[CONF_API_KEY],
464-
)
460+
client = anthropic.AsyncAnthropic(
461+
api_key=self._get_entry().data[CONF_API_KEY],
462+
http_client=get_async_client(self.hass),
465463
)
466464
return await get_model_list(client)
467465

@@ -470,11 +468,9 @@ async def _get_location_data(self) -> dict[str, str]:
470468
location_data: dict[str, str] = {}
471469
zone_home = self.hass.states.get(ENTITY_ID_HOME)
472470
if zone_home is not None:
473-
client = await self.hass.async_add_executor_job(
474-
partial(
475-
anthropic.AsyncAnthropic,
476-
api_key=self._get_entry().data[CONF_API_KEY],
477-
)
471+
client = anthropic.AsyncAnthropic(
472+
api_key=self._get_entry().data[CONF_API_KEY],
473+
http_client=get_async_client(self.hass),
478474
)
479475
location_schema = vol.Schema(
480476
{

0 commit comments

Comments
 (0)