1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Bump device registry version to 1.2 (#60199)

This commit is contained in:
Erik Montnemery
2021-11-23 22:22:15 +01:00
committed by GitHub
parent 73d4445f80
commit 24779dea3b
2 changed files with 138 additions and 29 deletions

View File

@@ -11,6 +11,7 @@ import attr
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import RequiredParameterMissing
from homeassistant.helpers import storage
from homeassistant.helpers.frame import report
from homeassistant.loader import bind_hass
from homeassistant.util.enum import StrEnum
@@ -31,7 +32,8 @@ _LOGGER = logging.getLogger(__name__)
DATA_REGISTRY = "device_registry"
EVENT_DEVICE_REGISTRY_UPDATED = "device_registry_updated"
STORAGE_KEY = "core.device_registry"
STORAGE_VERSION = 1
STORAGE_VERSION_MAJOR = 1
STORAGE_VERSION_MINOR = 2
SAVE_DELAY = 10
CLEANUP_DELAY = 10
@@ -159,6 +161,41 @@ def _async_get_device_id_from_index(
return None
class DeviceRegistryStore(storage.Store):
"""Store entity registry data."""
async def _async_migrate_func(
self, old_major_version: int, old_minor_version: int, old_data: dict[str, Any]
) -> dict[str, Any]:
"""Migrate to the new version."""
if old_major_version < 2 and old_minor_version < 2:
# From version 1.1
for device in old_data["devices"]:
# Introduced in 0.110
device["entry_type"] = device.get("entry_type")
# Introduced in 0.79
# renamed in 0.95
device["via_device_id"] = device.get("via_device_id") or device.get(
"hub_device_id"
)
# Introduced in 0.87
device["area_id"] = device.get("area_id")
device["name_by_user"] = device.get("name_by_user")
# Introduced in 0.119
device["disabled_by"] = device.get("disabled_by")
# Introduced in 2021.11
device["configuration_url"] = device.get("configuration_url")
# Introduced in 0.111
old_data["deleted_devices"] = old_data.get("deleted_devices", [])
for device in old_data["deleted_devices"]:
# Introduced in 2021.2
device["orphaned_timestamp"] = device.get("orphaned_timestamp")
if old_major_version > 1:
raise NotImplementedError
return old_data
class DeviceRegistry:
"""Class to hold a registry of devices."""
@@ -170,8 +207,12 @@ class DeviceRegistry:
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the device registry."""
self.hass = hass
self._store = hass.helpers.storage.Store(
STORAGE_VERSION, STORAGE_KEY, atomic_writes=True
self._store = DeviceRegistryStore(
hass,
STORAGE_VERSION_MAJOR,
STORAGE_KEY,
atomic_writes=True,
minor_version=STORAGE_VERSION_MINOR,
)
self._clear_index()
@@ -519,44 +560,36 @@ class DeviceRegistry:
deleted_devices = OrderedDict()
if data is not None:
data = cast("dict[str, Any]", data)
for device in data["devices"]:
devices[device["id"]] = DeviceEntry(
area_id=device["area_id"],
config_entries=set(device["config_entries"]),
configuration_url=device["configuration_url"],
# type ignores (if tuple arg was cast): likely https://github.com/python/mypy/issues/8625
connections={tuple(conn) for conn in device["connections"]}, # type: ignore[misc]
disabled_by=device["disabled_by"],
entry_type=DeviceEntryType(device["entry_type"])
if device["entry_type"]
else None,
id=device["id"],
identifiers={tuple(iden) for iden in device["identifiers"]}, # type: ignore[misc]
manufacturer=device["manufacturer"],
model=device["model"],
name_by_user=device["name_by_user"],
name=device["name"],
sw_version=device["sw_version"],
# Introduced in 0.110
entry_type=DeviceEntryType(device["entry_type"])
if device.get("entry_type")
else None,
id=device["id"],
# Introduced in 0.79
# renamed in 0.95
via_device_id=(
device.get("via_device_id") or device.get("hub_device_id")
),
# Introduced in 0.87
area_id=device.get("area_id"),
name_by_user=device.get("name_by_user"),
# Introduced in 0.119
disabled_by=device.get("disabled_by"),
# Introduced in 2021.11
configuration_url=device.get("configuration_url"),
via_device_id=device["via_device_id"],
)
# Introduced in 0.111
for device in data.get("deleted_devices", []):
for device in data["deleted_devices"]:
deleted_devices[device["id"]] = DeletedDeviceEntry(
config_entries=set(device["config_entries"]),
# type ignores (if tuple arg was cast): likely https://github.com/python/mypy/issues/8625
connections={tuple(conn) for conn in device["connections"]}, # type: ignore[misc]
identifiers={tuple(iden) for iden in device["identifiers"]}, # type: ignore[misc]
id=device["id"],
# Introduced in 2021.2
orphaned_timestamp=device.get("orphaned_timestamp"),
orphaned_timestamp=device["orphaned_timestamp"],
)
self.devices = devices