mirror of
https://github.com/home-assistant/core.git
synced 2026-08-03 20:24:55 +02:00
Add reconfigure flow for Electrolux integration (#177070)
This commit is contained in:
committed by
GitHub
parent
d98204936d
commit
7b432b72ef
@@ -97,6 +97,33 @@ class ElectroluxConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
return self._show_form(step_id="reauth_confirm", errors=errors)
|
||||
|
||||
async def async_step_reconfigure(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reconfiguration of the integration."""
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input:
|
||||
try:
|
||||
token_manager = await _authenticate_user(user_input)
|
||||
except InvalidCredentialsException, BadCredentialsException:
|
||||
errors["base"] = "invalid_auth"
|
||||
except FailedConnectionException:
|
||||
errors["base"] = "cannot_connect"
|
||||
else:
|
||||
await self.async_set_unique_id(token_manager.get_user_id())
|
||||
self._abort_if_unique_id_mismatch()
|
||||
return self.async_update_reload_and_abort(
|
||||
self._get_reconfigure_entry(),
|
||||
data_updates={
|
||||
CONF_ACCESS_TOKEN: user_input[CONF_ACCESS_TOKEN],
|
||||
CONF_REFRESH_TOKEN: user_input[CONF_REFRESH_TOKEN],
|
||||
CONF_API_KEY: user_input[CONF_API_KEY],
|
||||
},
|
||||
)
|
||||
|
||||
return self._show_form(step_id="reconfigure", errors=errors)
|
||||
|
||||
def _show_form(self, step_id: str, errors: dict[str, str]) -> ConfigFlowResult:
|
||||
return self.async_show_form(
|
||||
step_id=step_id,
|
||||
|
||||
@@ -68,7 +68,7 @@ rules:
|
||||
entity-translations: todo
|
||||
exception-translations: todo
|
||||
icon-translations: done
|
||||
reconfiguration-flow: todo
|
||||
reconfiguration-flow: done
|
||||
repair-issues: todo
|
||||
stale-devices: todo
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"abort": {
|
||||
"already_configured": "This Electrolux account is already configured.",
|
||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
|
||||
"unique_id_mismatch": "The provided credentials don't belong to the account the configuration entry was set up with originally."
|
||||
},
|
||||
"error": {
|
||||
@@ -24,6 +25,20 @@
|
||||
"description": "Please go to the [developer portal]({portal_link}) to generate new access and refresh tokens, then paste them below.",
|
||||
"title": "Reauthenticate your Electrolux Group account"
|
||||
},
|
||||
"reconfigure": {
|
||||
"data": {
|
||||
"access_token": "[%key:common::config_flow::data::access_token%]",
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||
"refresh_token": "Refresh token"
|
||||
},
|
||||
"data_description": {
|
||||
"access_token": "The new access token from Electrolux Group for Developer after reconfiguration.",
|
||||
"api_key": "Your Electrolux Group for Developer API key.",
|
||||
"refresh_token": "The refresh token used to renew your access token."
|
||||
},
|
||||
"description": "Please go to the [developer portal]({portal_link}) to generate new access and refresh tokens, then paste them below.",
|
||||
"title": "Reconfigure your Electrolux Group account"
|
||||
},
|
||||
"user": {
|
||||
"data": {
|
||||
"access_token": "[%key:common::config_flow::data::access_token%]",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Unit test for Electrolux config flow."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from electrolux_group_developer_sdk.auth.invalid_credentials_exception import (
|
||||
@@ -312,3 +313,113 @@ async def test_reauth_mismatched_entry(
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_appliance_client", "mock_token_manager")
|
||||
async def test_reconfigure_successful(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the reconfigure step succeeds and updates the config entry."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=valid_user_input
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
|
||||
assert mock_config_entry.data == valid_user_input
|
||||
|
||||
|
||||
def _get_ensure_credentials(*, mock_token_manager: AsyncMock, **_) -> AsyncMock:
|
||||
"""Get the ensure_credentials method from the mock token manager."""
|
||||
return mock_token_manager.ensure_credentials
|
||||
|
||||
|
||||
def _get_test_connection(*, mock_appliance_client: AsyncMock, **_) -> AsyncMock:
|
||||
"""Get the test_connection method from the mock appliance client."""
|
||||
return mock_appliance_client.test_connection
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("get_mock", "exception", "error_name"),
|
||||
[
|
||||
(_get_ensure_credentials, InvalidCredentialsException(), "invalid_auth"),
|
||||
(_get_test_connection, BadCredentialsException(), "invalid_auth"),
|
||||
(_get_test_connection, FailedConnectionException(), "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_error(
|
||||
hass: HomeAssistant,
|
||||
mock_appliance_client: AsyncMock,
|
||||
mock_token_manager: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
get_mock: Callable[..., AsyncMock],
|
||||
exception: Exception,
|
||||
error_name: str,
|
||||
) -> None:
|
||||
"""Test reconfigure flow with bad credentials."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
get_mock(
|
||||
mock_appliance_client=mock_appliance_client,
|
||||
mock_token_manager=mock_token_manager,
|
||||
).side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=invalid_user_input
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
assert result["errors"] == {"base": error_name}
|
||||
|
||||
get_mock(
|
||||
mock_appliance_client=mock_appliance_client,
|
||||
mock_token_manager=mock_token_manager,
|
||||
).side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=valid_user_input
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
|
||||
assert mock_config_entry.data == valid_user_input
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_appliance_client")
|
||||
async def test_reconfigure_mismatched_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_token_manager: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reconfigure flow mismatched user id error."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_token_manager.get_user_id.return_value = "different_user_id"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=valid_user_input
|
||||
)
|
||||
|
||||
assert result["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "unique_id_mismatch"
|
||||
|
||||
Reference in New Issue
Block a user