mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add reconfigure flow to WAQI (#186307)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
3bf7b1d572
commit
56dcdb406d
@@ -119,33 +119,43 @@ class WAQIConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Confirm reauthentication with a new API key."""
|
||||
return await self._async_step_update_api_key(
|
||||
"reauth_confirm", self._get_reauth_entry(), user_input
|
||||
)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reconfiguration with a new API key."""
|
||||
return await self._async_step_update_api_key(
|
||||
"reconfigure", self._get_reconfigure_entry(), user_input
|
||||
)
|
||||
|
||||
async def _async_step_update_api_key(
|
||||
self, step_id: str, entry: ConfigEntry, user_input: dict[str, Any] | None
|
||||
) -> ConfigFlowResult:
|
||||
"""Validate a new API key and store it in an existing entry."""
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
# Skips the entry being reauthenticated, so re-entering its key is allowed
|
||||
# Skips the entry being updated, so re-entering its key is allowed
|
||||
self._async_abort_entries_match({CONF_API_KEY: user_input[CONF_API_KEY]})
|
||||
if not (
|
||||
errors := await self._async_validate_api_key(user_input[CONF_API_KEY])
|
||||
):
|
||||
reauth_entry = self._get_reauth_entry()
|
||||
# A loaded entry is reloaded by its update listener when data changes
|
||||
reload = (
|
||||
reauth_entry.data[CONF_API_KEY] == user_input[CONF_API_KEY]
|
||||
or not reauth_entry.update_listeners
|
||||
entry.data[CONF_API_KEY] == user_input[CONF_API_KEY]
|
||||
or not entry.update_listeners
|
||||
)
|
||||
result = self.async_update_and_abort(
|
||||
reauth_entry,
|
||||
data_updates={CONF_API_KEY: user_input[CONF_API_KEY]},
|
||||
entry, data_updates={CONF_API_KEY: user_input[CONF_API_KEY]}
|
||||
)
|
||||
if reload:
|
||||
self.hass.config_entries.async_schedule_reload(
|
||||
reauth_entry.entry_id
|
||||
)
|
||||
self.hass.config_entries.async_schedule_reload(entry.entry_id)
|
||||
return result
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=API_KEY_SCHEMA,
|
||||
errors=errors,
|
||||
step_id=step_id, data_schema=API_KEY_SCHEMA, errors=errors
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,14 @@
|
||||
"api_key": "[%key:component::waqi::config::step::user::data_description::api_key%]"
|
||||
}
|
||||
},
|
||||
"reconfigure": {
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
},
|
||||
"data_description": {
|
||||
"api_key": "[%key:component::waqi::config::step::user::data_description::api_key%]"
|
||||
}
|
||||
},
|
||||
"user": {
|
||||
"data": {
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
|
||||
@@ -527,3 +527,130 @@ async def test_reauth_flow_duplicate(
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_config_entry.data == {CONF_API_KEY: "asd"}
|
||||
mock_waqi.get_by_ip.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"api_key",
|
||||
[
|
||||
pytest.param("new_key", id="new_key"),
|
||||
pytest.param("asd", id="same_key"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_reconfigure_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_waqi: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
api_key: str,
|
||||
) -> 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"
|
||||
assert not result["errors"]
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_API_KEY: api_key}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data == {CONF_API_KEY: api_key}
|
||||
mock_waqi.authenticate.assert_called_with(api_key)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"api_key",
|
||||
[
|
||||
pytest.param("new_key", id="new_key"),
|
||||
pytest.param("asd", id="same_key"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_flow_loaded_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_waqi: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
api_key: str,
|
||||
) -> None:
|
||||
"""Test reconfiguring a loaded entry reloads it once."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
mock_waqi.get_by_station_number.reset_mock()
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_API_KEY: api_key}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data == {CONF_API_KEY: api_key}
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
# Each setup fetches the single station once, so one call means one reload
|
||||
mock_waqi.get_by_station_number.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
(WAQIAuthenticationError("Test error"), "invalid_auth"),
|
||||
(WAQIConnectionError("Test error"), "cannot_connect"),
|
||||
(Exception("Test error"), "unknown"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_reconfigure_flow_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_waqi: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
exception: Exception,
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test reconfigure flow errors and recovery."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
mock_waqi.get_by_ip.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_API_KEY: "new_key"}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
mock_waqi.get_by_ip.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_API_KEY: "new_key"}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data == {CONF_API_KEY: "new_key"}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_reconfigure_flow_duplicate(
|
||||
hass: HomeAssistant,
|
||||
mock_waqi: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
second_mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reconfiguring with the API key of another entry aborts."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
second_mock_config_entry.add_to_hass(hass)
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_API_KEY: "asdf"}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_config_entry.data == {CONF_API_KEY: "asd"}
|
||||
mock_waqi.get_by_ip.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user