Add reconfiguration flow to Habitica (#136038)
parent
9e37c0dc8f
commit
ff80a7c5bc
|
@ -40,6 +40,7 @@ from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
FORGOT_PASSWORD_URL,
|
FORGOT_PASSWORD_URL,
|
||||||
HABITICANS_URL,
|
HABITICANS_URL,
|
||||||
|
SECTION_DANGER_ZONE,
|
||||||
SECTION_REAUTH_API_KEY,
|
SECTION_REAUTH_API_KEY,
|
||||||
SECTION_REAUTH_LOGIN,
|
SECTION_REAUTH_LOGIN,
|
||||||
SIGN_UP_URL,
|
SIGN_UP_URL,
|
||||||
|
@ -105,6 +106,21 @@ STEP_REAUTH_DATA_SCHEMA = vol.Schema(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
STEP_RECONF_DATA_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_API_KEY): str,
|
||||||
|
vol.Required(SECTION_DANGER_ZONE): data_entry_flow.section(
|
||||||
|
vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_URL): str,
|
||||||
|
vol.Required(CONF_VERIFY_SSL): bool,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
{"collapsed": True},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -260,6 +276,50 @@ class HabiticaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
errors=errors,
|
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] = {}
|
||||||
|
reconf_entry = self._get_reconfigure_entry()
|
||||||
|
suggested_values = {
|
||||||
|
CONF_API_KEY: reconf_entry.data[CONF_API_KEY],
|
||||||
|
SECTION_DANGER_ZONE: {
|
||||||
|
CONF_URL: reconf_entry.data[CONF_URL],
|
||||||
|
CONF_VERIFY_SSL: reconf_entry.data.get(CONF_VERIFY_SSL, True),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if user_input:
|
||||||
|
errors, user = await self.validate_api_key(
|
||||||
|
{
|
||||||
|
**reconf_entry.data,
|
||||||
|
**user_input,
|
||||||
|
**user_input[SECTION_DANGER_ZONE],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if not errors and user is not None:
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
reconf_entry,
|
||||||
|
data_updates={
|
||||||
|
CONF_API_KEY: user_input[CONF_API_KEY],
|
||||||
|
**user_input[SECTION_DANGER_ZONE],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure",
|
||||||
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
data_schema=STEP_RECONF_DATA_SCHEMA,
|
||||||
|
suggested_values=user_input or suggested_values,
|
||||||
|
),
|
||||||
|
errors=errors,
|
||||||
|
description_placeholders={
|
||||||
|
"site_data": SITE_DATA_URL,
|
||||||
|
"habiticans": HABITICANS_URL,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
async def validate_login(
|
async def validate_login(
|
||||||
self, user_input: Mapping[str, Any]
|
self, user_input: Mapping[str, Any]
|
||||||
) -> tuple[dict[str, str], LoginData | None, UserData | None]:
|
) -> tuple[dict[str, str], LoginData | None, UserData | None]:
|
||||||
|
|
|
@ -56,3 +56,4 @@ X_CLIENT = f"{DEVELOPER_ID} - {APPLICATION_NAME} {__version__}"
|
||||||
|
|
||||||
SECTION_REAUTH_LOGIN = "reauth_login"
|
SECTION_REAUTH_LOGIN = "reauth_login"
|
||||||
SECTION_REAUTH_API_KEY = "reauth_api_key"
|
SECTION_REAUTH_API_KEY = "reauth_api_key"
|
||||||
|
SECTION_DANGER_ZONE = "danger_zone"
|
||||||
|
|
|
@ -66,7 +66,7 @@ rules:
|
||||||
entity-translations: done
|
entity-translations: done
|
||||||
exception-translations: done
|
exception-translations: done
|
||||||
icon-translations: done
|
icon-translations: done
|
||||||
reconfiguration-flow: todo
|
reconfiguration-flow: done
|
||||||
repair-issues:
|
repair-issues:
|
||||||
status: done
|
status: done
|
||||||
comment: Used to inform of deprecated entities and actions.
|
comment: Used to inform of deprecated entities and actions.
|
||||||
|
|
|
@ -13,7 +13,8 @@
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||||
"unique_id_mismatch": "Hmm, those login details are correct, but they're not for this adventurer. Got another account to try?",
|
"unique_id_mismatch": "Hmm, those login details are correct, but they're not for this adventurer. Got another account to try?",
|
||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
|
@ -85,6 +86,30 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"title": "Update Habitica configuration",
|
||||||
|
"description": "![Habiticans]({habiticans})\n\nEnter your new API token below. You can find it in Habitica under [**Settings -> Site Data**]({site_data})",
|
||||||
|
"data": {
|
||||||
|
"api_key": "[%key:component::habitica::config::step::advanced::data::api_key%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"api_key": "[%key:component::habitica::config::step::advanced::data_description::api_key%]"
|
||||||
|
},
|
||||||
|
"sections": {
|
||||||
|
"danger_zone": {
|
||||||
|
"name": "Critical configuration options",
|
||||||
|
"description": "These settings impact core functionality. Modifications are unnecessary if connected to the official Habitica instance and may disrupt the integration. Proceed with caution.",
|
||||||
|
"data": {
|
||||||
|
"url": "[%key:common::config_flow::data::url%]",
|
||||||
|
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"url": "URL of the Habitica instance",
|
||||||
|
"verify_ssl": "[%key:component::habitica::config::step::advanced::data_description::verify_ssl%]"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -9,6 +9,7 @@ from homeassistant.components.habitica.const import (
|
||||||
CONF_API_USER,
|
CONF_API_USER,
|
||||||
DEFAULT_URL,
|
DEFAULT_URL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
SECTION_DANGER_ZONE,
|
||||||
SECTION_REAUTH_API_KEY,
|
SECTION_REAUTH_API_KEY,
|
||||||
SECTION_REAUTH_LOGIN,
|
SECTION_REAUTH_LOGIN,
|
||||||
)
|
)
|
||||||
|
@ -54,6 +55,13 @@ USER_INPUT_REAUTH_API_KEY = {
|
||||||
SECTION_REAUTH_LOGIN: {},
|
SECTION_REAUTH_LOGIN: {},
|
||||||
SECTION_REAUTH_API_KEY: {CONF_API_KEY: "cd0e5985-17de-4b4f-849e-5d506c5e4382"},
|
SECTION_REAUTH_API_KEY: {CONF_API_KEY: "cd0e5985-17de-4b4f-849e-5d506c5e4382"},
|
||||||
}
|
}
|
||||||
|
USER_INPUT_RECONFIGURE = {
|
||||||
|
CONF_API_KEY: "cd0e5985-17de-4b4f-849e-5d506c5e4382",
|
||||||
|
SECTION_DANGER_ZONE: {
|
||||||
|
CONF_URL: DEFAULT_URL,
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("habitica")
|
@pytest.mark.usefixtures("habitica")
|
||||||
|
@ -449,3 +457,79 @@ async def test_flow_reauth_unique_id_mismatch(hass: HomeAssistant) -> None:
|
||||||
assert result["reason"] == "unique_id_mismatch"
|
assert result["reason"] == "unique_id_mismatch"
|
||||||
|
|
||||||
assert len(hass.config_entries.async_entries()) == 1
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("habitica")
|
||||||
|
async def test_flow_reconfigure(
|
||||||
|
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
USER_INPUT_RECONFIGURE,
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data[CONF_API_KEY] == "cd0e5985-17de-4b4f-849e-5d506c5e4382"
|
||||||
|
assert config_entry.data[CONF_URL] == DEFAULT_URL
|
||||||
|
assert config_entry.data[CONF_VERIFY_SSL] is True
|
||||||
|
|
||||||
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("raise_error", "text_error"),
|
||||||
|
[
|
||||||
|
(ERROR_NOT_AUTHORIZED, "invalid_auth"),
|
||||||
|
(ERROR_BAD_REQUEST, "cannot_connect"),
|
||||||
|
(KeyError, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_flow_reconfigure_errors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
habitica: AsyncMock,
|
||||||
|
config_entry: MockConfigEntry,
|
||||||
|
raise_error: Exception,
|
||||||
|
text_error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow errors."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
result = await config_entry.start_reconfigure_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "reconfigure"
|
||||||
|
|
||||||
|
habitica.get_user.side_effect = raise_error
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
USER_INPUT_RECONFIGURE,
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {"base": text_error}
|
||||||
|
|
||||||
|
habitica.get_user.side_effect = None
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input=USER_INPUT_RECONFIGURE,
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data[CONF_API_KEY] == "cd0e5985-17de-4b4f-849e-5d506c5e4382"
|
||||||
|
assert config_entry.data[CONF_URL] == DEFAULT_URL
|
||||||
|
assert config_entry.data[CONF_VERIFY_SSL] is True
|
||||||
|
|
||||||
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
Loading…
Reference in New Issue