|
48 | 48 | DATA_TRACEBACK, |
49 | 49 | DATA_TAG_INTEGRATION, |
50 | 50 | DATA_PANEL_REGISTERED, |
| 51 | + DATA_DEVICE_REGISTRY, |
51 | 52 | ) |
52 | 53 | from .adaptive_store import AdaptiveStore |
| 54 | +from .device_registry import DeviceRegistry |
53 | 55 | from .coordinator import PadSpanCoordinator |
54 | 56 | from .maps_store import MapsStore |
55 | 57 | from .model_store import ModelStore |
@@ -148,6 +150,20 @@ async def _init_traceback(): |
148 | 150 | await tb_store.async_load() |
149 | 151 | return (DATA_TRACEBACK, tb_store, f"TracebackStore ready ({len(tb_store.frames)} frames)") |
150 | 152 |
|
| 153 | + async def _init_device_registry(): |
| 154 | + dev_reg = DeviceRegistry(hass) |
| 155 | + await dev_reg.async_load() |
| 156 | + # One-time migration from ObjectStore if device registry is empty |
| 157 | + if dev_reg.device_count() == 0: |
| 158 | + obj_store = hass.data.get(DOMAIN, {}).get(DATA_OBJECTS) |
| 159 | + if obj_store and obj_store.all(): |
| 160 | + stats = await dev_reg.async_migrate_from_object_store(obj_store) |
| 161 | + _LOGGER.info( |
| 162 | + "DeviceRegistry migration: %d devices, %d merged, %d skipped", |
| 163 | + stats["migrated"], stats["merged"], stats["skipped"], |
| 164 | + ) |
| 165 | + return (DATA_DEVICE_REGISTRY, dev_reg, f"DeviceRegistry ready ({dev_reg.device_count()} devices)") |
| 166 | + |
151 | 167 | async def _init_tag(): |
152 | 168 | from .tag_integration import TagIntegration |
153 | 169 | tag_int = TagIntegration(hass) |
@@ -188,6 +204,8 @@ async def _init_tag(): |
188 | 204 | deferred.append(_init_traceback()) |
189 | 205 | if DATA_TAG_INTEGRATION not in hass.data[DOMAIN]: |
190 | 206 | deferred.append(_init_tag()) |
| 207 | + if DATA_DEVICE_REGISTRY not in hass.data[DOMAIN]: |
| 208 | + deferred.append(_init_device_registry()) |
191 | 209 |
|
192 | 210 | if deferred: |
193 | 211 | results = await asyncio.gather(*deferred) |
@@ -478,6 +496,14 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
478 | 496 | except Exception as err: |
479 | 497 | _LOGGER.debug("Object history flush error: %s", err) |
480 | 498 |
|
| 499 | + # Flush device registry to disk before shutdown |
| 500 | + try: |
| 501 | + _dev_reg = hass.data.get(DOMAIN, {}).get(DATA_DEVICE_REGISTRY) |
| 502 | + if _dev_reg: |
| 503 | + await _dev_reg.async_flush_dirty() |
| 504 | + except Exception as err: |
| 505 | + _LOGGER.debug("DeviceRegistry flush error: %s", err) |
| 506 | + |
481 | 507 | # Flush traceback store to disk before shutdown |
482 | 508 | try: |
483 | 509 | _dom = hass.data.get(DOMAIN, {}) |
|
0 commit comments