diff --git a/homeassistant/components/axis/config_flow.py b/homeassistant/components/axis/config_flow.py index 38b5f5f7422..30bc653c202 100644 --- a/homeassistant/components/axis/config_flow.py +++ b/homeassistant/components/axis/config_flow.py @@ -146,6 +146,14 @@ class AxisFlowHandler(ConfigFlow, domain=AXIS_DOMAIN): title = f"{model} - {serial}" return self.async_create_entry(title=title, data=self.config) + async def async_step_reconfigure( + self, user_input: Mapping[str, Any] | None = None + ) -> ConfigFlowResult: + """Trigger a reconfiguration flow.""" + entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) + assert entry + return await self._redo_configuration(entry.data, keep_password=True) + async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: @@ -154,14 +162,22 @@ class AxisFlowHandler(ConfigFlow, domain=AXIS_DOMAIN): CONF_NAME: entry_data[CONF_NAME], CONF_HOST: entry_data[CONF_HOST], } + return await self._redo_configuration(entry_data, keep_password=False) + async def _redo_configuration( + self, entry_data: Mapping[str, Any], keep_password: bool + ) -> ConfigFlowResult: + """Re-run configuration step.""" self.discovery_schema = { vol.Required( - CONF_PROTOCOL, default=entry_data.get(CONF_PROTOCOL, DEFAULT_PROTOCOL) + CONF_PROTOCOL, default=entry_data.get(CONF_PROTOCOL, "http") ): str, vol.Required(CONF_HOST, default=entry_data[CONF_HOST]): str, vol.Required(CONF_USERNAME, default=entry_data[CONF_USERNAME]): str, - vol.Required(CONF_PASSWORD): str, + vol.Required( + CONF_PASSWORD, + default=entry_data[CONF_PASSWORD] if keep_password else "", + ): str, vol.Required(CONF_PORT, default=entry_data[CONF_PORT]): int, } diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index e0514250a98..a6a0235b118 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -18,6 +18,7 @@ from homeassistant.config_entries import ( SOURCE_DHCP, SOURCE_IGNORE, SOURCE_REAUTH, + SOURCE_RECONFIGURE, SOURCE_SSDP, SOURCE_USER, SOURCE_ZEROCONF, @@ -259,6 +260,44 @@ async def test_reauth_flow_update_configuration( assert mock_config_entry.data[CONF_PASSWORD] == "pass2" +async def test_reconfiguration_flow_update_configuration( + hass: HomeAssistant, mock_config_entry, mock_vapix_requests +) -> None: + """Test that config flow reconfiguration updates configured device.""" + assert mock_config_entry.data[CONF_HOST] == "1.2.3.4" + assert mock_config_entry.data[CONF_USERNAME] == "root" + assert mock_config_entry.data[CONF_PASSWORD] == "pass" + + result = await hass.config_entries.flow.async_init( + AXIS_DOMAIN, + context={ + "source": SOURCE_RECONFIGURE, + "entry_id": mock_config_entry.entry_id, + }, + ) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "user" + + mock_vapix_requests("2.3.4.5") + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_HOST: "2.3.4.5", + CONF_USERNAME: "user", + }, + ) + await hass.async_block_till_done() + + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "already_configured" + assert mock_config_entry.data[CONF_PROTOCOL] == "http" + assert mock_config_entry.data[CONF_HOST] == "2.3.4.5" + assert mock_config_entry.data[CONF_PORT] == 80 + assert mock_config_entry.data[CONF_USERNAME] == "user" + assert mock_config_entry.data[CONF_PASSWORD] == "pass" + + @pytest.mark.parametrize( ("source", "discovery_info"), [