mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Abort reauth flow on unique id mismatch in ista EcoTrend integration (#143430)
This commit is contained in:
parent
357ec7034e
commit
9249ea0dbb
@ -10,6 +10,7 @@ from homeassistant.components.recorder import get_instance
|
||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import IstaConfigEntry, IstaCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -100,8 +100,19 @@ class IstaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
user_input[CONF_PASSWORD],
|
||||
_LOGGER,
|
||||
)
|
||||
|
||||
def get_consumption_units() -> set[str]:
|
||||
ista.login()
|
||||
consumption_units = ista.get_consumption_unit_details()[
|
||||
"consumptionUnits"
|
||||
]
|
||||
return {unit["id"] for unit in consumption_units}
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(ista.login)
|
||||
consumption_units = await self.hass.async_add_executor_job(
|
||||
get_consumption_units
|
||||
)
|
||||
|
||||
except ServerError:
|
||||
errors["base"] = "cannot_connect"
|
||||
except (LoginError, KeycloakError):
|
||||
@ -110,6 +121,8 @@ class IstaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
if reauth_entry.unique_id not in consumption_units:
|
||||
return self.async_abort(reason="unique_id_mismatch")
|
||||
return self.async_update_reload_and_abort(reauth_entry, data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
|
@ -2,7 +2,8 @@
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"unique_id_mismatch": "The login details correspond to a different account. Please re-authenticate to the previously configured account."
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
|
@ -204,3 +204,37 @@ async def test_form__already_configured(
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_ista")
|
||||
async def test_flow_reauth_unique_id_mismatch(hass: HomeAssistant) -> None:
|
||||
"""Test reauth flow unique id mismatch."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_EMAIL: "test@example.com",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
unique_id="42243134-21f6-40a2-a79f-e417a3a12104",
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_EMAIL: "new@example.com",
|
||||
CONF_PASSWORD: "new-password",
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user