Skip to content

Commit

Permalink
Use reconfigure_confirm in lcn config flow (#127217)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Oct 1, 2024
1 parent c654d32 commit 9d557f4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
35 changes: 24 additions & 11 deletions homeassistant/components/lcn/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from __future__ import annotations

from collections.abc import Mapping
import logging
from typing import Any

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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -193,31 +196,41 @@ async def async_step_user(
return self.async_create_entry(title=data[CONF_HOST], data=data)

async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
self, entry_data: Mapping[str, Any]
) -> config_entries.ConfigFlowResult:
"""Reconfigure LCN configuration."""
errors = None
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
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 {},
)
2 changes: 1 addition & 1 deletion homeassistant/components/lcn/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
45 changes: 29 additions & 16 deletions tests/components/lcn/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down

0 comments on commit 9d557f4

Please sign in to comment.