Add reconfigure flow to Mealie (#121750)

This commit is contained in:
Joost Lekkerkerker 2024-07-11 09:11:59 +02:00 committed by GitHub
parent c6f1ec34e2
commit 4c972694cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 148 additions and 1 deletions

View File

@ -113,3 +113,39 @@ class MealieConfigFlow(ConfigFlow, domain=DOMAIN):
data_schema=REAUTH_SCHEMA,
errors=errors,
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration of the integration."""
self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reconfiguration confirmation."""
errors: dict[str, str] = {}
if user_input:
self.host = user_input[CONF_HOST]
errors, user_id = await self.check_connection(
user_input[CONF_API_TOKEN],
)
if not errors:
assert self.entry
if self.entry.unique_id == user_id:
return self.async_update_reload_and_abort(
self.entry,
data={
**self.entry.data,
CONF_HOST: user_input[CONF_HOST],
CONF_API_TOKEN: user_input[CONF_API_TOKEN],
},
reason="reconfigure_successful",
)
return self.async_abort(reason="wrong_account")
return self.async_show_form(
step_id="reconfigure_confirm",
data_schema=USER_SCHEMA,
errors=errors,
)

View File

@ -15,6 +15,13 @@
"data": {
"api_token": "[%key:common::config_flow::data::api_token%]"
}
},
"reconfigure_confirm": {
"description": "Please reconfigure with Mealie.",
"data": {
"host": "[%key:common::config_flow::data::url%]",
"api_token": "[%key:common::config_flow::data::api_token%]"
}
}
},
"error": {
@ -26,6 +33,7 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]",
"wrong_account": "You have to use the same account that was used to configure the integration."
}
},

View File

@ -6,7 +6,7 @@ from aiomealie import About, MealieAuthenticationError, MealieConnectionError
import pytest
from homeassistant.components.mealie.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER
from homeassistant.const import CONF_API_TOKEN, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -243,3 +243,106 @@ async def test_reauth_flow_exceptions(
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_mealie_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow."""
await setup_integration(hass, mock_config_entry)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
data=mock_config_entry.data,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "http://test:9090", CONF_API_TOKEN: "token2"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.data[CONF_API_TOKEN] == "token2"
assert mock_config_entry.data[CONF_HOST] == "http://test:9090"
async def test_reconfigure_flow_wrong_account(
hass: HomeAssistant,
mock_mealie_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test reconfigure flow with wrong account."""
await setup_integration(hass, mock_config_entry)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
data=mock_config_entry.data,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
mock_mealie_client.get_user_info.return_value.user_id = "wrong_user_id"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "http://test:9090", CONF_API_TOKEN: "token2"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "wrong_account"
@pytest.mark.parametrize(
("exception", "error"),
[
(MealieConnectionError, "cannot_connect"),
(MealieAuthenticationError, "invalid_auth"),
(Exception, "unknown"),
],
)
async def test_reconfigure_flow_exceptions(
hass: HomeAssistant,
mock_mealie_client: AsyncMock,
mock_setup_entry: AsyncMock,
mock_config_entry: MockConfigEntry,
exception: Exception,
error: str,
) -> None:
"""Test reconfigure flow errors."""
await setup_integration(hass, mock_config_entry)
mock_mealie_client.get_user_info.side_effect = exception
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_RECONFIGURE, "entry_id": mock_config_entry.entry_id},
data=mock_config_entry.data,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "http://test:9090", CONF_API_TOKEN: "token"},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure_confirm"
assert result["errors"] == {"base": error}
mock_mealie_client.get_user_info.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "http://test:9090", CONF_API_TOKEN: "token"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"