diff --git a/homeassistant/components/keenetic_ndms2/config_flow.py b/homeassistant/components/keenetic_ndms2/config_flow.py index 3862d34398f..c6095968c07 100644 --- a/homeassistant/components/keenetic_ndms2/config_flow.py +++ b/homeassistant/components/keenetic_ndms2/config_flow.py @@ -8,7 +8,12 @@ from urllib.parse import urlparse from ndms2_client import Client, ConnectionException, InterfaceInfo, TelnetConnection import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow +from homeassistant.config_entries import ( + SOURCE_RECONFIGURE, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -45,7 +50,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - host: str | bytes | None = None + _host: str | bytes | None = None @staticmethod @callback @@ -61,8 +66,9 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a flow initialized by the user.""" errors = {} if user_input is not None: - host = self.host or user_input[CONF_HOST] - self._async_abort_entries_match({CONF_HOST: host}) + host = self._host or user_input[CONF_HOST] + if self.source != SOURCE_RECONFIGURE: + self._async_abort_entries_match({CONF_HOST: host}) _client = Client( TelnetConnection( @@ -81,12 +87,17 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): except ConnectionException: errors["base"] = "cannot_connect" else: + if self.source == SOURCE_RECONFIGURE: + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data={CONF_HOST: host, **user_input}, + ) return self.async_create_entry( title=router_info.name, data={CONF_HOST: host, **user_input} ) host_schema: VolDictType = ( - {vol.Required(CONF_HOST): str} if not self.host else {} + {vol.Required(CONF_HOST): str} if not self._host else {} ) return self.async_show_form( @@ -102,6 +113,15 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): errors=errors, ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration.""" + existing_entry_data = dict(self._get_reconfigure_entry().data) + self._host = existing_entry_data[CONF_HOST] + + return await self.async_step_user(user_input) + async def async_step_ssdp( self, discovery_info: SsdpServiceInfo ) -> ConfigFlowResult: @@ -124,7 +144,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): self._async_abort_entries_match({CONF_HOST: host}) - self.host = host + self._host = host self.context["title_placeholders"] = { "name": friendly_name, "host": host, diff --git a/homeassistant/components/keenetic_ndms2/strings.json b/homeassistant/components/keenetic_ndms2/strings.json index 93b59be122d..3098996d48f 100644 --- a/homeassistant/components/keenetic_ndms2/strings.json +++ b/homeassistant/components/keenetic_ndms2/strings.json @@ -21,7 +21,8 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", "no_udn": "SSDP discovery info has no UDN", - "not_keenetic_ndms2": "Discovered device is not a Keenetic router" + "not_keenetic_ndms2": "Discovered device is not a Keenetic router", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" } }, "options": { diff --git a/tests/components/keenetic_ndms2/__init__.py b/tests/components/keenetic_ndms2/__init__.py index dc0c89e8ea6..dc812af6d01 100644 --- a/tests/components/keenetic_ndms2/__init__.py +++ b/tests/components/keenetic_ndms2/__init__.py @@ -25,6 +25,12 @@ MOCK_DATA = { CONF_PORT: 23, } +MOCK_RECONFIGURE = { + CONF_USERNAME: "user1", + CONF_PASSWORD: "pass1", + CONF_PORT: 123, +} + MOCK_OPTIONS = { CONF_SCAN_INTERVAL: 15, const.CONF_CONSIDER_HOME: 150, diff --git a/tests/components/keenetic_ndms2/test_config_flow.py b/tests/components/keenetic_ndms2/test_config_flow.py index 3293bd3d4da..1b86e6c265c 100644 --- a/tests/components/keenetic_ndms2/test_config_flow.py +++ b/tests/components/keenetic_ndms2/test_config_flow.py @@ -19,7 +19,14 @@ from homeassistant.helpers.service_info.ssdp import ( ATTR_UPNP_UDN, ) -from . import MOCK_DATA, MOCK_NAME, MOCK_OPTIONS, MOCK_SSDP_DISCOVERY_INFO +from . import ( + MOCK_DATA, + MOCK_IP, + MOCK_NAME, + MOCK_OPTIONS, + MOCK_RECONFIGURE, + MOCK_SSDP_DISCOVERY_INFO, +) from tests.common import MockConfigEntry @@ -75,6 +82,34 @@ async def test_flow_works(hass: HomeAssistant, connect) -> None: assert len(mock_setup_entry.mock_calls) == 1 +async def test_reconfigure(hass: HomeAssistant, connect) -> None: + """Test reconfigure flow.""" + entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA) + entry.add_to_hass(hass) + + result = await entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + with patch( + "homeassistant.components.keenetic_ndms2.async_setup_entry", return_value=True + ) as mock_setup_entry: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=MOCK_RECONFIGURE, + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reconfigure_successful" + assert entry.data == { + CONF_HOST: MOCK_IP, + **MOCK_RECONFIGURE, + } + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_options(hass: HomeAssistant) -> None: """Test updating options.""" entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA)