|
15 | 15 | CONF_VIA, |
16 | 16 | DOMAIN, |
17 | 17 | ) |
18 | | -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER |
| 18 | +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_RECONFIGURE, SOURCE_USER |
19 | 19 | from homeassistant.const import CONF_API_KEY, CONF_NAME |
20 | 20 | from homeassistant.core import HomeAssistant |
21 | 21 | from homeassistant.data_entry_flow import FlowResultType |
@@ -331,3 +331,128 @@ async def test_import_flow_exceptions( |
331 | 331 | ) |
332 | 332 | assert result["type"] is FlowResultType.ABORT |
333 | 333 | assert result["reason"] == expected_error |
| 334 | + |
| 335 | + |
| 336 | +async def test_reconfigure_success( |
| 337 | + hass: HomeAssistant, mock_nsapi: AsyncMock, mock_config_entry: MockConfigEntry |
| 338 | +) -> None: |
| 339 | + """Test successfully reconfiguring (updating) the API key.""" |
| 340 | + new_key = "new_api_key_123456" |
| 341 | + |
| 342 | + mock_config_entry.add_to_hass(hass) |
| 343 | + |
| 344 | + # Start reconfigure flow |
| 345 | + result = await hass.config_entries.flow.async_init( |
| 346 | + DOMAIN, |
| 347 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 348 | + ) |
| 349 | + |
| 350 | + assert result["type"] is FlowResultType.FORM |
| 351 | + assert result["step_id"] == "reconfigure" |
| 352 | + |
| 353 | + # Submit new API key, mock_nsapi.get_stations returns OK by default |
| 354 | + result2 = await hass.config_entries.flow.async_configure( |
| 355 | + result["flow_id"], user_input={CONF_API_KEY: new_key} |
| 356 | + ) |
| 357 | + |
| 358 | + assert result2["type"] is FlowResultType.ABORT |
| 359 | + assert result2["reason"] == "reconfigure_successful" |
| 360 | + |
| 361 | + # Entry should be updated with the new API key |
| 362 | + assert mock_config_entry.data[CONF_API_KEY] == new_key |
| 363 | + |
| 364 | + |
| 365 | +@pytest.mark.parametrize( |
| 366 | + ("exception", "expected_error"), |
| 367 | + [ |
| 368 | + (HTTPError("Invalid API key"), "invalid_auth"), |
| 369 | + (Timeout("Cannot connect"), "cannot_connect"), |
| 370 | + (RequestsConnectionError("Cannot connect"), "cannot_connect"), |
| 371 | + (Exception("Unexpected error"), "unknown"), |
| 372 | + ], |
| 373 | +) |
| 374 | +async def test_reconfigure_errors( |
| 375 | + hass: HomeAssistant, |
| 376 | + mock_nsapi: AsyncMock, |
| 377 | + mock_config_entry: MockConfigEntry, |
| 378 | + exception: Exception, |
| 379 | + expected_error: str, |
| 380 | +) -> None: |
| 381 | + """Test reconfigure flow error handling (invalid auth and cannot connect).""" |
| 382 | + mock_config_entry.add_to_hass(hass) |
| 383 | + |
| 384 | + # First present the form |
| 385 | + result = await hass.config_entries.flow.async_init( |
| 386 | + DOMAIN, |
| 387 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 388 | + ) |
| 389 | + assert result["type"] is FlowResultType.FORM |
| 390 | + assert result["step_id"] == "reconfigure" |
| 391 | + |
| 392 | + # Make get_stations raise the requested exception |
| 393 | + mock_nsapi.get_stations.side_effect = exception |
| 394 | + |
| 395 | + result2 = await hass.config_entries.flow.async_configure( |
| 396 | + result["flow_id"], user_input={CONF_API_KEY: "bad_key"} |
| 397 | + ) |
| 398 | + |
| 399 | + assert result2["type"] is FlowResultType.FORM |
| 400 | + assert result2["errors"] == {"base": expected_error} |
| 401 | + |
| 402 | + # Clear side effect and submit valid API key to complete the flow |
| 403 | + mock_nsapi.get_stations.side_effect = None |
| 404 | + |
| 405 | + result3 = await hass.config_entries.flow.async_configure( |
| 406 | + result["flow_id"], user_input={CONF_API_KEY: "new_valid_key"} |
| 407 | + ) |
| 408 | + |
| 409 | + assert result3["type"] is FlowResultType.ABORT |
| 410 | + assert result3["reason"] == "reconfigure_successful" |
| 411 | + assert mock_config_entry.data[CONF_API_KEY] == "new_valid_key" |
| 412 | + |
| 413 | + |
| 414 | +async def test_reconfigure_already_configured( |
| 415 | + hass: HomeAssistant, mock_nsapi: AsyncMock, mock_config_entry: MockConfigEntry |
| 416 | +) -> None: |
| 417 | + """Test reconfiguring with an API key that's already used by another entry.""" |
| 418 | + # Add first entry |
| 419 | + mock_config_entry.add_to_hass(hass) |
| 420 | + |
| 421 | + # Create and add second entry with different API key |
| 422 | + second_entry = MockConfigEntry( |
| 423 | + domain=DOMAIN, |
| 424 | + title="NS Integration 2", |
| 425 | + data={CONF_API_KEY: "another_api_key_456"}, |
| 426 | + unique_id="second_entry", |
| 427 | + ) |
| 428 | + second_entry.add_to_hass(hass) |
| 429 | + |
| 430 | + # Start reconfigure flow for the first entry |
| 431 | + result = await hass.config_entries.flow.async_init( |
| 432 | + DOMAIN, |
| 433 | + context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id}, |
| 434 | + ) |
| 435 | + |
| 436 | + assert result["type"] is FlowResultType.FORM |
| 437 | + assert result["step_id"] == "reconfigure" |
| 438 | + |
| 439 | + # Try to reconfigure to use the API key from the second entry |
| 440 | + result2 = await hass.config_entries.flow.async_configure( |
| 441 | + result["flow_id"], user_input={CONF_API_KEY: "another_api_key_456"} |
| 442 | + ) |
| 443 | + |
| 444 | + # Should show error that it's already configured |
| 445 | + assert result2["type"] is FlowResultType.FORM |
| 446 | + assert result2["errors"] == {"base": "already_configured"} |
| 447 | + |
| 448 | + # Verify the original entry was not changed |
| 449 | + assert mock_config_entry.data[CONF_API_KEY] == API_KEY |
| 450 | + |
| 451 | + # Now submit a valid unique API key to complete the flow |
| 452 | + result3 = await hass.config_entries.flow.async_configure( |
| 453 | + result["flow_id"], user_input={CONF_API_KEY: "new_unique_key_789"} |
| 454 | + ) |
| 455 | + |
| 456 | + assert result3["type"] is FlowResultType.ABORT |
| 457 | + assert result3["reason"] == "reconfigure_successful" |
| 458 | + assert mock_config_entry.data[CONF_API_KEY] == "new_unique_key_789" |
0 commit comments