Skip to content

Commit 9251dde

Browse files
authored
Add OpenRGB reconfiguration flow (home-assistant#154478)
1 parent 24d77cc commit 9251dde

File tree

4 files changed

+213
-1
lines changed

4 files changed

+213
-1
lines changed

homeassistant/components/openrgb/config_flow.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
}
2626
)
2727

28+
STEP_RECONFIGURE_DATA_SCHEMA = vol.Schema(
29+
{
30+
vol.Required(CONF_HOST): str,
31+
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
32+
}
33+
)
34+
2835

2936
async def validate_input(hass: HomeAssistant, host: str, port: int) -> None:
3037
"""Validate the user input allows us to connect."""
@@ -39,6 +46,48 @@ def _try_connect(host: str, port: int) -> None:
3946
class OpenRGBConfigFlow(ConfigFlow, domain=DOMAIN):
4047
"""Handle a config flow for OpenRGB."""
4148

49+
async def async_step_reconfigure(
50+
self, user_input: dict[str, Any] | None = None
51+
) -> ConfigFlowResult:
52+
"""Handle reconfiguration of the OpenRGB SDK Server."""
53+
reconfigure_entry = self._get_reconfigure_entry()
54+
55+
errors: dict[str, str] = {}
56+
if user_input is not None:
57+
host = user_input[CONF_HOST]
58+
port = user_input[CONF_PORT]
59+
60+
# Prevent duplicate entries
61+
self._async_abort_entries_match({CONF_HOST: host, CONF_PORT: port})
62+
63+
try:
64+
await validate_input(self.hass, host, port)
65+
except CONNECTION_ERRORS:
66+
errors["base"] = "cannot_connect"
67+
except Exception:
68+
_LOGGER.exception(
69+
"Unknown error while connecting to OpenRGB SDK server at %s",
70+
f"{host}:{port}",
71+
)
72+
errors["base"] = "unknown"
73+
else:
74+
return self.async_update_reload_and_abort(
75+
reconfigure_entry,
76+
data_updates={CONF_HOST: host, CONF_PORT: port},
77+
)
78+
79+
return self.async_show_form(
80+
step_id="reconfigure",
81+
data_schema=self.add_suggested_values_to_schema(
82+
data_schema=STEP_RECONFIGURE_DATA_SCHEMA,
83+
suggested_values={
84+
CONF_HOST: reconfigure_entry.data[CONF_HOST],
85+
CONF_PORT: reconfigure_entry.data[CONF_PORT],
86+
},
87+
),
88+
errors=errors,
89+
)
90+
4291
async def async_step_user(
4392
self, user_input: dict[str, Any] | None = None
4493
) -> ConfigFlowResult:

homeassistant/components/openrgb/quality_scale.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ rules:
6868
entity-translations: todo
6969
exception-translations: done
7070
icon-translations: todo
71-
reconfiguration-flow: todo
71+
reconfiguration-flow: done
7272
repair-issues: todo
7373
stale-devices: todo
7474

homeassistant/components/openrgb/strings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"config": {
33
"step": {
44
"user": {
5+
"title": "Set up OpenRGB SDK server",
56
"description": "Set up your OpenRGB SDK server to allow control from within Home Assistant.",
67
"data": {
78
"name": "[%key:common::config_flow::data::name%]",
@@ -13,6 +14,18 @@
1314
"host": "The IP address or hostname of the computer running the OpenRGB SDK server.",
1415
"port": "The port number that the OpenRGB SDK server is running on."
1516
}
17+
},
18+
"reconfigure": {
19+
"title": "Reconfigure OpenRGB SDK server",
20+
"description": "Update the connection settings for your OpenRGB SDK server.",
21+
"data": {
22+
"host": "[%key:common::config_flow::data::host%]",
23+
"port": "[%key:common::config_flow::data::port%]"
24+
},
25+
"data_description": {
26+
"host": "[%key:component::openrgb::config::step::user::data_description::host%]",
27+
"port": "[%key:component::openrgb::config::step::user::data_description::port%]"
28+
}
1629
}
1730
},
1831
"error": {
@@ -21,6 +34,7 @@
2134
"unknown": "[%key:common::config_flow::error::unknown%]"
2235
},
2336
"abort": {
37+
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
2438
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
2539
}
2640
},

tests/components/openrgb/test_config_flow.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,152 @@ async def test_user_flow_already_configured(
112112

113113
assert result["type"] is FlowResultType.ABORT
114114
assert result["reason"] == "already_configured"
115+
116+
117+
@pytest.mark.usefixtures("mock_setup_entry", "mock_openrgb_client")
118+
async def test_user_flow_duplicate_entry(
119+
hass: HomeAssistant, mock_config_entry: MockConfigEntry
120+
) -> None:
121+
"""Test user flow when trying to add duplicate host/port combination."""
122+
mock_config_entry.add_to_hass(hass)
123+
124+
# Create another config entry with different host/port
125+
other_entry = MockConfigEntry(
126+
domain=DOMAIN,
127+
title="Other Computer",
128+
data={
129+
CONF_NAME: "Other Computer",
130+
CONF_HOST: "192.168.1.200",
131+
CONF_PORT: 6743,
132+
},
133+
entry_id="01J0EXAMPLE0CONFIGENTRY01",
134+
)
135+
other_entry.add_to_hass(hass)
136+
137+
result = await hass.config_entries.flow.async_init(
138+
DOMAIN,
139+
context={"source": SOURCE_USER},
140+
)
141+
142+
# Try to add entry with same host/port as other_entry
143+
result = await hass.config_entries.flow.async_configure(
144+
result["flow_id"],
145+
user_input={
146+
CONF_NAME: "Yet Another Computer",
147+
CONF_HOST: "192.168.1.200",
148+
CONF_PORT: 6743,
149+
},
150+
)
151+
152+
assert result["type"] is FlowResultType.ABORT
153+
assert result["reason"] == "already_configured"
154+
155+
156+
@pytest.mark.usefixtures("mock_setup_entry", "mock_openrgb_client")
157+
async def test_reconfigure_flow(
158+
hass: HomeAssistant, mock_config_entry: MockConfigEntry
159+
) -> None:
160+
"""Test the reconfiguration flow."""
161+
mock_config_entry.add_to_hass(hass)
162+
163+
result = await mock_config_entry.start_reconfigure_flow(hass)
164+
165+
assert result["type"] is FlowResultType.FORM
166+
assert result["step_id"] == "reconfigure"
167+
168+
result = await hass.config_entries.flow.async_configure(
169+
result["flow_id"],
170+
user_input={
171+
CONF_HOST: "192.168.1.100",
172+
CONF_PORT: 6743,
173+
},
174+
)
175+
176+
assert result["type"] is FlowResultType.ABORT
177+
assert result["reason"] == "reconfigure_successful"
178+
assert mock_config_entry.data[CONF_HOST] == "192.168.1.100"
179+
assert mock_config_entry.data[CONF_PORT] == 6743
180+
181+
182+
@pytest.mark.parametrize(
183+
("exception", "error_key"),
184+
[
185+
(ConnectionRefusedError, "cannot_connect"),
186+
(OpenRGBDisconnected, "cannot_connect"),
187+
(TimeoutError, "cannot_connect"),
188+
(socket.gaierror, "cannot_connect"),
189+
(SDKVersionError, "cannot_connect"),
190+
(RuntimeError("Test error"), "unknown"),
191+
],
192+
)
193+
@pytest.mark.usefixtures("mock_setup_entry")
194+
async def test_reconfigure_flow_errors(
195+
hass: HomeAssistant,
196+
mock_config_entry: MockConfigEntry,
197+
exception: Exception,
198+
error_key: str,
199+
mock_openrgb_client,
200+
) -> None:
201+
"""Test reconfiguration flow with various errors."""
202+
mock_config_entry.add_to_hass(hass)
203+
204+
result = await mock_config_entry.start_reconfigure_flow(hass)
205+
206+
mock_openrgb_client.client_class_mock.side_effect = exception
207+
208+
result = await hass.config_entries.flow.async_configure(
209+
result["flow_id"],
210+
user_input={CONF_HOST: "192.168.1.100", CONF_PORT: 6743},
211+
)
212+
213+
assert result["type"] is FlowResultType.FORM
214+
assert result["step_id"] == "reconfigure"
215+
assert result["errors"] == {"base": error_key}
216+
217+
# Test recovery from error
218+
mock_openrgb_client.client_class_mock.side_effect = None
219+
220+
result = await hass.config_entries.flow.async_configure(
221+
result["flow_id"],
222+
user_input={CONF_HOST: "192.168.1.100", CONF_PORT: 6743},
223+
)
224+
225+
assert result["type"] is FlowResultType.ABORT
226+
assert result["reason"] == "reconfigure_successful"
227+
assert mock_config_entry.data[CONF_HOST] == "192.168.1.100"
228+
assert mock_config_entry.data[CONF_PORT] == 6743
229+
230+
231+
@pytest.mark.usefixtures("mock_setup_entry", "mock_openrgb_client")
232+
async def test_reconfigure_flow_duplicate_entry(
233+
hass: HomeAssistant, mock_config_entry: MockConfigEntry
234+
) -> None:
235+
"""Test reconfiguration flow when new config matches another existing entry."""
236+
mock_config_entry.add_to_hass(hass)
237+
238+
# Create another config entry with different host/port
239+
other_entry = MockConfigEntry(
240+
domain=DOMAIN,
241+
title="Other Computer",
242+
data={
243+
CONF_NAME: "Other Computer",
244+
CONF_HOST: "192.168.1.200",
245+
CONF_PORT: 6743,
246+
},
247+
entry_id="01J0EXAMPLE0CONFIGENTRY01",
248+
)
249+
other_entry.add_to_hass(hass)
250+
251+
result = await mock_config_entry.start_reconfigure_flow(hass)
252+
253+
# Try to reconfigure to match the other entry
254+
result = await hass.config_entries.flow.async_configure(
255+
result["flow_id"],
256+
user_input={
257+
CONF_HOST: "192.168.1.200",
258+
CONF_PORT: 6743,
259+
},
260+
)
261+
262+
assert result["type"] is FlowResultType.ABORT
263+
assert result["reason"] == "already_configured"

0 commit comments

Comments
 (0)