From 7c6b517672330013d9142d25ea989515df500004 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:52:30 +0200 Subject: [PATCH] Use reconfigure helpers in fronius config flow (#128001) * Use reconfigure helpers in fronius * Drop _async_abort_entries_match --- .../components/fronius/config_flow.py | 31 +++++-------------- homeassistant/components/fronius/strings.json | 3 +- tests/components/fronius/test_config_flow.py | 6 ++-- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/fronius/config_flow.py b/homeassistant/components/fronius/config_flow.py index b16f43d58e8..2adbf2ae2f3 100644 --- a/homeassistant/components/fronius/config_flow.py +++ b/homeassistant/components/fronius/config_flow.py @@ -10,7 +10,7 @@ from pyfronius import Fronius, FroniusError import voluptuous as vol from homeassistant.components.dhcp import DhcpServiceInfo -from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -72,7 +72,6 @@ class FroniusConfigFlow(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize flow.""" self.info: FroniusConfigEntryData - self._entry: ConfigEntry | None = None async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -145,6 +144,7 @@ class FroniusConfigFlow(ConfigFlow, domain=DOMAIN): ) -> ConfigFlowResult: """Add reconfigure step to allow to reconfigure a config entry.""" errors = {} + reconfigure_entry = self._get_reconfigure_entry() if user_input is not None: try: @@ -155,33 +155,16 @@ class FroniusConfigFlow(ConfigFlow, domain=DOMAIN): _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: - # Config didn't change or is already configured in another entry - self._async_abort_entries_match(dict(info)) + await self.async_set_unique_id(unique_id) + self._abort_if_unique_id_mismatch() - existing_entry = await self.async_set_unique_id( - unique_id, raise_on_progress=False - ) - assert self._entry is not None - if existing_entry and existing_entry.entry_id != self._entry.entry_id: - # Uid of device is already configured in another entry (but with different host) - self._abort_if_unique_id_configured() + return self.async_update_reload_and_abort(reconfigure_entry, data=info) - return self.async_update_reload_and_abort( - self._entry, - data=info, - reason="reconfigure_successful", - ) - - if self._entry is None: - self._entry = self.hass.config_entries.async_get_entry( - self.context["entry_id"] - ) - assert self._entry is not None - host = self._entry.data[CONF_HOST] + host = reconfigure_entry.data[CONF_HOST] return self.async_show_form( step_id="reconfigure", data_schema=vol.Schema({vol.Required(CONF_HOST, default=host): str}), - description_placeholders={"device": self._entry.title}, + description_placeholders={"device": reconfigure_entry.title}, errors=errors, ) diff --git a/homeassistant/components/fronius/strings.json b/homeassistant/components/fronius/strings.json index 1eaa612a6e7..dfdcfc0ddb2 100644 --- a/homeassistant/components/fronius/strings.json +++ b/homeassistant/components/fronius/strings.json @@ -26,7 +26,8 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "invalid_host": "[%key:common::config_flow::error::invalid_host%]", - "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "unique_id_mismatch": "The identifier does not match the previous identifier" } }, "entity": { diff --git a/tests/components/fronius/test_config_flow.py b/tests/components/fronius/test_config_flow.py index b85aafb2e1e..1b9c41d5aa6 100644 --- a/tests/components/fronius/test_config_flow.py +++ b/tests/components/fronius/test_config_flow.py @@ -344,7 +344,7 @@ async def test_reconfigure(hass: HomeAssistant) -> None: """Test reconfiguring an entry.""" entry = MockConfigEntry( domain=DOMAIN, - unique_id="123.4567890", + unique_id="1234567", data={ CONF_HOST: "10.1.2.3", "is_logger": True, @@ -490,7 +490,7 @@ async def test_reconfigure_already_configured(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "already_configured" + assert result["reason"] == "unique_id_mismatch" assert len(mock_setup_entry.mock_calls) == 0 @@ -531,4 +531,4 @@ async def test_reconfigure_already_existing(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert result2["type"] is FlowResultType.ABORT - assert result2["reason"] == "already_configured" + assert result2["reason"] == "unique_id_mismatch"