Skip to content

Commit d71b124

Browse files
authored
Add DHCP discovery to Aladdin Connect (#151532)
1 parent 4ad664a commit d71b124

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

homeassistant/components/aladdin_connect/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"codeowners": ["@swcloudgenie"],
55
"config_flow": true,
66
"dependencies": ["application_credentials"],
7+
"dhcp": [
8+
{
9+
"hostname": "gdocntl-*"
10+
}
11+
],
712
"documentation": "https://www.home-assistant.io/integrations/aladdin_connect",
813
"integration_type": "hub",
914
"iot_class": "cloud_polling",

homeassistant/components/aladdin_connect/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"reauth_confirm": {
88
"title": "[%key:common::config_flow::title::reauth%]",
99
"description": "Aladdin Connect needs to re-authenticate your account"
10+
},
11+
"oauth_discovery": {
12+
"description": "Home Assistant has found an Aladdin Connect device on your network. Press **Submit** to continue setting up Aladdin Connect."
1013
}
1114
},
1215
"abort": {

homeassistant/generated/dhcp.py

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/aladdin_connect/test_config_flow.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
OAUTH2_AUTHORIZE,
1111
OAUTH2_TOKEN,
1212
)
13+
from homeassistant.config_entries import SOURCE_DHCP
1314
from homeassistant.core import HomeAssistant
1415
from homeassistant.data_entry_flow import FlowResultType
1516
from homeassistant.helpers import config_entry_oauth2_flow
17+
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
1618

1719
from .const import CLIENT_ID, USER_ID
1820

@@ -95,6 +97,79 @@ async def test_full_flow(
9597
assert result["result"].unique_id == USER_ID
9698

9799

100+
@pytest.mark.usefixtures("current_request_with_host")
101+
async def test_full_dhcp_flow(
102+
hass: HomeAssistant,
103+
hass_client_no_auth: ClientSessionGenerator,
104+
aioclient_mock: AiohttpClientMocker,
105+
access_token,
106+
) -> None:
107+
"""Check full flow."""
108+
result = await hass.config_entries.flow.async_init(
109+
DOMAIN,
110+
context={"source": SOURCE_DHCP},
111+
data=DhcpServiceInfo(
112+
ip="192.168.0.123", macaddress="001122334455", hostname="gdocntl-334455"
113+
),
114+
)
115+
116+
assert result["type"] is FlowResultType.FORM
117+
assert result["step_id"] == "oauth_discovery"
118+
assert not result["errors"]
119+
120+
result = await hass.config_entries.flow.async_configure(
121+
result["flow_id"],
122+
{},
123+
)
124+
state = config_entry_oauth2_flow._encode_jwt(
125+
hass,
126+
{
127+
"flow_id": result["flow_id"],
128+
"redirect_uri": "https://example.com/auth/external/callback",
129+
},
130+
)
131+
132+
assert result["url"] == (
133+
f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}"
134+
"&redirect_uri=https://example.com/auth/external/callback"
135+
f"&state={state}"
136+
)
137+
138+
client = await hass_client_no_auth()
139+
resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
140+
assert resp.status == 200
141+
assert resp.headers["content-type"] == "text/html; charset=utf-8"
142+
143+
aioclient_mock.post(
144+
OAUTH2_TOKEN,
145+
json={
146+
"refresh_token": "mock-refresh-token",
147+
"access_token": access_token,
148+
"type": "Bearer",
149+
"expires_in": 60,
150+
},
151+
)
152+
153+
with patch(
154+
"homeassistant.components.aladdin_connect.async_setup_entry", return_value=True
155+
):
156+
result = await hass.config_entries.flow.async_configure(result["flow_id"])
157+
158+
assert result["type"] is FlowResultType.CREATE_ENTRY
159+
assert result["title"] == "Aladdin Connect"
160+
assert result["data"] == {
161+
"auth_implementation": DOMAIN,
162+
"token": {
163+
"access_token": access_token,
164+
"refresh_token": "mock-refresh-token",
165+
"expires_in": 60,
166+
"expires_at": result["data"]["token"]["expires_at"],
167+
"type": "Bearer",
168+
},
169+
}
170+
assert result["result"].unique_id == USER_ID
171+
172+
98173
@pytest.mark.usefixtures("current_request_with_host")
99174
async def test_duplicate_entry(
100175
hass: HomeAssistant,
@@ -146,6 +221,28 @@ async def test_duplicate_entry(
146221
assert result["reason"] == "already_configured"
147222

148223

224+
@pytest.mark.usefixtures("current_request_with_host")
225+
async def test_duplicate_dhcp_entry(
226+
hass: HomeAssistant,
227+
hass_client_no_auth: ClientSessionGenerator,
228+
aioclient_mock: AiohttpClientMocker,
229+
access_token,
230+
mock_config_entry: MockConfigEntry,
231+
) -> None:
232+
"""Check full flow."""
233+
mock_config_entry.add_to_hass(hass)
234+
result = await hass.config_entries.flow.async_init(
235+
DOMAIN,
236+
context={"source": SOURCE_DHCP},
237+
data=DhcpServiceInfo(
238+
ip="192.168.0.123", macaddress="001122334455", hostname="gdocntl-334455"
239+
),
240+
)
241+
242+
assert result["type"] is FlowResultType.ABORT
243+
assert result["reason"] == "already_configured"
244+
245+
149246
@pytest.mark.usefixtures("current_request_with_host")
150247
async def test_flow_reauth(
151248
hass: HomeAssistant,

0 commit comments

Comments
 (0)