Skip to content

Commit 7711eac

Browse files
Return early when setting cloud ai_task and conversation and not logged in to cloud (home-assistant#157402)
1 parent 32fe53c commit 7711eac

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

homeassistant/components/cloud/ai_task.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from json import JSONDecodeError
77
import logging
88

9+
from hass_nabucasa import NabuCasaBaseError
910
from 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)])

homeassistant/components/cloud/conversation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import Literal
66

7+
from hass_nabucasa import NabuCasaBaseError
78
from hass_nabucasa.llm import LLMError
89

910
from 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)])

tests/components/cloud/test_ai_task.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from 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
2527
from homeassistant.core import HomeAssistant
2628
from 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")
5067
def mock_handle_chat_log_fixture() -> AsyncMock:
5168
"""Patch the chat log handler."""

tests/components/cloud/test_conversation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
3752
def test_entity_availability(
3853
cloud_conversation_entity: CloudConversationEntity,
3954
) -> None:

0 commit comments

Comments
 (0)