|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import logging |
| 6 | +from typing import Any |
6 | 7 |
|
7 | 8 | from pooldose.client import PooldoseClient |
8 | 9 | from pooldose.request_status import RequestStatus |
9 | 10 |
|
10 | 11 | from homeassistant.const import CONF_HOST, Platform |
11 | | -from homeassistant.core import HomeAssistant |
| 12 | +from homeassistant.core import HomeAssistant, callback |
12 | 13 | from homeassistant.exceptions import ConfigEntryNotReady |
| 14 | +from homeassistant.helpers import entity_registry as er |
13 | 15 |
|
14 | 16 | from .coordinator import PooldoseConfigEntry, PooldoseCoordinator |
15 | 17 |
|
|
18 | 20 | PLATFORMS: list[Platform] = [Platform.SENSOR] |
19 | 21 |
|
20 | 22 |
|
| 23 | +async def async_migrate_entry(hass: HomeAssistant, entry: PooldoseConfigEntry) -> bool: |
| 24 | + """Migrate old entry.""" |
| 25 | + # Version 1.1 -> 1.2: Migrate entity unique IDs |
| 26 | + # - ofa_orp_value -> ofa_orp_time |
| 27 | + # - ofa_ph_value -> ofa_ph_time |
| 28 | + if entry.version == 1 and entry.minor_version < 2: |
| 29 | + |
| 30 | + @callback |
| 31 | + def migrate_unique_id(entity_entry: er.RegistryEntry) -> dict[str, Any] | None: |
| 32 | + """Migrate entity unique IDs for pooldose sensors.""" |
| 33 | + new_unique_id = entity_entry.unique_id |
| 34 | + |
| 35 | + # Check if this entry needs migration |
| 36 | + if "_ofa_orp_value" in new_unique_id: |
| 37 | + new_unique_id = new_unique_id.replace("_ofa_orp_value", "_ofa_orp_time") |
| 38 | + elif "_ofa_ph_value" in new_unique_id: |
| 39 | + new_unique_id = new_unique_id.replace("_ofa_ph_value", "_ofa_ph_time") |
| 40 | + else: |
| 41 | + # No migration needed |
| 42 | + return None |
| 43 | + |
| 44 | + return {"new_unique_id": new_unique_id} |
| 45 | + |
| 46 | + await er.async_migrate_entries(hass, entry.entry_id, migrate_unique_id) |
| 47 | + |
| 48 | + hass.config_entries.async_update_entry(entry, version=1, minor_version=2) |
| 49 | + |
| 50 | + return True |
| 51 | + |
| 52 | + |
21 | 53 | async def async_setup_entry(hass: HomeAssistant, entry: PooldoseConfigEntry) -> bool: |
22 | 54 | """Set up Seko PoolDose from a config entry.""" |
23 | 55 | # Get host from config entry data (connection-critical configuration) |
|
0 commit comments