diff --git a/homeassistant/components/anthropic/entity.py b/homeassistant/components/anthropic/entity.py index 62f39bd4a02c..658267219e35 100644 --- a/homeassistant/components/anthropic/entity.py +++ b/homeassistant/components/anthropic/entity.py @@ -858,6 +858,11 @@ class AnthropicBaseLLMEntity(Entity): ] ) messages.extend(new_messages) + except anthropic.AuthenticationError as err: + self.entry.async_start_reauth(self.hass) + raise HomeAssistantError( + "Authentication error with Anthropic API, reauthentication required" + ) from err except anthropic.AnthropicError as err: raise HomeAssistantError( f"Sorry, I had a problem talking to Anthropic: {err}" diff --git a/tests/components/anthropic/test_conversation.py b/tests/components/anthropic/test_conversation.py index b3aa18265817..8c3327b6cc96 100644 --- a/tests/components/anthropic/test_conversation.py +++ b/tests/components/anthropic/test_conversation.py @@ -4,7 +4,7 @@ import datetime from typing import Any from unittest.mock import AsyncMock, Mock, patch -from anthropic import RateLimitError +from anthropic import AuthenticationError, RateLimitError from anthropic.types import ( CitationsWebSearchResultLocation, CitationWebSearchResultLocationParam, @@ -36,8 +36,10 @@ from homeassistant.components.anthropic.const import ( CONF_WEB_SEARCH_REGION, CONF_WEB_SEARCH_TIMEZONE, CONF_WEB_SEARCH_USER_LOCATION, + DOMAIN, ) from homeassistant.components.anthropic.entity import CitationDetails, ContentDetails +from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.const import CONF_LLM_HASS_API from homeassistant.core import Context, HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -107,7 +109,7 @@ async def test_error_handling( mock_init_component, mock_create_stream: AsyncMock, ) -> None: - """Test that the default prompt works.""" + """Test error handling.""" mock_create_stream.side_effect = RateLimitError( message=None, response=Response(status_code=429, request=Request(method="POST", url=URL())), @@ -122,6 +124,38 @@ async def test_error_handling( assert result.response.error_code == "unknown", result +async def test_auth_error_handling( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_init_component, + mock_create_stream: AsyncMock, +) -> None: + """Test reauth after authentication error during conversation.""" + mock_create_stream.side_effect = AuthenticationError( + message="Invalid API key", + response=Response(status_code=403, request=Request(method="POST", url=URL())), + body=None, + ) + + result = await conversation.async_converse( + hass, "hello", None, Context(), agent_id="conversation.claude_conversation" + ) + + assert result.response.response_type == intent.IntentResponseType.ERROR + assert result.response.error_code == "unknown", result + + await hass.async_block_till_done() + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + flow = flows[0] + assert flow["step_id"] == "reauth_confirm" + assert flow["handler"] == DOMAIN + assert "context" in flow + assert flow["context"]["source"] == SOURCE_REAUTH + assert flow["context"]["entry_id"] == mock_config_entry.entry_id + + async def test_template_error( hass: HomeAssistant, mock_config_entry: MockConfigEntry,