Skip to content

Commit

Permalink
Use reconfigure helpers in axis config flow (home-assistant#127976)
Browse files Browse the repository at this point in the history
* Use reconfigure helpers in axis config flow

* Add string

* Update strings.json
  • Loading branch information
epenet authored Oct 12, 2024
1 parent caf85fe commit 3e56185
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
38 changes: 22 additions & 16 deletions homeassistant/components/axis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.config_entries import (
SOURCE_IGNORE,
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
Expand Down Expand Up @@ -87,27 +89,30 @@ async def async_step_user(

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 {
Expand Down Expand Up @@ -152,8 +157,9 @@ async def async_step_reconfigure(
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]
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/axis/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions tests/components/axis/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e56185

Please sign in to comment.