From 6faa9abc756344a67732979ce3cf5bedeefc7f70 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 17 Aug 2023 08:51:59 +0200 Subject: [PATCH] Fix Verisure config entry migration (#98546) --- homeassistant/components/verisure/__init__.py | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/verisure/__init__.py b/homeassistant/components/verisure/__init__.py index 302bd23b66f..62f41913862 100644 --- a/homeassistant/components/verisure/__init__.py +++ b/homeassistant/components/verisure/__init__.py @@ -83,21 +83,22 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: LOGGER.debug("Migrating from version %s", entry.version) if entry.version == 1: - config_entry_default_code = entry.options.get(CONF_LOCK_DEFAULT_CODE) - entity_reg = er.async_get(hass) - entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id) - for entity in entries: - if entity.entity_id.startswith("lock"): - entity_reg.async_update_entity_options( - entity.entity_id, - LOCK_DOMAIN, - {CONF_DEFAULT_CODE: config_entry_default_code}, - ) - new_options = entry.options.copy() - del new_options[CONF_LOCK_DEFAULT_CODE] + if config_entry_default_code := entry.options.get(CONF_LOCK_DEFAULT_CODE): + entity_reg = er.async_get(hass) + entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id) + for entity in entries: + if entity.entity_id.startswith("lock"): + entity_reg.async_update_entity_options( + entity.entity_id, + LOCK_DOMAIN, + {CONF_DEFAULT_CODE: config_entry_default_code}, + ) + new_options = entry.options.copy() + del new_options[CONF_LOCK_DEFAULT_CODE] + + hass.config_entries.async_update_entry(entry, options=new_options) entry.version = 2 - hass.config_entries.async_update_entry(entry, options=new_options) LOGGER.info("Migration to version %s successful", entry.version)