Add reconfigure flow to Zonneplan (#180991)

This commit is contained in:
Erwin Douna
2026-09-01 11:28:14 +02:00
committed by GitHub
parent 649efe2659
commit 4c66a9c589
4 changed files with 161 additions and 4 deletions
@@ -13,7 +13,12 @@ from pyzonneplan import (
)
import voluptuous as vol
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import CONF_EMAIL, CONF_TOKEN
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import (
@@ -104,10 +109,15 @@ class ZonneplanConfigFlow(ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown"
else:
await self.async_set_unique_id(account.user_account.uuid)
if self.source == SOURCE_REAUTH:
if self.source in (SOURCE_REAUTH, SOURCE_RECONFIGURE):
self._abort_if_unique_id_mismatch()
entry = (
self._get_reauth_entry()
if self.source == SOURCE_REAUTH
else self._get_reconfigure_entry()
)
return self.async_update_reload_and_abort(
self._get_reauth_entry(),
entry,
data_updates={
CONF_EMAIL: account.user_account.email,
CONF_TOKEN: token.as_dict(),
@@ -155,3 +165,30 @@ class ZonneplanConfigFlow(ConfigFlow, domain=DOMAIN):
errors=errors,
description_placeholders={CONF_EMAIL: email},
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the integration."""
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Request a new OTP for the email of the entry being reconfigured."""
email = self._get_reconfigure_entry().data[CONF_EMAIL]
errors: dict[str, str] | None = None
if user_input is not None and not (
errors := await self._async_request_otp(user_input[CONF_EMAIL])
):
return await self.async_step_otp()
return self.async_show_form(
step_id="reconfigure_confirm",
data_schema=self.add_suggested_values_to_schema(
data_schema=STEP_USER_DATA_SCHEMA,
suggested_values=user_input or {CONF_EMAIL: email},
),
errors=errors,
description_placeholders={CONF_EMAIL: email},
)
@@ -64,7 +64,7 @@ rules:
entity-translations: done
exception-translations: done
icon-translations: done
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues: todo
stale-devices: todo
@@ -3,6 +3,7 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"unique_id_mismatch": "The one-time password was validated for a different Zonneplan account than the one configured."
},
"error": {
@@ -31,6 +32,15 @@
"description": "The stored credentials for {email} are no longer valid. Confirm your email address to receive a new one-time password.",
"title": "[%key:common::config_flow::title::reauth%]"
},
"reconfigure_confirm": {
"data": {
"email": "[%key:common::config_flow::data::email%]"
},
"data_description": {
"email": "[%key:component::zonneplan::config::step::user::data_description::email%]"
},
"description": "Reconfigure the {email} account. Confirm your email address to receive a new one-time password."
},
"user": {
"data": {
"email": "[%key:common::config_flow::data::email%]"
@@ -288,3 +288,113 @@ async def test_reauth_wrong_account(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_zonneplan_client: AsyncMock,
) -> None:
"""Test the reconfigure flow refreshes the stored token."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
assert result["description_placeholders"] == {CONF_EMAIL: MOCK_EMAIL}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=MOCK_USER_INPUT
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "otp"
mock_zonneplan_client.async_request_otp.assert_called_once()
new_token = Token(
access_token="new-access-token",
refresh_token="new-refresh-token",
expires_at=datetime(2031, 1, 1, tzinfo=UTC),
)
mock_zonneplan_client.async_submit_otp.return_value = new_token
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"otp": "123456"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data[CONF_EMAIL] == MOCK_EMAIL
assert mock_config_entry.data[CONF_TOKEN] == new_token.as_dict()
@pytest.mark.parametrize(
("exception", "reason"),
[
pytest.param(ZonneplanConnectionError("offline"), "cannot_connect"),
pytest.param(ZonneplanTimeoutError("timed out"), "timeout_connect"),
pytest.param(Exception("unexpected"), "unknown"),
],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfigure_confirm_exceptions(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_zonneplan_client: AsyncMock,
exception: Exception,
reason: str,
) -> None:
"""Test we handle all reconfigure confirm step exceptions."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
mock_zonneplan_client.async_request_otp.side_effect = exception
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=MOCK_USER_INPUT
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": reason}
mock_zonneplan_client.async_request_otp.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=MOCK_USER_INPUT
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "otp"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"otp": "123456"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reconfigure_wrong_account(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_zonneplan_client: AsyncMock,
) -> None:
"""Test reconfiguring with a different account is aborted."""
mock_config_entry.add_to_hass(hass)
mock_zonneplan_client.async_get_account.return_value = MOCK_OTHER_ACCOUNT
result = await mock_config_entry.start_reconfigure_flow(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=MOCK_USER_INPUT
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"otp": "123456"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"