Add reauthentication to Anthropic (#163019)

This commit is contained in:
Denis Shulyaka
2026-02-14 21:53:25 +01:00
committed by GitHub
parent acaa2aeeee
commit cbcfc43c5a
5 changed files with 99 additions and 16 deletions
@@ -780,6 +780,44 @@ async def test_creating_ai_task_subentry_advanced(
}
async def test_reauth(hass: HomeAssistant) -> None:
"""Test we can reauthenticate."""
# Pretend we already set up a config entry.
hass.config.components.add("anthropic")
mock_config_entry = MockConfigEntry(
domain=DOMAIN,
state=config_entries.ConfigEntryState.LOADED,
)
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
with (
patch(
"homeassistant.components.anthropic.config_flow.anthropic.resources.models.AsyncModels.list",
new_callable=AsyncMock,
),
patch(
"homeassistant.components.anthropic.async_setup_entry",
return_value=True,
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_API_KEY: "new_api_key",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert mock_config_entry.data[CONF_API_KEY] == "new_api_key"
@pytest.mark.parametrize(
("current_llm_apis", "suggested_llm_apis", "expected_options"),
[
+26 -12
View File
@@ -9,11 +9,16 @@ from anthropic import (
AuthenticationError,
BadRequestError,
)
import httpx
from httpx import URL, Request, Response
import pytest
from homeassistant.components.anthropic.const import DATA_REPAIR_DEFER_RELOAD, DOMAIN
from homeassistant.config_entries import ConfigEntryDisabler, ConfigSubentryData
from homeassistant.config_entries import (
ConfigEntryDisabler,
ConfigEntryState,
ConfigSubentryData,
)
from homeassistant.const import CONF_API_KEY, CONF_LLM_HASS_API
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er, llm
@@ -40,17 +45,6 @@ from tests.common import MockConfigEntry
),
"anthropic integration not ready yet: Your credit balance is too low to access the Claude API",
),
(
AuthenticationError(
message="invalid x-api-key",
response=Response(
status_code=401,
request=Request(method="POST", url=URL()),
),
body={"type": "error", "error": {"type": "authentication_error"}},
),
"Invalid API key",
),
],
)
async def test_init_error(
@@ -70,6 +64,26 @@ async def test_init_error(
assert error in caplog.text
async def test_init_auth_error(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test auth error during init errors."""
with patch(
"anthropic.resources.models.AsyncModels.list",
side_effect=AuthenticationError(
response=httpx.Response(
status_code=500, request=httpx.Request(method="GET", url="test")
),
body=None,
message="",
),
):
assert await async_setup_component(hass, "anthropic", {})
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
async def test_deferred_update(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,