mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Add reconfigure flow to Uptime Kuma (#148833)
This commit is contained in:
parent
828f0f8b26
commit
d46e0e132b
@ -16,6 +16,7 @@ from yarl import URL
|
|||||||
|
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
|
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.selector import (
|
from homeassistant.helpers.selector import (
|
||||||
TextSelector,
|
TextSelector,
|
||||||
@ -42,6 +43,29 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
|
|||||||
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Optional(CONF_API_KEY, default=""): str})
|
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Optional(CONF_API_KEY, default=""): str})
|
||||||
|
|
||||||
|
|
||||||
|
async def validate_connection(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
url: URL | str,
|
||||||
|
verify_ssl: bool,
|
||||||
|
api_key: str,
|
||||||
|
) -> dict[str, str]:
|
||||||
|
"""Validate Uptime Kuma connectivity."""
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
session = async_get_clientsession(hass, verify_ssl)
|
||||||
|
uptime_kuma = UptimeKuma(session, url, api_key)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await uptime_kuma.metrics()
|
||||||
|
except UptimeKumaAuthenticationException:
|
||||||
|
errors["base"] = "invalid_auth"
|
||||||
|
except UptimeKumaException:
|
||||||
|
errors["base"] = "cannot_connect"
|
||||||
|
except Exception:
|
||||||
|
_LOGGER.exception("Unexpected exception")
|
||||||
|
errors["base"] = "unknown"
|
||||||
|
return errors
|
||||||
|
|
||||||
|
|
||||||
class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Uptime Kuma."""
|
"""Handle a config flow for Uptime Kuma."""
|
||||||
|
|
||||||
@ -54,19 +78,14 @@ class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
url = URL(user_input[CONF_URL])
|
url = URL(user_input[CONF_URL])
|
||||||
self._async_abort_entries_match({CONF_URL: url.human_repr()})
|
self._async_abort_entries_match({CONF_URL: url.human_repr()})
|
||||||
|
|
||||||
session = async_get_clientsession(self.hass, user_input[CONF_VERIFY_SSL])
|
if not (
|
||||||
uptime_kuma = UptimeKuma(session, url, user_input[CONF_API_KEY])
|
errors := await validate_connection(
|
||||||
|
self.hass,
|
||||||
try:
|
url,
|
||||||
await uptime_kuma.metrics()
|
user_input[CONF_VERIFY_SSL],
|
||||||
except UptimeKumaAuthenticationException:
|
user_input[CONF_API_KEY],
|
||||||
errors["base"] = "invalid_auth"
|
)
|
||||||
except UptimeKumaException:
|
):
|
||||||
errors["base"] = "cannot_connect"
|
|
||||||
except Exception:
|
|
||||||
_LOGGER.exception("Unexpected exception")
|
|
||||||
errors["base"] = "unknown"
|
|
||||||
else:
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=url.host or "",
|
title=url.host or "",
|
||||||
data={**user_input, CONF_URL: url.human_repr()},
|
data={**user_input, CONF_URL: url.human_repr()},
|
||||||
@ -95,23 +114,14 @@ class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
entry = self._get_reauth_entry()
|
entry = self._get_reauth_entry()
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
session = async_get_clientsession(self.hass, entry.data[CONF_VERIFY_SSL])
|
if not (
|
||||||
uptime_kuma = UptimeKuma(
|
errors := await validate_connection(
|
||||||
session,
|
self.hass,
|
||||||
entry.data[CONF_URL],
|
entry.data[CONF_URL],
|
||||||
|
entry.data[CONF_VERIFY_SSL],
|
||||||
user_input[CONF_API_KEY],
|
user_input[CONF_API_KEY],
|
||||||
)
|
)
|
||||||
|
):
|
||||||
try:
|
|
||||||
await uptime_kuma.metrics()
|
|
||||||
except UptimeKumaAuthenticationException:
|
|
||||||
errors["base"] = "invalid_auth"
|
|
||||||
except UptimeKumaException:
|
|
||||||
errors["base"] = "cannot_connect"
|
|
||||||
except Exception:
|
|
||||||
_LOGGER.exception("Unexpected exception")
|
|
||||||
errors["base"] = "unknown"
|
|
||||||
else:
|
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
entry,
|
entry,
|
||||||
data_updates=user_input,
|
data_updates=user_input,
|
||||||
@ -124,3 +134,37 @@ class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
),
|
),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reconfigure flow."""
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
|
entry = self._get_reconfigure_entry()
|
||||||
|
|
||||||
|
if user_input is not None:
|
||||||
|
url = URL(user_input[CONF_URL])
|
||||||
|
self._async_abort_entries_match({CONF_URL: url.human_repr()})
|
||||||
|
|
||||||
|
if not (
|
||||||
|
errors := await validate_connection(
|
||||||
|
self.hass,
|
||||||
|
url,
|
||||||
|
user_input[CONF_VERIFY_SSL],
|
||||||
|
user_input[CONF_API_KEY],
|
||||||
|
)
|
||||||
|
):
|
||||||
|
return self.async_update_reload_and_abort(
|
||||||
|
entry,
|
||||||
|
data_updates={**user_input, CONF_URL: url.human_repr()},
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="reconfigure",
|
||||||
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
data_schema=STEP_USER_DATA_SCHEMA,
|
||||||
|
suggested_values=user_input or entry.data,
|
||||||
|
),
|
||||||
|
errors=errors,
|
||||||
|
)
|
||||||
|
@ -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: exempt
|
status: exempt
|
||||||
comment: has no repairs
|
comment: has no repairs
|
||||||
|
@ -23,6 +23,19 @@
|
|||||||
"data_description": {
|
"data_description": {
|
||||||
"api_key": "[%key:component::uptime_kuma::config::step::user::data_description::api_key%]"
|
"api_key": "[%key:component::uptime_kuma::config::step::user::data_description::api_key%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"title": "Update configuration for Uptime Kuma",
|
||||||
|
"data": {
|
||||||
|
"url": "[%key:common::config_flow::data::url%]",
|
||||||
|
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
|
||||||
|
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"url": "[%key:component::uptime_kuma::config::step::user::data_description::url%]",
|
||||||
|
"verify_ssl": "[%key:component::uptime_kuma::config::step::user::data_description::verify_ssl%]",
|
||||||
|
"api_key": "[%key:component::uptime_kuma::config::step::user::data_description::api_key%]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
@ -32,7 +45,8 @@
|
|||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
"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%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
@ -190,3 +190,93 @@ async def test_flow_reauth_errors(
|
|||||||
assert config_entry.data[CONF_API_KEY] == "newapikey"
|
assert config_entry.data[CONF_API_KEY] == "newapikey"
|
||||||
|
|
||||||
assert len(hass.config_entries.async_entries()) == 1
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
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"],
|
||||||
|
{
|
||||||
|
CONF_URL: "https://uptime.example.org:3001/",
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
CONF_API_KEY: "newapikey",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data == {
|
||||||
|
CONF_URL: "https://uptime.example.org:3001/",
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
CONF_API_KEY: "newapikey",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("raise_error", "text_error"),
|
||||||
|
[
|
||||||
|
(UptimeKumaConnectionException, "cannot_connect"),
|
||||||
|
(UptimeKumaAuthenticationException, "invalid_auth"),
|
||||||
|
(ValueError, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_flow_reconfigure_errors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: MockConfigEntry,
|
||||||
|
mock_pythonkuma: AsyncMock,
|
||||||
|
raise_error: Exception,
|
||||||
|
text_error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test reconfigure flow errors and recover."""
|
||||||
|
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"
|
||||||
|
|
||||||
|
mock_pythonkuma.metrics.side_effect = raise_error
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_URL: "https://uptime.example.org:3001/",
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
CONF_API_KEY: "newapikey",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["errors"] == {"base": text_error}
|
||||||
|
|
||||||
|
mock_pythonkuma.metrics.side_effect = None
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_URL: "https://uptime.example.org:3001/",
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
CONF_API_KEY: "newapikey",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "reconfigure_successful"
|
||||||
|
assert config_entry.data == {
|
||||||
|
CONF_URL: "https://uptime.example.org:3001/",
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
CONF_API_KEY: "newapikey",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user