diff --git a/homeassistant/components/axis/config_flow.py b/homeassistant/components/axis/config_flow.py index 6a5543a284c..78dfe016c44 100644 --- a/homeassistant/components/axis/config_flow.py +++ b/homeassistant/components/axis/config_flow.py @@ -75,6 +75,8 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): updates={ CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT], + CONF_USERNAME: user_input[CONF_USERNAME], + CONF_PASSWORD: user_input[CONF_PASSWORD], } ) @@ -131,6 +133,23 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): title = f"{model} - {self.serial}" return self.async_create_entry(title=title, data=self.device_config) + async def async_step_reauth(self, device_config: dict): + """Trigger a reauthentication flow.""" + # pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167 + self.context["title_placeholders"] = { + CONF_NAME: device_config[CONF_NAME], + CONF_HOST: device_config[CONF_HOST], + } + + self.discovery_schema = { + vol.Required(CONF_HOST, default=device_config[CONF_HOST]): str, + vol.Required(CONF_USERNAME, default=device_config[CONF_USERNAME]): str, + vol.Required(CONF_PASSWORD): str, + vol.Required(CONF_PORT, default=device_config[CONF_PORT]): int, + } + + return await self.async_step_user() + async def async_step_dhcp(self, discovery_info: dict): """Prepare configuration for a DHCP discovered Axis device.""" return await self._process_discovered_device( diff --git a/homeassistant/components/axis/device.py b/homeassistant/components/axis/device.py index d0726a19c25..c7b8b54fda1 100644 --- a/homeassistant/components/axis/device.py +++ b/homeassistant/components/axis/device.py @@ -13,6 +13,7 @@ from axis.streammanager import SIGNAL_PLAYING, STATE_STOPPED from homeassistant.components import mqtt from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN from homeassistant.components.mqtt.models import Message +from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.const import ( CONF_HOST, CONF_NAME, @@ -213,9 +214,13 @@ class AxisNetworkDevice: except CannotConnect as err: raise ConfigEntryNotReady from err - except Exception as err: # pylint: disable=broad-except - LOGGER.error( - "Unknown error connecting with Axis device (%s): %s", self.host, err + except AuthenticationRequired: + self.hass.async_create_task( + self.hass.config_entries.flow.async_init( + AXIS_DOMAIN, + context={"source": SOURCE_REAUTH}, + data=self.config_entry.data, + ) ) return False diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index 2520b06696f..30cdf4c0d48 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -16,6 +16,7 @@ from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS from homeassistant.config_entries import ( SOURCE_DHCP, SOURCE_IGNORE, + SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF, ) @@ -112,33 +113,6 @@ async def test_manual_configuration_update_configuration(hass): assert len(mock_setup_entry.mock_calls) == 1 -async def test_flow_fails_already_configured(hass): - """Test that config flow fails on already configured device.""" - await setup_axis_integration(hass) - - result = await hass.config_entries.flow.async_init( - AXIS_DOMAIN, context={"source": SOURCE_USER} - ) - - assert result["type"] == RESULT_TYPE_FORM - assert result["step_id"] == SOURCE_USER - - with respx.mock: - mock_default_vapix_requests(respx) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_HOST: "1.2.3.4", - CONF_USERNAME: "user", - CONF_PASSWORD: "pass", - CONF_PORT: 80, - }, - ) - - assert result["type"] == RESULT_TYPE_ABORT - assert result["reason"] == "already_configured" - - async def test_flow_fails_faulty_credentials(hass): """Test that config flow fails on faulty credentials.""" result = await hass.config_entries.flow.async_init( @@ -237,6 +211,40 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass): assert result["data"][CONF_NAME] == "M1065-LW 2" +async def test_reauth_flow_update_configuration(hass): + """Test that config flow fails on already configured device.""" + config_entry = await setup_axis_integration(hass) + device = hass.data[AXIS_DOMAIN][config_entry.unique_id] + + result = await hass.config_entries.flow.async_init( + AXIS_DOMAIN, + context={"source": SOURCE_REAUTH}, + data=config_entry.data, + ) + + assert result["type"] == RESULT_TYPE_FORM + assert result["step_id"] == SOURCE_USER + + with respx.mock: + mock_default_vapix_requests(respx, "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: "user2", + CONF_PASSWORD: "pass2", + CONF_PORT: 80, + }, + ) + await hass.async_block_till_done() + + assert result["type"] == RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" + assert device.host == "2.3.4.5" + assert device.username == "user2" + assert device.password == "pass2" + + async def test_dhcp_flow(hass): """Test that DHCP discovery for new devices work.""" result = await hass.config_entries.flow.async_init( diff --git a/tests/components/axis/test_device.py b/tests/components/axis/test_device.py index d750a5bdded..a5371395638 100644 --- a/tests/components/axis/test_device.py +++ b/tests/components/axis/test_device.py @@ -412,6 +412,16 @@ async def test_device_not_accessible(hass): assert hass.data[AXIS_DOMAIN] == {} +async def test_device_trigger_reauth_flow(hass): + """Failed authentication trigger a reauthentication flow.""" + with patch.object( + axis.device, "get_device", side_effect=axis.errors.AuthenticationRequired + ), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init: + await setup_axis_integration(hass) + mock_flow_init.assert_called_once() + assert hass.data[AXIS_DOMAIN] == {} + + async def test_device_unknown_error(hass): """Unknown errors are handled.""" with patch.object(axis.device, "get_device", side_effect=Exception):