99 AuthenticationError ,
1010 BadRequestError ,
1111)
12+ import httpx
1213from httpx import URL , Request , Response
1314import pytest
1415
1516from homeassistant .components .anthropic .const import DATA_REPAIR_DEFER_RELOAD , DOMAIN
16- from homeassistant .config_entries import ConfigEntryDisabler , ConfigSubentryData
17+ from homeassistant .config_entries import (
18+ ConfigEntryDisabler ,
19+ ConfigEntryState ,
20+ ConfigSubentryData ,
21+ )
1722from homeassistant .const import CONF_API_KEY , CONF_LLM_HASS_API
1823from homeassistant .core import HomeAssistant
1924from homeassistant .helpers import device_registry as dr , entity_registry as er , llm
4045 ),
4146 "anthropic integration not ready yet: Your credit balance is too low to access the Claude API" ,
4247 ),
43- (
44- AuthenticationError (
45- message = "invalid x-api-key" ,
46- response = Response (
47- status_code = 401 ,
48- request = Request (method = "POST" , url = URL ()),
49- ),
50- body = {"type" : "error" , "error" : {"type" : "authentication_error" }},
51- ),
52- "Invalid API key" ,
53- ),
5448 ],
5549)
5650async def test_init_error (
@@ -70,6 +64,26 @@ async def test_init_error(
7064 assert error in caplog .text
7165
7266
67+ async def test_init_auth_error (
68+ hass : HomeAssistant ,
69+ mock_config_entry : MockConfigEntry ,
70+ ) -> None :
71+ """Test auth error during init errors."""
72+ with patch (
73+ "anthropic.resources.models.AsyncModels.list" ,
74+ side_effect = AuthenticationError (
75+ response = httpx .Response (
76+ status_code = 500 , request = httpx .Request (method = "GET" , url = "test" )
77+ ),
78+ body = None ,
79+ message = "" ,
80+ ),
81+ ):
82+ assert await async_setup_component (hass , "anthropic" , {})
83+ await hass .async_block_till_done ()
84+ assert mock_config_entry .state is ConfigEntryState .SETUP_ERROR
85+
86+
7387async def test_deferred_update (
7488 hass : HomeAssistant ,
7589 mock_config_entry : MockConfigEntry ,
0 commit comments