File tree Expand file tree Collapse file tree 4 files changed +40
-4
lines changed
homeassistant/components/cloud Expand file tree Collapse file tree 4 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 66from json import JSONDecodeError
77import logging
88
9+ from hass_nabucasa import NabuCasaBaseError
910from hass_nabucasa .llm import (
1011 LLMAuthenticationError ,
1112 LLMError ,
@@ -93,10 +94,11 @@ async def async_setup_entry(
9394 async_add_entities : AddConfigEntryEntitiesCallback ,
9495) -> None :
9596 """Set up Home Assistant Cloud AI Task entity."""
96- cloud = hass .data [DATA_CLOUD ]
97+ if not (cloud := hass .data [DATA_CLOUD ]).is_logged_in :
98+ return
9799 try :
98100 await cloud .llm .async_ensure_token ()
99- except LLMError :
101+ except ( LLMError , NabuCasaBaseError ) :
100102 return
101103
102104 async_add_entities ([CloudLLMTaskEntity (cloud , config_entry )])
Original file line number Diff line number Diff line change 44
55from typing import Literal
66
7+ from hass_nabucasa import NabuCasaBaseError
78from hass_nabucasa .llm import LLMError
89
910from homeassistant .components import conversation
@@ -23,10 +24,11 @@ async def async_setup_entry(
2324 async_add_entities : AddConfigEntryEntitiesCallback ,
2425) -> None :
2526 """Set up the Home Assistant Cloud conversation entity."""
26- cloud = hass .data [DATA_CLOUD ]
27+ if not (cloud := hass .data [DATA_CLOUD ]).is_logged_in :
28+ return
2729 try :
2830 await cloud .llm .async_ensure_token ()
29- except LLMError :
31+ except ( LLMError , NabuCasaBaseError ) :
3032 return
3133
3234 async_add_entities ([CloudConversationEntity (cloud , config_entry )])
Original file line number Diff line number Diff line change 2121from homeassistant .components .cloud .ai_task import (
2222 CloudLLMTaskEntity ,
2323 async_prepare_image_generation_attachments ,
24+ async_setup_entry ,
2425)
26+ from homeassistant .components .cloud .const import DATA_CLOUD
2527from homeassistant .core import HomeAssistant
2628from homeassistant .exceptions import ConfigEntryAuthFailed , HomeAssistantError
2729
@@ -46,6 +48,21 @@ def mock_cloud_ai_task_entity(hass: HomeAssistant) -> CloudLLMTaskEntity:
4648 return entity
4749
4850
51+ async def test_setup_entry_skips_when_not_logged_in (
52+ hass : HomeAssistant ,
53+ ) -> None :
54+ """Test setup_entry exits early when not logged in."""
55+ cloud = MagicMock ()
56+ cloud .is_logged_in = False
57+ entry = MockConfigEntry (domain = "cloud" )
58+ entry .add_to_hass (hass )
59+ hass .data [DATA_CLOUD ] = cloud
60+
61+ async_add_entities = AsyncMock ()
62+ await async_setup_entry (hass , entry , async_add_entities )
63+ async_add_entities .assert_not_called ()
64+
65+
4966@pytest .fixture (name = "mock_handle_chat_log" )
5067def mock_handle_chat_log_fixture () -> AsyncMock :
5168 """Patch the chat log handler."""
Original file line number Diff line number Diff line change @@ -34,6 +34,21 @@ def cloud_conversation_entity(hass: HomeAssistant) -> CloudConversationEntity:
3434 return entity
3535
3636
37+ async def test_setup_entry_skips_when_not_logged_in (
38+ hass : HomeAssistant ,
39+ ) -> None :
40+ """Test setup_entry exits early when not logged in."""
41+ cloud = MagicMock ()
42+ cloud .is_logged_in = False
43+ entry = MockConfigEntry (domain = "cloud" )
44+ entry .add_to_hass (hass )
45+ hass .data [DATA_CLOUD ] = cloud
46+
47+ async_add_entities = AsyncMock ()
48+ await async_setup_entry (hass , entry , async_add_entities )
49+ async_add_entities .assert_not_called ()
50+
51+
3752def test_entity_availability (
3853 cloud_conversation_entity : CloudConversationEntity ,
3954) -> None :
You can’t perform that action at this time.
0 commit comments