def async_migrate_entity( ent_reg: EntityRegistry, platform: str, old_unique_id: str, new_unique_id: str ) -> None: """Check if entity with old unique ID exists, and if so migrate it to new ID.""" if entity_id := ent_reg.async_get_entity_id(platform, DOMAIN, old_unique_id): _LOGGER.debug( "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", entity_id, old_unique_id, new_unique_id, ) try: ent_reg.async_update_entity( entity_id, new_unique_id=new_unique_id, ) except ValueError: _LOGGER.debug( ( "Entity %s can't be migrated because the unique ID is taken; " "Cleaning it up since it is likely no longer valid" ), entity_id, ) ent_reg.async_remove(entity_id)
async def code_slots_changed( ent_reg: EntityRegistry, platform: entity_platform.EntityPlatform, config_entry: ConfigEntry, old_slots: List[int], new_slots: List[int], ): """Handle code slots changed.""" slots_to_add = list(set(new_slots) - set(old_slots)) slots_to_remove = list(set(old_slots) - set(new_slots)) for slot in slots_to_remove: sensor_name = slugify( f"{config_entry.data[CONF_LOCK_NAME]}_code_slot_{slot}" ) entity_id = f"sensor.{sensor_name}" if ent_reg.async_get(entity_id): await platform.async_remove_entity(entity_id) ent_reg.async_remove(entity_id) async_add_entities( [CodesSensor(hass, entry, x) for x in slots_to_add], True, )