Add reconfigure flow to NextCloud (#175457)

This commit is contained in:
G Johansson
2026-07-03 19:30:50 +02:00
committed by GitHub
parent 96920cc263
commit 14e2d51963
4 changed files with 132 additions and 19 deletions
@@ -11,7 +11,11 @@ from nextcloudmonitor import (
)
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
SOURCE_RECONFIGURE,
ConfigFlow,
ConfigFlowResult,
)
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
from .const import DEFAULT_VERIFY_SSL, DOMAIN
@@ -46,8 +50,7 @@ class NextcloudConfigFlow(ConfigFlow, domain=DOMAIN):
user_input.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL),
)
@override
async def async_step_user(
async def async_step_config(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
@@ -62,16 +65,37 @@ class NextcloudConfigFlow(ConfigFlow, domain=DOMAIN):
except NextcloudMonitorConnectionError, NextcloudMonitorRequestError:
errors["base"] = "connection_error"
else:
if self.source == SOURCE_RECONFIGURE:
return self.async_update_reload_and_abort(
self._get_reconfigure_entry(), data_updates=user_input
)
return self.async_create_entry(
title=user_input[CONF_URL],
data=user_input,
)
data_schema = self.add_suggested_values_to_schema(DATA_SCHEMA_USER, user_input)
data = user_input
if self.source == SOURCE_RECONFIGURE:
data = data or dict(self._get_reconfigure_entry().data)
data_schema = self.add_suggested_values_to_schema(DATA_SCHEMA_USER, data)
return self.async_show_form(
step_id="user", data_schema=data_schema, errors=errors
step_id="config", data_schema=data_schema, errors=errors
)
@override
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
return await self.async_step_config(user_input)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle a reconfigure flow initialized by the user."""
return await self.async_step_config(user_input)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
@@ -3,7 +3,8 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"connection_error_during_import": "Connection error occurred during yaml configuration import",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
},
"error": {
"connection_error": "[%key:common::config_flow::error::cannot_connect%]",
@@ -11,14 +12,7 @@
},
"flow_title": "Nextcloud",
"step": {
"reauth_confirm": {
"data": {
"password": "[%key:common::config_flow::data::password%]",
"username": "[%key:common::config_flow::data::username%]"
},
"description": "Update your login information for {url}."
},
"user": {
"config": {
"data": {
"password": "[%key:common::config_flow::data::password%]",
"url": "[%key:common::config_flow::data::url%]",
@@ -26,6 +20,13 @@
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
},
"description": "Enter your Nextcloud information."
},
"reauth_confirm": {
"data": {
"password": "[%key:common::config_flow::data::password%]",
"username": "[%key:common::config_flow::data::username%]"
},
"description": "Update your login information for {url}."
}
}
},
@@ -7,6 +7,14 @@
'verify_ssl': True,
})
# ---
# name: test_reconfigure_entry
dict({
'password': 'other_password',
'url': 'https://my.nc_url.local',
'username': 'other_user',
'verify_ssl': True,
})
# ---
# name: test_user_create_entry
dict({
'password': 'nc_pass',
+85 -5
View File
@@ -32,7 +32,7 @@ async def test_user_create_entry(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "config"
assert result["errors"] == {}
# test NextcloudMonitorAuthorizationError
@@ -46,7 +46,7 @@ async def test_user_create_entry(
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "config"
assert result["errors"] == {"base": "invalid_auth"}
# test NextcloudMonitorConnectionError
@@ -60,7 +60,7 @@ async def test_user_create_entry(
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "config"
assert result["errors"] == {"base": "connection_error"}
# test NextcloudMonitorRequestError
@@ -74,7 +74,7 @@ async def test_user_create_entry(
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "config"
assert result["errors"] == {"base": "connection_error"}
# test success
@@ -93,6 +93,86 @@ async def test_user_create_entry(
assert result["data"] == snapshot
async def test_reconfigure_entry(
hass: HomeAssistant, snapshot: SnapshotAssertion
) -> None:
"""Test that the reconfigure step works."""
entry = MockConfigEntry(
domain=DOMAIN,
title="https://my.nc_url.local",
unique_id="nc_url",
data=VALID_CONFIG,
)
entry.add_to_hass(hass)
result = await entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "config"
assert result["errors"] == {}
# test NextcloudMonitorAuthorizationError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorAuthorizationError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "config"
assert result["errors"] == {"base": "invalid_auth"}
# test NextcloudMonitorConnectionError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorConnectionError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "config"
assert result["errors"] == {"base": "connection_error"}
# test NextcloudMonitorRequestError
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
side_effect=NextcloudMonitorRequestError,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
VALID_CONFIG,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "config"
assert result["errors"] == {"base": "connection_error"}
# test success
with patch(
"homeassistant.components.nextcloud.config_flow.NextcloudMonitor",
return_value=True,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
**VALID_CONFIG,
CONF_USERNAME: "other_user",
CONF_PASSWORD: "other_password",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == snapshot
async def test_user_already_configured(hass: HomeAssistant) -> None:
"""Test that errors are shown when duplicates are added."""
entry = MockConfigEntry(
@@ -107,7 +187,7 @@ async def test_user_already_configured(hass: HomeAssistant) -> None:
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "config"
assert result["errors"] == {}
with patch(