mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Drop support for Uptime Kuma < 2.0.0 (#187418)
This commit is contained in:
@@ -68,6 +68,9 @@ async def validate_connection(
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
if int(uptime_kuma.version.major) < 2:
|
||||
errors["base"] = "version_unsupported"
|
||||
return errors
|
||||
|
||||
|
||||
|
||||
@@ -11,15 +11,13 @@ from pythonkuma import (
|
||||
UptimeKumaException,
|
||||
UptimeKumaMonitor,
|
||||
UptimeKumaParseException,
|
||||
UptimeKumaVersion,
|
||||
)
|
||||
from pythonkuma.update import LatestRelease, UpdateChecker
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryError
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
@@ -56,7 +54,6 @@ class UptimeKumaDataUpdateCoordinator(
|
||||
self.api = UptimeKuma(
|
||||
session, config_entry.data[CONF_URL], config_entry.data[CONF_API_KEY]
|
||||
)
|
||||
self.version: UptimeKumaVersion | None = None
|
||||
|
||||
@override
|
||||
async def _async_update_data(self) -> dict[str | int, UptimeKumaMonitor]:
|
||||
@@ -82,78 +79,15 @@ class UptimeKumaDataUpdateCoordinator(
|
||||
translation_key="request_failed_exception",
|
||||
) from e
|
||||
else:
|
||||
async_migrate_entities_unique_ids(self.hass, self, metrics)
|
||||
self.version = self.api.version
|
||||
if int(self.api.version.major) < 2:
|
||||
raise ConfigEntryError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="unsupported_version",
|
||||
)
|
||||
|
||||
return metrics
|
||||
|
||||
|
||||
@callback
|
||||
def async_migrate_entities_unique_ids(
|
||||
hass: HomeAssistant,
|
||||
coordinator: UptimeKumaDataUpdateCoordinator,
|
||||
metrics: dict[str | int, UptimeKumaMonitor],
|
||||
) -> None:
|
||||
"""Migrate unique_ids in the entity registry after updating Uptime Kuma."""
|
||||
|
||||
if (
|
||||
coordinator.version is None
|
||||
or coordinator.version.version == coordinator.api.version.version
|
||||
or int(coordinator.api.version.major) < 2
|
||||
):
|
||||
return
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
registry_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, coordinator.config_entry.entry_id
|
||||
)
|
||||
|
||||
for registry_entry in registry_entries:
|
||||
name = registry_entry.unique_id.removeprefix(
|
||||
f"{registry_entry.config_entry_id}_"
|
||||
).removesuffix(f"_{registry_entry.translation_key}")
|
||||
if monitor := next(
|
||||
(
|
||||
m
|
||||
for m in metrics.values()
|
||||
if m.monitor_name == name and m.monitor_id is not None
|
||||
),
|
||||
None,
|
||||
):
|
||||
entity_registry.async_update_entity(
|
||||
registry_entry.entity_id,
|
||||
new_unique_id=f"{registry_entry.config_entry_id}_{monitor.monitor_id!s}_{registry_entry.translation_key}",
|
||||
)
|
||||
|
||||
# migrate device identifiers and update version
|
||||
device_reg = dr.async_get(hass)
|
||||
for monitor in metrics.values():
|
||||
if device := device_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_name!s}"),
|
||||
coordinator.config_entry.entry_id,
|
||||
):
|
||||
new_identifier = {
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_id!s}")
|
||||
}
|
||||
device_reg.async_update_device(
|
||||
device.id,
|
||||
new_identifiers=new_identifier,
|
||||
sw_version=coordinator.api.version.version,
|
||||
)
|
||||
if device := device_reg.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{coordinator.config_entry.entry_id}_update"),
|
||||
coordinator.config_entry.entry_id,
|
||||
):
|
||||
device_reg.async_update_device(
|
||||
device.id,
|
||||
sw_version=coordinator.api.version.version,
|
||||
)
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_reload(coordinator.config_entry.entry_id)
|
||||
)
|
||||
|
||||
|
||||
class UptimeKumaSoftwareUpdateCoordinator(DataUpdateCoordinator[LatestRelease]):
|
||||
"""Uptime Kuma coordinator for retrieving update information."""
|
||||
|
||||
|
||||
@@ -17,9 +17,7 @@ async def async_get_config_entry_diagnostics(
|
||||
"""Return diagnostics for a config entry."""
|
||||
|
||||
return {
|
||||
"version": entry.runtime_data.version.version
|
||||
if entry.runtime_data.version
|
||||
else None,
|
||||
"version": entry.runtime_data.api.version.version,
|
||||
"monitors": async_redact_data(
|
||||
{k: asdict(v) for k, v in entry.runtime_data.data.items()}, TO_REDACT
|
||||
),
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
from collections.abc import Callable, Mapping
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
from typing import Any, override
|
||||
from typing import TYPE_CHECKING, Any, override
|
||||
|
||||
from pythonkuma import MonitorType, UptimeKumaMonitor
|
||||
from pythonkuma.models import MonitorStatus
|
||||
@@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
|
||||
)
|
||||
from homeassistant.const import CONF_URL, PERCENTAGE, EntityCategory, UnitOfTime
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
@@ -247,12 +248,7 @@ class UptimeKumaSensorEntity(
|
||||
)
|
||||
|
||||
url = URL(coordinator.config_entry.data[CONF_URL]) / "dashboard"
|
||||
if url.host in LOCAL_INSTANCE:
|
||||
configuration_url = None
|
||||
elif isinstance(monitor, int):
|
||||
configuration_url = url / str(monitor)
|
||||
else:
|
||||
configuration_url = url
|
||||
configuration_url = None if url.host in LOCAL_INSTANCE else url / str(monitor)
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
@@ -283,3 +279,22 @@ class UptimeKumaSensorEntity(
|
||||
if (fn := self.entity_description.attributes_fn) is not None:
|
||||
return fn(self.coordinator.data[self.monitor])
|
||||
return super().extra_state_attributes
|
||||
|
||||
@override
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(self.device_info, DeviceInfo)
|
||||
if self.device_info.sw_version != self.coordinator.api.version.version:
|
||||
device_registry = dr.async_get(self.hass)
|
||||
if device_entry := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{self.coordinator.config_entry.entry_id}_{self.monitor!s}"),
|
||||
self.coordinator.config_entry.entry_id,
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_entry.id, sw_version=self.coordinator.api.version.version
|
||||
)
|
||||
self.device_info.sw_version = self.coordinator.api.version.version
|
||||
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||
"invalid_data": "Invalid data received, check the URL",
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]",
|
||||
"version_unsupported": "Uptime Kuma 2.0.0 or newer is required"
|
||||
},
|
||||
"step": {
|
||||
"hassio_confirm": {
|
||||
@@ -167,6 +168,9 @@
|
||||
"request_failed_exception": {
|
||||
"message": "Connection to Uptime Kuma failed"
|
||||
},
|
||||
"unsupported_version": {
|
||||
"message": "Uptime Kuma versions older than 2.0.0 are no longer supported. Please upgrade to Uptime Kuma 2.0.0 or newer to use this integration."
|
||||
},
|
||||
"update_check_failed": {
|
||||
"message": "Failed to check for latest Uptime Kuma update"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Update platform for the Uptime Kuma integration."""
|
||||
|
||||
from enum import StrEnum
|
||||
from typing import override
|
||||
from typing import TYPE_CHECKING, override
|
||||
|
||||
from yarl import URL
|
||||
|
||||
@@ -11,7 +11,8 @@ from homeassistant.components.update import (
|
||||
UpdateEntityFeature,
|
||||
)
|
||||
from homeassistant.const import CONF_URL, EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
@@ -131,3 +132,22 @@ class UptimeKumaUpdateEntity(
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return super().available and self.update_checker.last_update_success
|
||||
|
||||
@override
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(self.device_info, DeviceInfo)
|
||||
if self.device_info.sw_version != self.coordinator.api.version.version:
|
||||
device_registry = dr.async_get(self.hass)
|
||||
if device_entry := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, self.coordinator.config_entry.entry_id),
|
||||
self.coordinator.config_entry.entry_id,
|
||||
):
|
||||
device_registry.async_update_device(
|
||||
device_entry.id, sw_version=self.coordinator.api.version.version
|
||||
)
|
||||
self.device_info.sw_version = self.coordinator.api.version.version
|
||||
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
@@ -8,6 +8,7 @@ from pythonkuma import (
|
||||
UptimeKumaAuthenticationException,
|
||||
UptimeKumaConnectionException,
|
||||
UptimeKumaParseException,
|
||||
UptimeKumaVersion,
|
||||
)
|
||||
|
||||
from homeassistant.components.uptime_kuma.const import DOMAIN
|
||||
@@ -103,6 +104,54 @@ async def test_form_errors(
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_error_unsupported_version(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_pythonkuma: AsyncMock,
|
||||
) -> None:
|
||||
"""Test we handle unsupported version error and recover."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="1.23.16", major="1", minor="23", patch="16"
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_URL: "https://uptime.example.org/",
|
||||
CONF_VERIFY_SSL: True,
|
||||
CONF_API_KEY: "apikey",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "version_unsupported"}
|
||||
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="2.0.0", major="2", minor="0", patch="0"
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_URL: "https://uptime.example.org/",
|
||||
CONF_VERIFY_SSL: True,
|
||||
CONF_API_KEY: "apikey",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "uptime.example.org"
|
||||
assert result["data"] == {
|
||||
CONF_URL: "https://uptime.example.org/",
|
||||
CONF_VERIFY_SSL: True,
|
||||
CONF_API_KEY: "apikey",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||
async def test_form_already_configured(
|
||||
hass: HomeAssistant,
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
"""Tests for the Uptime Kuma integration."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
from pytest_homeassistant.typing import WebSocketGenerator
|
||||
from pythonkuma import (
|
||||
UptimeKumaAuthenticationException,
|
||||
UptimeKumaException,
|
||||
UptimeKumaParseException,
|
||||
UptimeKumaVersion,
|
||||
)
|
||||
|
||||
from homeassistant.components.uptime_kuma.const import DOMAIN
|
||||
@@ -60,6 +63,23 @@ async def test_config_entry_not_ready(
|
||||
assert config_entry.state is state
|
||||
|
||||
|
||||
async def test_config_entry_error(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pythonkuma: AsyncMock,
|
||||
) -> None:
|
||||
"""Test config entry not ready."""
|
||||
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="1.23.16", major="1", minor="23", patch="16"
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_config_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
@@ -175,3 +195,44 @@ async def test_remove_entry_device(
|
||||
assert device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, "123456789"), config_entry.entry_id
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_pythonkuma")
|
||||
async def test_device_info_sw_version_update(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_pythonkuma: AsyncMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test device info is updated on Uptime Kuma version change."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert (
|
||||
device_entry := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, "123456789_1"), config_entry.entry_id
|
||||
)
|
||||
)
|
||||
|
||||
assert device_entry.sw_version == "2.0.0"
|
||||
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="2.5.6", major="2", minor="5", patch="6"
|
||||
)
|
||||
|
||||
freezer.tick(timedelta(seconds=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
device_entry := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, "123456789_1"), config_entry.entry_id
|
||||
)
|
||||
)
|
||||
|
||||
assert device_entry.sw_version == "2.5.6"
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
"""Test for Uptime Kuma sensor platform."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from pytest_homeassistant.common import (
|
||||
MockConfigEntry,
|
||||
async_fire_time_changed,
|
||||
snapshot_platform,
|
||||
)
|
||||
from pythonkuma import MonitorStatus, UptimeKumaMonitor, UptimeKumaVersion
|
||||
from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.uptime_kuma.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -47,65 +39,3 @@ async def test_setup(
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_migrate_unique_id(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pythonkuma: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
) -> None:
|
||||
"""Snapshot test states of sensor platform."""
|
||||
mock_pythonkuma.metrics.return_value = {
|
||||
"Monitor": UptimeKumaMonitor(
|
||||
monitor_name="Monitor",
|
||||
monitor_hostname="null",
|
||||
monitor_port="null",
|
||||
monitor_status=MonitorStatus.UP,
|
||||
monitor_url="test",
|
||||
monitor_tags=["tag1", "tag2:value"],
|
||||
)
|
||||
}
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="1.23.16", major="1", minor="23", patch="16"
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert (entity := entity_registry.async_get("sensor.monitor_status"))
|
||||
assert entity.unique_id == "123456789_Monitor_status"
|
||||
|
||||
mock_pythonkuma.metrics.return_value = {
|
||||
1: UptimeKumaMonitor(
|
||||
monitor_id=1,
|
||||
monitor_name="Monitor",
|
||||
monitor_hostname="null",
|
||||
monitor_port="null",
|
||||
monitor_status=MonitorStatus.UP,
|
||||
monitor_url="test",
|
||||
monitor_tags=["tag1", "tag2:value"],
|
||||
)
|
||||
}
|
||||
mock_pythonkuma.version = UptimeKumaVersion(
|
||||
version="2.0.2", major="2", minor="0", patch="2"
|
||||
)
|
||||
freezer.tick(timedelta(seconds=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (entity := entity_registry.async_get("sensor.monitor_status"))
|
||||
assert entity.unique_id == "123456789_1_status"
|
||||
|
||||
assert (
|
||||
device := device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{entity.config_entry_id}_1"), config_entry.entry_id
|
||||
)
|
||||
)
|
||||
assert device.sw_version == "2.0.2"
|
||||
|
||||
Reference in New Issue
Block a user