diff --git a/homeassistant/components/axis/config_flow.py b/homeassistant/components/axis/config_flow.py index 0434ed71a22..84d9880b7f8 100644 --- a/homeassistant/components/axis/config_flow.py +++ b/homeassistant/components/axis/config_flow.py @@ -13,6 +13,8 @@ import voluptuous as vol from homeassistant.components import dhcp, ssdp, zeroconf from homeassistant.config_entries import ( SOURCE_IGNORE, + SOURCE_REAUTH, + SOURCE_RECONFIGURE, ConfigEntry, ConfigFlow, ConfigFlowResult, @@ -87,27 +89,30 @@ class AxisFlowHandler(ConfigFlow, domain=AXIS_DOMAIN): else: serial = api.vapix.serial_number - await self.async_set_unique_id(format_mac(serial)) - - self._abort_if_unique_id_configured( - updates={ - CONF_PROTOCOL: user_input[CONF_PROTOCOL], - CONF_HOST: user_input[CONF_HOST], - CONF_PORT: user_input[CONF_PORT], - CONF_USERNAME: user_input[CONF_USERNAME], - CONF_PASSWORD: user_input[CONF_PASSWORD], - } - ) - - self.config = { + config = { CONF_PROTOCOL: user_input[CONF_PROTOCOL], CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT], CONF_USERNAME: user_input[CONF_USERNAME], CONF_PASSWORD: user_input[CONF_PASSWORD], - CONF_MODEL: api.vapix.product_number, } + await self.async_set_unique_id(format_mac(serial)) + + if self.source == SOURCE_REAUTH: + self._abort_if_unique_id_mismatch() + return self.async_update_reload_and_abort( + self._get_reauth_entry(), data_updates=config + ) + if self.source == SOURCE_RECONFIGURE: + self._abort_if_unique_id_mismatch() + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), data_updates=config + ) + self._abort_if_unique_id_configured() + + self.config = config | {CONF_MODEL: api.vapix.product_number} + return await self._create_entry(serial) data = self.discovery_schema or { @@ -152,8 +157,9 @@ class AxisFlowHandler(ConfigFlow, domain=AXIS_DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Trigger a reconfiguration flow.""" - entry = self._get_reconfigure_entry() - return await self._redo_configuration(entry.data, keep_password=True) + return await self._redo_configuration( + self._get_reconfigure_entry().data, keep_password=True + ) async def async_step_reauth( self, entry_data: Mapping[str, Any] diff --git a/homeassistant/components/axis/strings.json b/homeassistant/components/axis/strings.json index 9534989305d..da1963deacd 100644 --- a/homeassistant/components/axis/strings.json +++ b/homeassistant/components/axis/strings.json @@ -26,7 +26,10 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "link_local_address": "Link local addresses are not supported", - "not_axis_device": "Discovered device not an Axis device" + "not_axis_device": "Discovered device not an Axis device", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "unique_id_mismatch": "The serial number of the device does not match the previous serial number" } }, "options": { diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index c8ffc46ca3f..52dd9c2f8ad 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -75,7 +75,7 @@ async def test_flow_manual_configuration(hass: HomeAssistant) -> None: } -async def test_manual_configuration_update_configuration( +async def test_manual_configuration_duplicate_fails( hass: HomeAssistant, config_entry_setup: MockConfigEntry, mock_requests: Callable[[str], None], @@ -105,7 +105,7 @@ async def test_manual_configuration_update_configuration( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" - assert config_entry_setup.data[CONF_HOST] == "2.3.4.5" + assert config_entry_setup.data[CONF_HOST] == "1.2.3.4" @pytest.mark.parametrize( @@ -221,7 +221,7 @@ async def test_reauth_flow_update_configuration( await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "already_configured" + assert result["reason"] == "reauth_successful" assert config_entry_setup.data[CONF_PROTOCOL] == "https" assert config_entry_setup.data[CONF_HOST] == "2.3.4.5" assert config_entry_setup.data[CONF_PORT] == 443 @@ -255,7 +255,7 @@ async def test_reconfiguration_flow_update_configuration( await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "already_configured" + assert result["reason"] == "reconfigure_successful" assert config_entry_setup.data[CONF_PROTOCOL] == "http" assert config_entry_setup.data[CONF_HOST] == "2.3.4.5" assert config_entry_setup.data[CONF_PORT] == 80