diff --git a/homeassistant/components/anthropic/__init__.py b/homeassistant/components/anthropic/__init__.py index dae88bbcb15..ac4e3522c26 100644 --- a/homeassistant/components/anthropic/__init__.py +++ b/homeassistant/components/anthropic/__init__.py @@ -17,7 +17,7 @@ from homeassistant.helpers import ( ) from homeassistant.helpers.typing import ConfigType -from .const import CONF_CHAT_MODEL, DEFAULT, DEFAULT_CONVERSATION_NAME, DOMAIN, LOGGER +from .const import DEFAULT_CONVERSATION_NAME, DOMAIN, LOGGER PLATFORMS = (Platform.AI_TASK, Platform.CONVERSATION) CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) @@ -37,14 +37,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> partial(anthropic.AsyncAnthropic, api_key=entry.data[CONF_API_KEY]) ) try: - # Use model from first conversation subentry for validation - subentries = list(entry.subentries.values()) - if subentries: - model_id = subentries[0].data.get(CONF_CHAT_MODEL, DEFAULT[CONF_CHAT_MODEL]) - else: - model_id = DEFAULT[CONF_CHAT_MODEL] - model = await client.models.retrieve(model_id=model_id, timeout=10.0) - LOGGER.debug("Anthropic model: %s", model.display_name) + await client.models.list(timeout=10.0) except anthropic.AuthenticationError as err: LOGGER.error("Invalid API key: %s", err) return False diff --git a/tests/components/anthropic/conftest.py b/tests/components/anthropic/conftest.py index 207e1d410e6..5b40b1d0c38 100644 --- a/tests/components/anthropic/conftest.py +++ b/tests/components/anthropic/conftest.py @@ -20,8 +20,8 @@ from anthropic.types import ( from anthropic.types.raw_message_delta_event import Delta import pytest -from homeassistant.components.anthropic import CONF_CHAT_MODEL from homeassistant.components.anthropic.const import ( + CONF_CHAT_MODEL, CONF_WEB_SEARCH, CONF_WEB_SEARCH_CITY, CONF_WEB_SEARCH_COUNTRY, @@ -184,13 +184,10 @@ async def mock_init_component( ), ] ) - with ( - patch("anthropic.resources.models.AsyncModels.retrieve"), - patch( - "anthropic.resources.models.AsyncModels.list", - new_callable=AsyncMock, - return_value=model_list, - ), + with patch( + "anthropic.resources.models.AsyncModels.list", + new_callable=AsyncMock, + return_value=model_list, ): assert await async_setup_component(hass, "anthropic", {}) await hass.async_block_till_done() diff --git a/tests/components/anthropic/test_conversation.py b/tests/components/anthropic/test_conversation.py index b259b988424..cf5a3d17c82 100644 --- a/tests/components/anthropic/test_conversation.py +++ b/tests/components/anthropic/test_conversation.py @@ -99,7 +99,7 @@ async def test_template_error( "prompt": "talk like a {% if True %}smarthome{% else %}pirate please.", }, ) - with patch("anthropic.resources.models.AsyncModels.retrieve"): + with patch("anthropic.resources.models.AsyncModels.list", new_callable=AsyncMock): await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() @@ -138,7 +138,7 @@ async def test_template_variables( create_content_block(0, ["Okay, let", " me take care of that for you", "."]) ] with ( - patch("anthropic.resources.models.AsyncModels.retrieve"), + patch("anthropic.resources.models.AsyncModels.list", new_callable=AsyncMock), patch("homeassistant.auth.AuthManager.async_get_user", return_value=mock_user), ): await hass.config_entries.async_setup(mock_config_entry.entry_id) diff --git a/tests/components/anthropic/test_init.py b/tests/components/anthropic/test_init.py index a97a3b7a378..10626d24fc6 100644 --- a/tests/components/anthropic/test_init.py +++ b/tests/components/anthropic/test_init.py @@ -62,7 +62,7 @@ async def test_init_error( ) -> None: """Test initialization errors.""" with patch( - "anthropic.resources.models.AsyncModels.retrieve", + "anthropic.resources.models.AsyncModels.list", side_effect=side_effect, ): assert await async_setup_component(hass, "anthropic", {})