Possible to migrate unique_id to a new value for users of my integration?

Hi. I’m mantaining a custom integration that already set unique ids as for best practice.
We now found out that the value we use to set the unique_ids could change in some circumstances in ways out of our control (external provider can change them). We already come out with a better way to identify devices and we are ready to implement it. Thing is, we would like to have a “migration” plan to update the entities for those who already use the integration in order to avoid spamming them with duplicate entities.

Is it possible to programmatically change the unique_id for entities already in the registry?

If I try this, of course, it doesn’t work:

entity_registry = er.async_get(server.hass)
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
for entity in entities:
    entity.unique_id = 'foo'

attr.exceptions.FrozenInstanceError

Thanks in advance

I think you’d have to find the previous entry, store it, make a new entry with a new unique id, and then update the new one with the previous settings.

Create a migration and call there er.async_update_entity with new_unique_id parameter for all your entities.