From 94ecb792ec2fea66e2f9a65f0767693d1172eb8b Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 6 Feb 2021 13:17:52 +0100 Subject: [PATCH] Use async_update_entry rather than updating config_entry.data directly in Axis (#46078) Don't step version in migrate_entry to support rollbacking --- homeassistant/components/axis/__init__.py | 15 ++++++++++----- tests/components/axis/test_init.py | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/axis/__init__.py b/homeassistant/components/axis/__init__.py index c467359c17e..8722c41c3e0 100644 --- a/homeassistant/components/axis/__init__.py +++ b/homeassistant/components/axis/__init__.py @@ -48,8 +48,11 @@ async def async_migrate_entry(hass, config_entry): # Flatten configuration but keep old data if user rollbacks HASS prior to 0.106 if config_entry.version == 1: - config_entry.data = {**config_entry.data, **config_entry.data[CONF_DEVICE]} - config_entry.unique_id = config_entry.data[CONF_MAC] + unique_id = config_entry.data[CONF_MAC] + data = {**config_entry.data, **config_entry.data[CONF_DEVICE]} + hass.config_entries.async_update_entry( + config_entry, unique_id=unique_id, data=data + ) config_entry.version = 2 # Normalise MAC address of device which also affects entity unique IDs @@ -66,10 +69,12 @@ async def async_migrate_entry(hass, config_entry): ) } - await async_migrate_entries(hass, config_entry.entry_id, update_unique_id) + if old_unique_id != new_unique_id: + await async_migrate_entries(hass, config_entry.entry_id, update_unique_id) - config_entry.unique_id = new_unique_id - config_entry.version = 3 + hass.config_entries.async_update_entry( + config_entry, unique_id=new_unique_id + ) _LOGGER.info("Migration to version %s successful", config_entry.version) diff --git a/tests/components/axis/test_init.py b/tests/components/axis/test_init.py index b7faceaf10d..36a603ea7b3 100644 --- a/tests/components/axis/test_init.py +++ b/tests/components/axis/test_init.py @@ -109,7 +109,7 @@ async def test_migrate_entry(hass): CONF_MODEL: "model", CONF_NAME: "name", } - assert entry.version == 3 + assert entry.version == 2 # Keep version to support rollbacking assert entry.unique_id == "00:40:8c:12:34:56" vmd4_entity = registry.async_get("binary_sensor.vmd4")