From 07322c69925dacf66016250e7963d06bb38e2ba8 Mon Sep 17 00:00:00 2001 From: dontinelli <73341522+dontinelli@users.noreply.github.com> Date: Sun, 22 Dec 2024 19:57:34 +0100 Subject: [PATCH] Add reconfigure flow to slide_local (#133669) --- .../components/slide_local/config_flow.py | 41 ++++++++++++++++++- .../components/slide_local/quality_scale.yaml | 4 +- .../components/slide_local/strings.json | 15 ++++++- .../slide_local/test_config_flow.py | 30 ++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/slide_local/config_flow.py b/homeassistant/components/slide_local/config_flow.py index 3ccc89be375..23c509a02dc 100644 --- a/homeassistant/components/slide_local/config_flow.py +++ b/homeassistant/components/slide_local/config_flow.py @@ -103,7 +103,7 @@ class SlideConfigFlow(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle the user step.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: if not (errors := await self.async_test_connection(user_input)): await self.async_set_unique_id(self._mac) @@ -136,6 +136,45 @@ class SlideConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration of the integration.""" + errors: dict[str, str] = {} + + if user_input is not None: + if not (errors := await self.async_test_connection(user_input)): + await self.async_set_unique_id(self._mac) + self._abort_if_unique_id_mismatch( + description_placeholders={CONF_MAC: self._mac} + ) + user_input |= { + CONF_API_VERSION: self._api_version, + } + + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data_updates=user_input, + ) + + entry: SlideConfigEntry = self._get_reconfigure_entry() + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Required(CONF_HOST): str, + } + ), + { + CONF_HOST: entry.data[CONF_HOST], + CONF_PASSWORD: entry.data.get(CONF_PASSWORD, ""), + }, + ), + errors=errors, + ) + async def async_step_zeroconf( self, discovery_info: ZeroconfServiceInfo ) -> ConfigFlowResult: diff --git a/homeassistant/components/slide_local/quality_scale.yaml b/homeassistant/components/slide_local/quality_scale.yaml index 7a2be591927..54dfd87d98c 100644 --- a/homeassistant/components/slide_local/quality_scale.yaml +++ b/homeassistant/components/slide_local/quality_scale.yaml @@ -50,12 +50,12 @@ rules: diagnostics: done exception-translations: done icon-translations: done - reconfiguration-flow: todo + reconfiguration-flow: done dynamic-devices: status: exempt comment: | Slide_local represents a single physical device, no dynamic changes of devices possible (besides removal of instance itself). - discovery-update-info: todo + discovery-update-info: done repair-issues: todo docs-use-cases: done docs-supported-devices: done diff --git a/homeassistant/components/slide_local/strings.json b/homeassistant/components/slide_local/strings.json index 6aeda9f92fd..b5fe88255a7 100644 --- a/homeassistant/components/slide_local/strings.json +++ b/homeassistant/components/slide_local/strings.json @@ -12,6 +12,17 @@ "password": "The device code of your Slide (inside of the Slide or in the box, length is 8 characters). If your Slide runs firmware version 2 this is optional, as it is not used by the local API." } }, + "reconfigure": { + "description": "Reconfigure the information for your Slide device", + "data": { + "host": "[%key:common::config_flow::data::host%]", + "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "host": "[%key:component::slide_local::config::step::user::data_description::host%]", + "password": "[%key:component::slide_local::config::step::user::data_description::password%]" + } + }, "zeroconf_confirm": { "title": "Confirm setup for Slide", "description": "Do you want to setup {host}?" @@ -19,7 +30,9 @@ }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", - "discovery_connection_failed": "The setup of the discovered device failed with the following error: {error}. Please try to set it up manually." + "discovery_connection_failed": "The setup of the discovered device failed with the following error: {error}. Please try to set it up manually.", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "unique_id_mismatch": "The mac address of the device ({mac}) does not match the previous mac address." }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", diff --git a/tests/components/slide_local/test_config_flow.py b/tests/components/slide_local/test_config_flow.py index 48be7dd7850..9f2923988ca 100644 --- a/tests/components/slide_local/test_config_flow.py +++ b/tests/components/slide_local/test_config_flow.py @@ -282,6 +282,36 @@ async def test_abort_if_already_setup( assert result["reason"] == "already_configured" +async def test_reconfigure( + hass: HomeAssistant, + mock_slide_api: AsyncMock, + mock_config_entry: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test reconfigure flow options.""" + + 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" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "127.0.0.3", + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert len(mock_setup_entry.mock_calls) == 1 + + entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id) + assert entry + assert entry.data[CONF_HOST] == "127.0.0.3" + + async def test_zeroconf( hass: HomeAssistant, mock_slide_api: AsyncMock, mock_setup_entry: AsyncMock ) -> None: