Skip to content

Commit 83c4e2a

Browse files
authored
Fix Anthropic init with incorrect model (home-assistant#157421)
1 parent a7dbf55 commit 83c4e2a

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

homeassistant/components/anthropic/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818
from homeassistant.helpers.typing import ConfigType
1919

20-
from .const import CONF_CHAT_MODEL, DEFAULT, DEFAULT_CONVERSATION_NAME, DOMAIN, LOGGER
20+
from .const import DEFAULT_CONVERSATION_NAME, DOMAIN, LOGGER
2121

2222
PLATFORMS = (Platform.AI_TASK, Platform.CONVERSATION)
2323
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
@@ -37,14 +37,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) ->
3737
partial(anthropic.AsyncAnthropic, api_key=entry.data[CONF_API_KEY])
3838
)
3939
try:
40-
# Use model from first conversation subentry for validation
41-
subentries = list(entry.subentries.values())
42-
if subentries:
43-
model_id = subentries[0].data.get(CONF_CHAT_MODEL, DEFAULT[CONF_CHAT_MODEL])
44-
else:
45-
model_id = DEFAULT[CONF_CHAT_MODEL]
46-
model = await client.models.retrieve(model_id=model_id, timeout=10.0)
47-
LOGGER.debug("Anthropic model: %s", model.display_name)
40+
await client.models.list(timeout=10.0)
4841
except anthropic.AuthenticationError as err:
4942
LOGGER.error("Invalid API key: %s", err)
5043
return False

tests/components/anthropic/conftest.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from anthropic.types.raw_message_delta_event import Delta
2121
import pytest
2222

23-
from homeassistant.components.anthropic import CONF_CHAT_MODEL
2423
from homeassistant.components.anthropic.const import (
24+
CONF_CHAT_MODEL,
2525
CONF_WEB_SEARCH,
2626
CONF_WEB_SEARCH_CITY,
2727
CONF_WEB_SEARCH_COUNTRY,
@@ -184,13 +184,10 @@ async def mock_init_component(
184184
),
185185
]
186186
)
187-
with (
188-
patch("anthropic.resources.models.AsyncModels.retrieve"),
189-
patch(
190-
"anthropic.resources.models.AsyncModels.list",
191-
new_callable=AsyncMock,
192-
return_value=model_list,
193-
),
187+
with patch(
188+
"anthropic.resources.models.AsyncModels.list",
189+
new_callable=AsyncMock,
190+
return_value=model_list,
194191
):
195192
assert await async_setup_component(hass, "anthropic", {})
196193
await hass.async_block_till_done()

tests/components/anthropic/test_conversation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def test_template_error(
9999
"prompt": "talk like a {% if True %}smarthome{% else %}pirate please.",
100100
},
101101
)
102-
with patch("anthropic.resources.models.AsyncModels.retrieve"):
102+
with patch("anthropic.resources.models.AsyncModels.list", new_callable=AsyncMock):
103103
await hass.config_entries.async_setup(mock_config_entry.entry_id)
104104
await hass.async_block_till_done()
105105

@@ -138,7 +138,7 @@ async def test_template_variables(
138138
create_content_block(0, ["Okay, let", " me take care of that for you", "."])
139139
]
140140
with (
141-
patch("anthropic.resources.models.AsyncModels.retrieve"),
141+
patch("anthropic.resources.models.AsyncModels.list", new_callable=AsyncMock),
142142
patch("homeassistant.auth.AuthManager.async_get_user", return_value=mock_user),
143143
):
144144
await hass.config_entries.async_setup(mock_config_entry.entry_id)

tests/components/anthropic/test_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def test_init_error(
6262
) -> None:
6363
"""Test initialization errors."""
6464
with patch(
65-
"anthropic.resources.models.AsyncModels.retrieve",
65+
"anthropic.resources.models.AsyncModels.list",
6666
side_effect=side_effect,
6767
):
6868
assert await async_setup_component(hass, "anthropic", {})

0 commit comments

Comments
 (0)