@@ -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