Skip to content

Commit bd10f6e

Browse files
joostlekballoobCopilot
authored andcommitted
Require cloud for Aladdin Connect (home-assistant#153278)
Co-authored-by: Paulus Schoutsen <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent ed9cfb4 commit bd10f6e

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

homeassistant/components/aladdin_connect/config_flow.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ class OAuth2FlowHandler(
2222
VERSION = CONFIG_FLOW_VERSION
2323
MINOR_VERSION = CONFIG_FLOW_MINOR_VERSION
2424

25+
async def async_step_user(
26+
self, user_input: dict[str, Any] | None = None
27+
) -> ConfigFlowResult:
28+
"""Check we have the cloud integration set up."""
29+
if "cloud" not in self.hass.config.components:
30+
return self.async_abort(
31+
reason="cloud_not_enabled",
32+
description_placeholders={"default_config": "default_config"},
33+
)
34+
return await super().async_step_user(user_input)
35+
2536
async def async_step_reauth(
2637
self, user_input: Mapping[str, Any]
2738
) -> ConfigFlowResult:

homeassistant/components/aladdin_connect/strings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]",
2525
"user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]",
2626
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
27-
"wrong_account": "You are authenticated with a different account than the one set up. Please authenticate with the configured account."
27+
"wrong_account": "You are authenticated with a different account than the one set up. Please authenticate with the configured account.",
28+
"cloud_not_enabled": "Please make sure you run Home Assistant with `{default_config}` enabled in your configuration.yaml."
2829
},
2930
"create_entry": {
3031
"default": "[%key:common::config_flow::create_entry::authenticated%]"

tests/components/aladdin_connect/test_config_flow.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
OAUTH2_AUTHORIZE,
1111
OAUTH2_TOKEN,
1212
)
13-
from homeassistant.config_entries import SOURCE_DHCP
13+
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
1414
from homeassistant.core import HomeAssistant
1515
from homeassistant.data_entry_flow import FlowResultType
1616
from homeassistant.helpers import config_entry_oauth2_flow
@@ -23,6 +23,12 @@
2323
from tests.typing import ClientSessionGenerator
2424

2525

26+
@pytest.fixture
27+
def use_cloud(hass: HomeAssistant) -> None:
28+
"""Set up the cloud component."""
29+
hass.config.components.add("cloud")
30+
31+
2632
@pytest.fixture
2733
async def access_token(hass: HomeAssistant) -> str:
2834
"""Return a valid access token with sub field for unique ID."""
@@ -37,7 +43,7 @@ async def access_token(hass: HomeAssistant) -> str:
3743
)
3844

3945

40-
@pytest.mark.usefixtures("current_request_with_host")
46+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
4147
async def test_full_flow(
4248
hass: HomeAssistant,
4349
hass_client_no_auth: ClientSessionGenerator,
@@ -97,7 +103,7 @@ async def test_full_flow(
97103
assert result["result"].unique_id == USER_ID
98104

99105

100-
@pytest.mark.usefixtures("current_request_with_host")
106+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
101107
async def test_full_dhcp_flow(
102108
hass: HomeAssistant,
103109
hass_client_no_auth: ClientSessionGenerator,
@@ -170,7 +176,7 @@ async def test_full_dhcp_flow(
170176
assert result["result"].unique_id == USER_ID
171177

172178

173-
@pytest.mark.usefixtures("current_request_with_host")
179+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
174180
async def test_duplicate_entry(
175181
hass: HomeAssistant,
176182
hass_client_no_auth: ClientSessionGenerator,
@@ -221,7 +227,7 @@ async def test_duplicate_entry(
221227
assert result["reason"] == "already_configured"
222228

223229

224-
@pytest.mark.usefixtures("current_request_with_host")
230+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
225231
async def test_duplicate_dhcp_entry(
226232
hass: HomeAssistant,
227233
hass_client_no_auth: ClientSessionGenerator,
@@ -243,7 +249,7 @@ async def test_duplicate_dhcp_entry(
243249
assert result["reason"] == "already_configured"
244250

245251

246-
@pytest.mark.usefixtures("current_request_with_host")
252+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
247253
async def test_flow_reauth(
248254
hass: HomeAssistant,
249255
hass_client_no_auth: ClientSessionGenerator,
@@ -306,7 +312,7 @@ async def test_flow_reauth(
306312
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
307313

308314

309-
@pytest.mark.usefixtures("current_request_with_host")
315+
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
310316
async def test_flow_wrong_account_reauth(
311317
hass: HomeAssistant,
312318
hass_client_no_auth: ClientSessionGenerator,
@@ -370,3 +376,39 @@ async def test_flow_wrong_account_reauth(
370376
# Should abort with wrong account
371377
assert result["type"] == "abort"
372378
assert result["reason"] == "wrong_account"
379+
380+
381+
@pytest.mark.usefixtures("current_request_with_host")
382+
async def test_no_cloud(
383+
hass: HomeAssistant,
384+
hass_client_no_auth: ClientSessionGenerator,
385+
aioclient_mock: AiohttpClientMocker,
386+
) -> None:
387+
"""Check we abort when cloud is not enabled."""
388+
result = await hass.config_entries.flow.async_init(
389+
DOMAIN, context={"source": SOURCE_USER}
390+
)
391+
392+
assert result["type"] is FlowResultType.ABORT
393+
assert result["reason"] == "cloud_not_enabled"
394+
395+
396+
@pytest.mark.usefixtures("current_request_with_host")
397+
async def test_reauthentication_no_cloud(
398+
hass: HomeAssistant,
399+
hass_client_no_auth: ClientSessionGenerator,
400+
aioclient_mock: AiohttpClientMocker,
401+
mock_config_entry: MockConfigEntry,
402+
) -> None:
403+
"""Test Aladdin Connect reauthentication without cloud."""
404+
mock_config_entry.add_to_hass(hass)
405+
406+
result = await mock_config_entry.start_reauth_flow(hass)
407+
408+
assert result["type"] is FlowResultType.FORM
409+
assert result["step_id"] == "reauth_confirm"
410+
411+
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
412+
413+
assert result["type"] is FlowResultType.ABORT
414+
assert result["reason"] == "cloud_not_enabled"

0 commit comments

Comments
 (0)