Skip to content

Commit 9f2bdca

Browse files
authored
Use unique_id_mismatch in aseko_pool_live reauth (#128339)
1 parent c5046f7 commit 9f2bdca

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

homeassistant/components/aseko_pool_live/config_flow.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AsekoConfigFlow(ConfigFlow, domain=DOMAIN):
2929
}
3030
)
3131

32-
async def get_account_info(self, email: str, password: str) -> dict:
32+
async def get_account_info(self, email: str, password: str) -> dict[str, Any]:
3333
"""Get account info from the mobile API and the web API."""
3434
aseko = Aseko(email, password)
3535
user = await aseko.login()
@@ -70,7 +70,9 @@ async def async_step_user(
7070
async def async_store_credentials(self, info: dict[str, Any]) -> ConfigFlowResult:
7171
"""Store validated credentials."""
7272

73+
await self.async_set_unique_id(info[CONF_UNIQUE_ID])
7374
if self.source == SOURCE_REAUTH:
75+
self._abort_if_unique_id_mismatch()
7476
return self.async_update_reload_and_abort(
7577
self._get_reauth_entry(),
7678
title=info[CONF_EMAIL],
@@ -80,7 +82,6 @@ async def async_store_credentials(self, info: dict[str, Any]) -> ConfigFlowResul
8082
},
8183
)
8284

83-
await self.async_set_unique_id(info[CONF_UNIQUE_ID])
8485
self._abort_if_unique_id_configured()
8586

8687
return self.async_create_entry(

homeassistant/components/aseko_pool_live/strings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"abort": {
2323
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
24-
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
24+
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
25+
"unique_id_mismatch": "The user identifier does not match the previous identifier"
2526
}
2627
},
2728
"entity": {

tests/components/aseko_pool_live/test_config_flow.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ async def test_async_step_reauth_success(hass: HomeAssistant, user: User) -> Non
128128

129129
mock_entry = MockConfigEntry(
130130
domain=DOMAIN,
131-
unique_id="UID",
132-
data={CONF_EMAIL: "[email protected]"},
131+
unique_id="a_user_id",
132+
data={CONF_EMAIL: "[email protected]", CONF_PASSWORD: "passw0rd"},
133+
version=2,
133134
)
134135
mock_entry.add_to_hass(hass)
135136

@@ -151,13 +152,61 @@ async def test_async_step_reauth_success(hass: HomeAssistant, user: User) -> Non
151152
):
152153
result = await hass.config_entries.flow.async_configure(
153154
result["flow_id"],
154-
{CONF_EMAIL: "[email protected]", CONF_PASSWORD: "passw0rd"},
155+
{CONF_EMAIL: "[email protected]", CONF_PASSWORD: "new_password"},
155156
)
156157
await hass.async_block_till_done()
157158

158159
assert result["type"] is FlowResultType.ABORT
159160
assert result["reason"] == "reauth_successful"
160161
assert len(mock_setup_entry.mock_calls) == 1
162+
assert mock_entry.unique_id == "a_user_id"
163+
assert dict(mock_entry.data) == {
164+
CONF_EMAIL: "[email protected]",
165+
CONF_PASSWORD: "new_password",
166+
}
167+
168+
169+
async def test_async_step_reauth_mismatch(hass: HomeAssistant, user: User) -> None:
170+
"""Test mismatch reauthentication."""
171+
172+
mock_entry = MockConfigEntry(
173+
domain=DOMAIN,
174+
unique_id="UID",
175+
data={CONF_EMAIL: "[email protected]", CONF_PASSWORD: "passw0rd"},
176+
version=2,
177+
)
178+
mock_entry.add_to_hass(hass)
179+
180+
result = await mock_entry.start_reauth_flow(hass)
181+
182+
assert result["type"] is FlowResultType.FORM
183+
assert result["step_id"] == "reauth_confirm"
184+
assert result["errors"] == {}
185+
186+
with (
187+
patch(
188+
"homeassistant.components.aseko_pool_live.config_flow.Aseko.login",
189+
return_value=user,
190+
),
191+
patch(
192+
"homeassistant.components.aseko_pool_live.async_setup_entry",
193+
return_value=True,
194+
) as mock_setup_entry,
195+
):
196+
result = await hass.config_entries.flow.async_configure(
197+
result["flow_id"],
198+
{CONF_EMAIL: "[email protected]", CONF_PASSWORD: "new_password"},
199+
)
200+
await hass.async_block_till_done()
201+
202+
assert result["type"] is FlowResultType.ABORT
203+
assert result["reason"] == "unique_id_mismatch"
204+
assert len(mock_setup_entry.mock_calls) == 0
205+
assert mock_entry.unique_id == "UID"
206+
assert dict(mock_entry.data) == {
207+
CONF_EMAIL: "[email protected]",
208+
CONF_PASSWORD: "passw0rd",
209+
}
161210

162211

163212
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)