diff --git a/homeassistant/components/anthropic/__init__.py b/homeassistant/components/anthropic/__init__.py index aa6cf509fa1..84c9054b476 100644 --- a/homeassistant/components/anthropic/__init__.py +++ b/homeassistant/components/anthropic/__init__.py @@ -2,6 +2,8 @@ from __future__ import annotations +from functools import partial + import anthropic from homeassistant.config_entries import ConfigEntry @@ -20,7 +22,9 @@ type AnthropicConfigEntry = ConfigEntry[anthropic.AsyncClient] async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> bool: """Set up Anthropic from a config entry.""" - client = anthropic.AsyncAnthropic(api_key=entry.data[CONF_API_KEY]) + client = await hass.async_add_executor_job( + partial(anthropic.AsyncAnthropic, api_key=entry.data[CONF_API_KEY]) + ) try: await client.messages.create( model="claude-3-haiku-20240307", diff --git a/homeassistant/components/anthropic/config_flow.py b/homeassistant/components/anthropic/config_flow.py index fa43a3c4bcc..63a70f31fea 100644 --- a/homeassistant/components/anthropic/config_flow.py +++ b/homeassistant/components/anthropic/config_flow.py @@ -2,6 +2,7 @@ from __future__ import annotations +from functools import partial import logging from types import MappingProxyType from typing import Any @@ -59,7 +60,9 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None: Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ - client = anthropic.AsyncAnthropic(api_key=data[CONF_API_KEY]) + client = await hass.async_add_executor_job( + partial(anthropic.AsyncAnthropic, api_key=data[CONF_API_KEY]) + ) await client.messages.create( model="claude-3-haiku-20240307", max_tokens=1, diff --git a/tests/components/anthropic/test_conversation.py b/tests/components/anthropic/test_conversation.py index bda9ca32b34..a35df281fb6 100644 --- a/tests/components/anthropic/test_conversation.py +++ b/tests/components/anthropic/test_conversation.py @@ -488,6 +488,7 @@ async def test_unknown_hass_api( CONF_LLM_HASS_API: "non-existing", }, ) + await hass.async_block_till_done() result = await conversation.async_converse( hass, "hello", "1234", Context(), agent_id="conversation.claude"