Use reconfigure_confirm in lcn config flow (#127217)

pull/127219/head
epenet 2024-10-01 14:29:31 +02:00 committed by GitHub
parent c654d3283e
commit 9d557f47b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 29 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Mapping
import logging
from typing import Any
@ -9,7 +10,7 @@ import pypck
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
from homeassistant.const import (
CONF_BASE,
CONF_DEVICES,
@ -113,6 +114,8 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
MINOR_VERSION = 2
_context_entry: ConfigEntry
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Import existing configuration from LCN."""
# validate the imported connection parameters
@ -193,31 +196,41 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_create_entry(title=data[CONF_HOST], data=data)
async def async_step_reconfigure(
self, entry_data: Mapping[str, Any]
) -> config_entries.ConfigFlowResult:
"""Reconfigure LCN configuration."""
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
assert entry
self._context_entry = entry
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> config_entries.ConfigFlowResult:
"""Reconfigure LCN configuration."""
errors = None
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
assert entry
if user_input is not None:
user_input[CONF_HOST] = entry.data[CONF_HOST]
user_input[CONF_HOST] = self._context_entry.data[CONF_HOST]
await self.hass.config_entries.async_unload(entry.entry_id)
await self.hass.config_entries.async_unload(self._context_entry.entry_id)
if (error := await validate_connection(user_input)) is not None:
errors = {CONF_BASE: error}
if errors is None:
data = entry.data.copy()
data = self._context_entry.data.copy()
data.update(user_input)
self.hass.config_entries.async_update_entry(entry, data=data)
await self.hass.config_entries.async_setup(entry.entry_id)
self.hass.config_entries.async_update_entry(
self._context_entry, data=data
)
await self.hass.config_entries.async_setup(self._context_entry.entry_id)
return self.async_abort(reason="reconfigure_successful")
await self.hass.config_entries.async_setup(entry.entry_id)
await self.hass.config_entries.async_setup(self._context_entry.entry_id)
return self.async_show_form(
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(CONFIG_SCHEMA, entry.data),
step_id="reconfigure_confirm",
data_schema=self.add_suggested_values_to_schema(
CONFIG_SCHEMA, self._context_entry.data
),
errors=errors or {},
)

View File

@ -34,7 +34,7 @@
"acknowledge": "Retry sendig commands if no response is received (increases bus traffic)."
}
},
"reconfigure": {
"reconfigure_confirm": {
"title": "Reconfigure LCN host",
"description": "Reconfigure connection to LCN host.",
"data": {

View File

@ -204,20 +204,26 @@ async def test_step_reconfigure(hass: HomeAssistant, entry: MockConfigEntry) ->
entry.add_to_hass(hass)
old_entry_data = entry.data.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=old_entry_data,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
with (
patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"),
patch("homeassistant.components.lcn.async_setup", return_value=True),
patch("homeassistant.components.lcn.async_setup_entry", return_value=True),
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=CONFIG_DATA.copy(),
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_DATA.copy(),
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
@ -242,18 +248,25 @@ async def test_step_reconfigure_error(
) -> None:
"""Test for error in reconfigure step is handled correctly."""
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=entry.data,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
with patch(
"homeassistant.components.lcn.PchkConnectionManager.async_connect",
side_effect=error,
):
data = {**CONNECTION_DATA, CONF_HOST: "pchk"}
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_RECONFIGURE,
"entry_id": entry.entry_id,
},
data=data,
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG_DATA.copy(),
)
assert result["type"] == data_entry_flow.FlowResultType.FORM