|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from homeassistant.components.airos.const import DOMAIN, SECTION_ADVANCED_SETTINGS |
14 | | -from homeassistant.config_entries import SOURCE_USER |
| 14 | +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER |
15 | 15 | from homeassistant.const import ( |
16 | 16 | CONF_HOST, |
17 | 17 | CONF_PASSWORD, |
|
26 | 26 |
|
27 | 27 | NEW_PASSWORD = "new_password" |
28 | 28 | REAUTH_STEP = "reauth_confirm" |
| 29 | +RECONFIGURE_STEP = "reconfigure" |
29 | 30 |
|
30 | 31 | MOCK_CONFIG = { |
31 | 32 | CONF_HOST: "1.1.1.1", |
@@ -253,3 +254,151 @@ async def test_reauth_unique_id_mismatch( |
253 | 254 |
|
254 | 255 | updated_entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id) |
255 | 256 | assert updated_entry.data[CONF_PASSWORD] != NEW_PASSWORD |
| 257 | + |
| 258 | + |
| 259 | +async def test_successful_reconfigure( |
| 260 | + hass: HomeAssistant, |
| 261 | + mock_airos_client: AsyncMock, |
| 262 | + mock_config_entry: MockConfigEntry, |
| 263 | +) -> None: |
| 264 | + """Test successful reconfigure.""" |
| 265 | + mock_config_entry.add_to_hass(hass) |
| 266 | + |
| 267 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 268 | + |
| 269 | + result = await hass.config_entries.flow.async_init( |
| 270 | + DOMAIN, |
| 271 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 272 | + ) |
| 273 | + |
| 274 | + assert result["type"] is FlowResultType.FORM |
| 275 | + assert result["step_id"] == RECONFIGURE_STEP |
| 276 | + |
| 277 | + user_input = { |
| 278 | + CONF_PASSWORD: NEW_PASSWORD, |
| 279 | + SECTION_ADVANCED_SETTINGS: { |
| 280 | + CONF_SSL: True, |
| 281 | + CONF_VERIFY_SSL: True, |
| 282 | + }, |
| 283 | + } |
| 284 | + |
| 285 | + result = await hass.config_entries.flow.async_configure( |
| 286 | + result["flow_id"], |
| 287 | + user_input=user_input, |
| 288 | + ) |
| 289 | + |
| 290 | + assert result["type"] is FlowResultType.ABORT |
| 291 | + assert result["reason"] == "reconfigure_successful" |
| 292 | + |
| 293 | + updated_entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id) |
| 294 | + assert updated_entry.data[CONF_PASSWORD] == NEW_PASSWORD |
| 295 | + assert updated_entry.data[SECTION_ADVANCED_SETTINGS][CONF_SSL] is True |
| 296 | + assert updated_entry.data[SECTION_ADVANCED_SETTINGS][CONF_VERIFY_SSL] is True |
| 297 | + |
| 298 | + assert updated_entry.data[CONF_HOST] == MOCK_CONFIG[CONF_HOST] |
| 299 | + assert updated_entry.data[CONF_USERNAME] == MOCK_CONFIG[CONF_USERNAME] |
| 300 | + |
| 301 | + |
| 302 | +@pytest.mark.parametrize( |
| 303 | + ("reconfigure_exception", "expected_error"), |
| 304 | + [ |
| 305 | + (AirOSConnectionAuthenticationError, "invalid_auth"), |
| 306 | + (AirOSDeviceConnectionError, "cannot_connect"), |
| 307 | + (AirOSKeyDataMissingError, "key_data_missing"), |
| 308 | + (Exception, "unknown"), |
| 309 | + ], |
| 310 | + ids=[ |
| 311 | + "invalid_auth", |
| 312 | + "cannot_connect", |
| 313 | + "key_data_missing", |
| 314 | + "unknown", |
| 315 | + ], |
| 316 | +) |
| 317 | +async def test_reconfigure_flow_failure( |
| 318 | + hass: HomeAssistant, |
| 319 | + mock_airos_client: AsyncMock, |
| 320 | + mock_config_entry: MockConfigEntry, |
| 321 | + reconfigure_exception: Exception, |
| 322 | + expected_error: str, |
| 323 | +) -> None: |
| 324 | + """Test reconfigure from start (failure) to finish (success).""" |
| 325 | + mock_config_entry.add_to_hass(hass) |
| 326 | + |
| 327 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 328 | + |
| 329 | + result = await hass.config_entries.flow.async_init( |
| 330 | + DOMAIN, |
| 331 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 332 | + ) |
| 333 | + |
| 334 | + user_input = { |
| 335 | + CONF_PASSWORD: NEW_PASSWORD, |
| 336 | + SECTION_ADVANCED_SETTINGS: { |
| 337 | + CONF_SSL: True, |
| 338 | + CONF_VERIFY_SSL: True, |
| 339 | + }, |
| 340 | + } |
| 341 | + |
| 342 | + mock_airos_client.login.side_effect = reconfigure_exception |
| 343 | + |
| 344 | + result = await hass.config_entries.flow.async_configure( |
| 345 | + result["flow_id"], |
| 346 | + user_input=user_input, |
| 347 | + ) |
| 348 | + |
| 349 | + assert result["type"] is FlowResultType.FORM |
| 350 | + assert result["step_id"] == RECONFIGURE_STEP |
| 351 | + assert result["errors"] == {"base": expected_error} |
| 352 | + |
| 353 | + mock_airos_client.login.side_effect = None |
| 354 | + result = await hass.config_entries.flow.async_configure( |
| 355 | + result["flow_id"], |
| 356 | + user_input=user_input, |
| 357 | + ) |
| 358 | + |
| 359 | + assert result["type"] is FlowResultType.ABORT |
| 360 | + assert result["reason"] == "reconfigure_successful" |
| 361 | + |
| 362 | + updated_entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id) |
| 363 | + assert updated_entry.data[CONF_PASSWORD] == NEW_PASSWORD |
| 364 | + |
| 365 | + |
| 366 | +async def test_reconfigure_unique_id_mismatch( |
| 367 | + hass: HomeAssistant, |
| 368 | + mock_airos_client: AsyncMock, |
| 369 | + mock_config_entry: MockConfigEntry, |
| 370 | +) -> None: |
| 371 | + """Test reconfiguration failure when the unique ID changes.""" |
| 372 | + mock_config_entry.add_to_hass(hass) |
| 373 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 374 | + |
| 375 | + result = await hass.config_entries.flow.async_init( |
| 376 | + DOMAIN, |
| 377 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 378 | + ) |
| 379 | + flow_id = result["flow_id"] |
| 380 | + |
| 381 | + mock_airos_client.status.return_value.derived.mac = "FF:23:45:67:89:AB" |
| 382 | + |
| 383 | + user_input = { |
| 384 | + CONF_PASSWORD: NEW_PASSWORD, |
| 385 | + SECTION_ADVANCED_SETTINGS: { |
| 386 | + CONF_SSL: True, |
| 387 | + CONF_VERIFY_SSL: True, |
| 388 | + }, |
| 389 | + } |
| 390 | + |
| 391 | + result = await hass.config_entries.flow.async_configure( |
| 392 | + flow_id, |
| 393 | + user_input=user_input, |
| 394 | + ) |
| 395 | + |
| 396 | + assert result["type"] is FlowResultType.ABORT |
| 397 | + assert result["reason"] == "unique_id_mismatch" |
| 398 | + |
| 399 | + updated_entry = hass.config_entries.async_get_entry(mock_config_entry.entry_id) |
| 400 | + assert updated_entry.data[CONF_PASSWORD] == MOCK_CONFIG[CONF_PASSWORD] |
| 401 | + assert ( |
| 402 | + updated_entry.data[SECTION_ADVANCED_SETTINGS][CONF_SSL] |
| 403 | + == MOCK_CONFIG[SECTION_ADVANCED_SETTINGS][CONF_SSL] |
| 404 | + ) |
0 commit comments