mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 09:38:21 +00:00
Add hassio discovery flow to Uptime Kuma (#148770)
This commit is contained in:
parent
ba4e7e50e0
commit
c4d4ef884e
@ -23,6 +23,7 @@ from homeassistant.helpers.selector import (
|
|||||||
TextSelectorConfig,
|
TextSelectorConfig,
|
||||||
TextSelectorType,
|
TextSelectorType,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ async def validate_connection(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
url: URL | str,
|
url: URL | str,
|
||||||
verify_ssl: bool,
|
verify_ssl: bool,
|
||||||
api_key: str,
|
api_key: str | None,
|
||||||
) -> dict[str, str]:
|
) -> dict[str, str]:
|
||||||
"""Validate Uptime Kuma connectivity."""
|
"""Validate Uptime Kuma connectivity."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
@ -69,6 +70,8 @@ async def validate_connection(
|
|||||||
class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Uptime Kuma."""
|
"""Handle a config flow for Uptime Kuma."""
|
||||||
|
|
||||||
|
_hassio_discovery: HassioServiceInfo | None = None
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
@ -168,3 +171,61 @@ class UptimeKumaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
),
|
),
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_hassio(
|
||||||
|
self, discovery_info: HassioServiceInfo
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Prepare configuration for Uptime Kuma add-on.
|
||||||
|
|
||||||
|
This flow is triggered by the discovery component.
|
||||||
|
"""
|
||||||
|
self._async_abort_entries_match({CONF_URL: discovery_info.config[CONF_URL]})
|
||||||
|
await self.async_set_unique_id(discovery_info.uuid)
|
||||||
|
self._abort_if_unique_id_configured(
|
||||||
|
updates={CONF_URL: discovery_info.config[CONF_URL]}
|
||||||
|
)
|
||||||
|
|
||||||
|
self._hassio_discovery = discovery_info
|
||||||
|
return await self.async_step_hassio_confirm()
|
||||||
|
|
||||||
|
async def async_step_hassio_confirm(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Confirm Supervisor discovery."""
|
||||||
|
assert self._hassio_discovery
|
||||||
|
errors: dict[str, str] = {}
|
||||||
|
api_key = user_input[CONF_API_KEY] if user_input else None
|
||||||
|
|
||||||
|
if not (
|
||||||
|
errors := await validate_connection(
|
||||||
|
self.hass,
|
||||||
|
self._hassio_discovery.config[CONF_URL],
|
||||||
|
True,
|
||||||
|
api_key,
|
||||||
|
)
|
||||||
|
):
|
||||||
|
if user_input is None:
|
||||||
|
self._set_confirm_only()
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="hassio_confirm",
|
||||||
|
description_placeholders={
|
||||||
|
"addon": self._hassio_discovery.config["addon"]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return self.async_create_entry(
|
||||||
|
title=self._hassio_discovery.slug,
|
||||||
|
data={
|
||||||
|
CONF_URL: self._hassio_discovery.config[CONF_URL],
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: api_key,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="hassio_confirm",
|
||||||
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
|
data_schema=STEP_REAUTH_DATA_SCHEMA, suggested_values=user_input
|
||||||
|
),
|
||||||
|
description_placeholders={"addon": self._hassio_discovery.config["addon"]},
|
||||||
|
errors=errors if user_input is not None else None,
|
||||||
|
)
|
||||||
|
@ -44,12 +44,10 @@ rules:
|
|||||||
# Gold
|
# Gold
|
||||||
devices: done
|
devices: done
|
||||||
diagnostics: done
|
diagnostics: done
|
||||||
discovery-update-info:
|
discovery-update-info: done
|
||||||
status: exempt
|
|
||||||
comment: is not locally discoverable
|
|
||||||
discovery:
|
discovery:
|
||||||
status: exempt
|
status: done
|
||||||
comment: is not locally discoverable
|
comment: hassio addon supports discovery, other installation methods are not discoverable
|
||||||
docs-data-update: done
|
docs-data-update: done
|
||||||
docs-examples: todo
|
docs-examples: todo
|
||||||
docs-known-limitations: done
|
docs-known-limitations: done
|
||||||
|
@ -36,6 +36,16 @@
|
|||||||
"verify_ssl": "[%key:component::uptime_kuma::config::step::user::data_description::verify_ssl%]",
|
"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%]"
|
"api_key": "[%key:component::uptime_kuma::config::step::user::data_description::api_key%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"hassio_confirm": {
|
||||||
|
"title": "Uptime Kuma via Home Assistant add-on",
|
||||||
|
"description": "Do you want to configure Home Assistant to connect to the Uptime Kuma service provided by the add-on: {addon}?",
|
||||||
|
"data": {
|
||||||
|
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||||
|
},
|
||||||
|
"data_description": {
|
||||||
|
"api_key": "[%key:component::uptime_kuma::config::step::user::data_description::api_key%]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
|
@ -10,9 +10,20 @@ from pythonkuma.update import LatestRelease
|
|||||||
|
|
||||||
from homeassistant.components.uptime_kuma.const import DOMAIN
|
from homeassistant.components.uptime_kuma.const import DOMAIN
|
||||||
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.helpers.service_info.hassio import HassioServiceInfo
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
ADDON_SERVICE_INFO = HassioServiceInfo(
|
||||||
|
config={
|
||||||
|
"addon": "Uptime Kuma",
|
||||||
|
CONF_URL: "http://localhost:3001/",
|
||||||
|
},
|
||||||
|
name="Uptime Kuma",
|
||||||
|
slug="a0d7b954_uptime-kuma",
|
||||||
|
uuid="1234",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||||
|
@ -6,11 +6,13 @@ import pytest
|
|||||||
from pythonkuma import UptimeKumaAuthenticationException, UptimeKumaConnectionException
|
from pythonkuma import UptimeKumaAuthenticationException, UptimeKumaConnectionException
|
||||||
|
|
||||||
from homeassistant.components.uptime_kuma.const import DOMAIN
|
from homeassistant.components.uptime_kuma.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_IGNORE, SOURCE_USER
|
||||||
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.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
|
from .conftest import ADDON_SERVICE_INFO
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
@ -280,3 +282,201 @@ async def test_flow_reconfigure_errors(
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert len(hass.config_entries.async_entries()) == 1
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_hassio_addon_discovery(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_pythonkuma: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test config flow initiated by Supervisor."""
|
||||||
|
mock_pythonkuma.metrics.side_effect = [UptimeKumaAuthenticationException, None]
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "hassio_confirm"
|
||||||
|
assert result["description_placeholders"] == {"addon": "Uptime Kuma"}
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{CONF_API_KEY: "apikey"},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
assert result["title"] == "a0d7b954_uptime-kuma"
|
||||||
|
assert result["data"] == {
|
||||||
|
CONF_URL: "http://localhost:3001/",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: "apikey",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_hassio_addon_discovery_confirm_only(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test config flow initiated by Supervisor.
|
||||||
|
|
||||||
|
Config flow will first try to configure without authentication and if it
|
||||||
|
fails will show the form.
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "hassio_confirm"
|
||||||
|
assert result["description_placeholders"] == {"addon": "Uptime Kuma"}
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
assert result["title"] == "a0d7b954_uptime-kuma"
|
||||||
|
assert result["data"] == {
|
||||||
|
CONF_URL: "http://localhost:3001/",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: None,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_hassio_addon_discovery_already_configured(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test config flow initiated by Supervisor."""
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={
|
||||||
|
CONF_URL: "http://localhost:3001/",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: "apikey",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("raise_error", "text_error"),
|
||||||
|
[
|
||||||
|
(UptimeKumaConnectionException, "cannot_connect"),
|
||||||
|
(UptimeKumaAuthenticationException, "invalid_auth"),
|
||||||
|
(ValueError, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_hassio_addon_discovery_errors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_pythonkuma: AsyncMock,
|
||||||
|
raise_error: Exception,
|
||||||
|
text_error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test we handle errors and recover."""
|
||||||
|
mock_pythonkuma.metrics.side_effect = UptimeKumaAuthenticationException
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_pythonkuma.metrics.side_effect = raise_error
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{CONF_API_KEY: "apikey"},
|
||||||
|
)
|
||||||
|
|
||||||
|
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_API_KEY: "apikey"},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
assert result["title"] == "a0d7b954_uptime-kuma"
|
||||||
|
assert result["data"] == {
|
||||||
|
CONF_URL: "http://localhost:3001/",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: "apikey",
|
||||||
|
}
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_hassio_addon_discovery_ignored(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test we abort discovery flow if discovery was ignored."""
|
||||||
|
|
||||||
|
MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
source=SOURCE_IGNORE,
|
||||||
|
data={},
|
||||||
|
entry_id="123456789",
|
||||||
|
unique_id="1234",
|
||||||
|
).add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||||
|
async def test_hassio_addon_discovery_update_info(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Test we abort discovery flow if already configured and we update from discovery info."""
|
||||||
|
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
title="a0d7b954_uptime-kuma",
|
||||||
|
data={
|
||||||
|
CONF_URL: "http://localhost:80/",
|
||||||
|
CONF_VERIFY_SSL: True,
|
||||||
|
CONF_API_KEY: "apikey",
|
||||||
|
},
|
||||||
|
entry_id="123456789",
|
||||||
|
unique_id="1234",
|
||||||
|
)
|
||||||
|
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
data=ADDON_SERVICE_INFO,
|
||||||
|
context={"source": SOURCE_HASSIO},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
assert entry.data[CONF_URL] == "http://localhost:3001/"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user