Add reconfigure flow to System Bridge (#190131)

This commit is contained in:
Aidan Timson
2026-11-03 18:53:17 +01:00
committed by GitHub
parent 5628b146e4
commit 1076095232
4 changed files with 171 additions and 4 deletions
@@ -36,6 +36,12 @@ STEP_USER_DATA_SCHEMA = probatio.Schema(
probatio.Required(probatio.Secret(CONF_TOKEN)): cv.string,
}
)
STEP_RECONFIGURE_DATA_SCHEMA = probatio.Schema(
{
probatio.Required(CONF_HOST): cv.string,
probatio.Required(CONF_PORT): cv.string,
}
)
async def _validate_input(
@@ -198,16 +204,46 @@ class SystemBridgeConfigFlow(
# Check if already configured
await self.async_set_unique_id(uuid)
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
updates = {CONF_HOST: host}
if (port := properties.get("port")) is not None:
updates[CONF_PORT] = port
self._abort_if_unique_id_configured(updates=updates)
self._name = host
self._input = {
CONF_HOST: host,
CONF_PORT: properties.get("port"),
CONF_PORT: port,
}
return await self.async_step_authenticate()
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the host and port."""
reconfigure_entry = self._get_reconfigure_entry()
errors: dict[str, str] = {}
if user_input is not None:
errors, info = await _async_get_info(
self.hass, {**reconfigure_entry.data, **user_input}
)
if not errors and info is not None:
await self.async_set_unique_id(info["uuid"])
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort(
reconfigure_entry, data_updates=user_input
)
return self.async_show_form(
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(
STEP_RECONFIGURE_DATA_SCHEMA,
user_input or reconfigure_entry.data,
),
errors=errors,
)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
@@ -78,7 +78,7 @@ rules:
status: todo
comment: Some errors, such as the coordinator's UpdateFailed, are not translated.
icon-translations: done
reconfiguration-flow: todo
reconfiguration-flow: done
repair-issues:
status: todo
comment: |
@@ -22,6 +22,17 @@
},
"description": "Please enter the token set in your configuration for {name}."
},
"reconfigure": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"port": "[%key:common::config_flow::data::port%]"
},
"data_description": {
"host": "The hostname or IP address of the computer running System Bridge.",
"port": "The port System Bridge listens on. The default is 9170."
},
"description": "Update the connection details for System Bridge."
},
"user": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
@@ -2,6 +2,7 @@
from collections.abc import Generator
from contextlib import contextmanager
from dataclasses import replace
from unittest.mock import patch
import pytest
@@ -11,10 +12,11 @@ from systembridgeconnector.exceptions import (
ConnectionClosedException,
ConnectionErrorException,
)
from systembridgeconnector.models.modules import ModulesData
from homeassistant import config_entries
from homeassistant.components.system_bridge.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_TOKEN
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@@ -753,3 +755,121 @@ async def test_zeroconf_bad_zeroconf_info(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unknown"
async def test_zeroconf_updates_existing_entry(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test zeroconf updates the host and port of an existing entry."""
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=replace(
FIXTURE_ZEROCONF,
properties={
**FIXTURE_ZEROCONF.properties,
"ip": "192.168.1.20",
"port": "9171",
},
),
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert mock_config_entry.data[CONF_HOST] == "192.168.1.20"
assert mock_config_entry.data[CONF_PORT] == "9171"
async def test_reconfigure_flow(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test reconfigure flow."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
with _patch_success():
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.1.20", CONF_PORT: "9171"}
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data == {
CONF_HOST: "192.168.1.20",
CONF_PORT: "9171",
CONF_TOKEN: FIXTURE_USER_INPUT[CONF_TOKEN],
}
@pytest.mark.parametrize(
("side_effect", "error"),
[
pytest.param(ConnectionErrorException, "cannot_connect", id="cannot_connect"),
pytest.param(AuthenticationException, "invalid_auth", id="invalid_auth"),
pytest.param(Exception, "unknown", id="unknown"),
],
)
async def test_reconfigure_flow_errors(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
side_effect: type[Exception],
error: str,
) -> None:
"""Test reconfigure flow shows errors and recovers."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
with patch(
"homeassistant.components.system_bridge.config_flow.WebSocketClient.connect",
side_effect=side_effect,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.1.20", CONF_PORT: "9171"}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["errors"] == {"base": error}
with _patch_success():
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.1.20", CONF_PORT: "9171"}
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
async def test_reconfigure_flow_unique_id_mismatch(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test reconfigure flow aborts when the host is a different System Bridge."""
mock_config_entry.add_to_hass(hass)
result = await mock_config_entry.start_reconfigure_flow(hass)
with (
_patch_success(),
patch(
"systembridgeconnector.websocket_client.WebSocketClient.get_data",
return_value=ModulesData(
system=replace(FIXTURE_DATA_RESPONSE.system, uuid="other-uuid")
),
),
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "192.168.1.20", CONF_PORT: "9171"}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert mock_config_entry.data[CONF_HOST] == FIXTURE_USER_INPUT[CONF_HOST]