Skip to content

Commit

Permalink
Add reconfigure flow to Axis integration (#114067)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored Mar 24, 2024
1 parent 579084a commit 2e2b40f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
20 changes: 18 additions & 2 deletions homeassistant/components/axis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ async def _create_entry(self, serial: str) -> ConfigFlowResult:
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:
Expand All @@ -154,14 +162,22 @@ async def async_step_reauth(
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,
}

Expand Down
39 changes: 39 additions & 0 deletions tests/components/axis/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SOURCE_DHCP,
SOURCE_IGNORE,
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
SOURCE_SSDP,
SOURCE_USER,
SOURCE_ZEROCONF,
Expand Down Expand Up @@ -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"),
[
Expand Down

0 comments on commit 2e2b40f

Please sign in to comment.