From 9d557f47b7e6517a99e8a629adab82dae3fa2785 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:29:31 +0200 Subject: [PATCH] Use reconfigure_confirm in lcn config flow (#127217) --- homeassistant/components/lcn/config_flow.py | 37 +++++++++++------ homeassistant/components/lcn/strings.json | 2 +- tests/components/lcn/test_config_flow.py | 45 +++++++++++++-------- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/lcn/config_flow.py b/homeassistant/components/lcn/config_flow.py index a1a98a39db3..d50fc2fd888 100644 --- a/homeassistant/components/lcn/config_flow.py +++ b/homeassistant/components/lcn/config_flow.py @@ -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 {}, ) diff --git a/homeassistant/components/lcn/strings.json b/homeassistant/components/lcn/strings.json index 9b5ce8c9cc0..90650c2aed1 100644 --- a/homeassistant/components/lcn/strings.json +++ b/homeassistant/components/lcn/strings.json @@ -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": { diff --git a/tests/components/lcn/test_config_flow.py b/tests/components/lcn/test_config_flow.py index a34592a4f87..67c10b250a8 100644 --- a/tests/components/lcn/test_config_flow.py +++ b/tests/components/lcn/test_config_flow.py @@ -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