Skip to content

Commit 4a00311

Browse files
tr4nt0rfrenck
authored andcommitted
Improve migration to Uptime Kuma v2.0.0 (home-assistant#155055)
1 parent dcc3f14 commit 4a00311

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

homeassistant/components/uptime_kuma/coordinator.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
2020
from homeassistant.core import HomeAssistant, callback
2121
from homeassistant.exceptions import ConfigEntryAuthFailed
22-
from homeassistant.helpers import entity_registry as er
22+
from homeassistant.helpers import device_registry as dr, entity_registry as er
2323
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2424
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
2525

@@ -89,7 +89,8 @@ def async_migrate_entities_unique_ids(
8989
"""Migrate unique_ids in the entity registry after updating Uptime Kuma."""
9090

9191
if (
92-
coordinator.version is coordinator.api.version
92+
coordinator.version is None
93+
or coordinator.version.version == coordinator.api.version.version
9394
or int(coordinator.api.version.major) < 2
9495
):
9596
return
@@ -116,6 +117,32 @@ def async_migrate_entities_unique_ids(
116117
new_unique_id=f"{registry_entry.config_entry_id}_{monitor.monitor_id!s}_{registry_entry.translation_key}",
117118
)
118119

120+
# migrate device identifiers and update version
121+
device_reg = dr.async_get(hass)
122+
for monitor in metrics.values():
123+
if device := device_reg.async_get_device(
124+
{(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_name!s}")}
125+
):
126+
new_identifier = {
127+
(DOMAIN, f"{coordinator.config_entry.entry_id}_{monitor.monitor_id!s}")
128+
}
129+
device_reg.async_update_device(
130+
device.id,
131+
new_identifiers=new_identifier,
132+
sw_version=coordinator.api.version.version,
133+
)
134+
if device := device_reg.async_get_device(
135+
{(DOMAIN, f"{coordinator.config_entry.entry_id}_update")}
136+
):
137+
device_reg.async_update_device(
138+
device.id,
139+
sw_version=coordinator.api.version.version,
140+
)
141+
142+
hass.async_create_task(
143+
hass.config_entries.async_reload(coordinator.config_entry.entry_id)
144+
)
145+
119146

120147
class UptimeKumaSoftwareUpdateCoordinator(DataUpdateCoordinator[LatestRelease]):
121148
"""Uptime Kuma coordinator for retrieving update information."""

tests/components/uptime_kuma/test_sensor.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
from pythonkuma import MonitorStatus, UptimeKumaMonitor, UptimeKumaVersion
1010
from syrupy.assertion import SnapshotAssertion
1111

12+
from homeassistant.components.uptime_kuma.const import DOMAIN
1213
from homeassistant.config_entries import ConfigEntryState
1314
from homeassistant.const import Platform
1415
from homeassistant.core import HomeAssistant
15-
from homeassistant.helpers import entity_registry as er
16+
from homeassistant.helpers import device_registry as dr, entity_registry as er
1617

1718
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
1819

@@ -53,6 +54,7 @@ async def test_migrate_unique_id(
5354
snapshot: SnapshotAssertion,
5455
entity_registry: er.EntityRegistry,
5556
freezer: FrozenDateTimeFactory,
57+
device_registry: dr.DeviceRegistry,
5658
) -> None:
5759
"""Snapshot test states of sensor platform."""
5860
mock_pythonkuma.metrics.return_value = {
@@ -87,11 +89,18 @@ async def test_migrate_unique_id(
8789
)
8890
}
8991
mock_pythonkuma.version = UptimeKumaVersion(
90-
version="2.0.0-beta.3", major="2", minor="0", patch="0-beta.3"
92+
version="2.0.2", major="2", minor="0", patch="2"
9193
)
9294
freezer.tick(timedelta(seconds=30))
9395
async_fire_time_changed(hass)
9496
await hass.async_block_till_done()
9597

9698
assert (entity := entity_registry.async_get("sensor.monitor_status"))
9799
assert entity.unique_id == "123456789_1_status"
100+
101+
assert (
102+
device := device_registry.async_get_device(
103+
identifiers={(DOMAIN, f"{entity.config_entry_id}_1")}
104+
)
105+
)
106+
assert device.sw_version == "2.0.2"

0 commit comments

Comments
 (0)