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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

View File

@ -172,32 +172,34 @@ class RegistryEntry:
hass.states.async_set(self.entity_id, STATE_UNAVAILABLE, attrs)
class EntityRegistryStore(storage.Store):
class EntityRegistryStore(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
self,
old_major_version: int,
old_minor_version: int,
old_data: dict[str, list[dict[str, Any]]],
) -> dict:
"""Migrate to the new version."""
data = old_data
if old_major_version == 1 and old_minor_version < 2:
# From version 1.1
# Version 1.2 implements migration and freezes the available keys
for entity in data["entities"]:
# Populate all keys
entity["area_id"] = entity.get("area_id")
entity["capabilities"] = entity.get("capabilities") or {}
entity["config_entry_id"] = entity.get("config_entry_id")
entity["device_class"] = entity.get("device_class")
entity["device_id"] = entity.get("device_id")
entity["disabled_by"] = entity.get("disabled_by")
entity["entity_category"] = entity.get("entity_category")
entity["icon"] = entity.get("icon")
entity["name"] = entity.get("name")
entity["original_icon"] = entity.get("original_icon")
entity["original_name"] = entity.get("original_name")
entity["platform"] = entity["platform"]
entity["supported_features"] = entity.get("supported_features", 0)
entity["unit_of_measurement"] = entity.get("unit_of_measurement")
# Populate keys which were introduced before version 1.2
entity.setdefault("area_id", None)
entity.setdefault("capabilities", {})
entity.setdefault("config_entry_id", None)
entity.setdefault("device_class", None)
entity.setdefault("device_id", None)
entity.setdefault("disabled_by", None)
entity.setdefault("entity_category", None)
entity.setdefault("icon", None)
entity.setdefault("name", None)
entity.setdefault("original_icon", None)
entity.setdefault("original_name", None)
entity.setdefault("supported_features", 0)
entity.setdefault("unit_of_measurement", None)
if old_major_version == 1 and old_minor_version < 3:
# Version 1.3 adds original_device_class
@ -238,7 +240,7 @@ class EntityRegistryStore(storage.Store):
if old_major_version == 1 and old_minor_version < 9:
# Version 1.9 adds translation_key
for entity in data["entities"]:
entity["translation_key"] = entity.get("translation_key")
entity["translation_key"] = None
if old_major_version > 1:
raise NotImplementedError