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

Use dict.setdefault in registry migration code (#84277)

This commit is contained in:
Erik Montnemery
2022-12-20 11:31:31 +01:00
committed by GitHub
parent 7142b4ecac
commit f88ed6b69e
2 changed files with 38 additions and 42 deletions

View File

@@ -146,40 +146,34 @@ class DeviceRegistryStore(storage.Store[dict[str, list[dict[str, Any]]]]):
"""Store entity registry data."""
async def _async_migrate_func(
self, old_major_version: int, old_minor_version: int, old_data: dict[str, Any]
self,
old_major_version: int,
old_minor_version: int,
old_data: dict[str, list[dict[str, Any]]],
) -> dict[str, Any]:
"""Migrate to the new version."""
if old_major_version < 2:
if old_minor_version < 2:
# From version 1.1
# Version 1.2 implements migration and freezes the available keys,
# populate keys which were introduced before version 1.2
for device in old_data["devices"]:
# Introduced in 0.110
device.setdefault("area_id", None)
device.setdefault("configuration_url", None)
device.setdefault("disabled_by", None)
try:
device["entry_type"] = DeviceEntryType(device.get("entry_type"))
device["entry_type"] = DeviceEntryType(device.get("entry_type")) # type: ignore[arg-type]
except ValueError:
device["entry_type"] = None
# 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", [])
device.setdefault("name_by_user", None)
# via_device_id was originally introduced as hub_device_id
device.setdefault("via_device_id", device.get("hub_device_id"))
old_data.setdefault("deleted_devices", [])
for device in old_data["deleted_devices"]:
# Introduced in 2021.2
device["orphaned_timestamp"] = device.get("orphaned_timestamp")
device.setdefault("orphaned_timestamp", None)
if old_minor_version < 3:
# Introduced in 2022.2
# Version 1.3 adds hw_version
for device in old_data["devices"]:
device["hw_version"] = device.get("hw_version")
device["hw_version"] = None
if old_major_version > 1:
raise NotImplementedError