Handle previously migrated HEOS device identifier (#137596)

This commit is contained in:
Andrew Sayre 2025-02-07 09:04:34 -06:00 committed by GitHub
parent 600269ad0a
commit dd82212e45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 4 deletions

View File

@ -39,9 +39,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: HeosConfigEntry) -> bool
): ):
for domain, player_id in device.identifiers: for domain, player_id in device.identifiers:
if domain == DOMAIN and not isinstance(player_id, str): if domain == DOMAIN and not isinstance(player_id, str):
device_registry.async_update_device( # type: ignore[unreachable] # Create set of identifiers excluding this integration
device.id, new_identifiers={(DOMAIN, str(player_id))} identifiers = { # type: ignore[unreachable]
(domain, identifier)
for domain, identifier in device.identifiers
if domain != DOMAIN
}
migrated_identifiers = {(DOMAIN, str(player_id))}
# Add migrated if not already present in another device, which occurs if the user downgraded and then upgraded
if not device_registry.async_get_device(migrated_identifiers):
identifiers.update(migrated_identifiers)
if len(identifiers) > 0:
device_registry.async_update_device(
device.id, new_identifiers=identifiers
) )
else:
device_registry.async_remove_device(device.id)
break break
coordinator = HeosCoordinator(hass, entry) coordinator = HeosCoordinator(hass, entry)

View File

@ -193,13 +193,36 @@ async def test_device_id_migration(
# Create a device with a legacy identifier # Create a device with a legacy identifier
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, 1)}, # type: ignore[arg-type] identifiers={(DOMAIN, 1), ("Other", "1")}, # type: ignore[arg-type]
) )
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
identifiers={("Other", 1)}, # type: ignore[arg-type] identifiers={("Other", 1)}, # type: ignore[arg-type]
) )
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
assert device_registry.async_get_device({("Other", 1)}) is not None # type: ignore[arg-type] assert device_registry.async_get_device({("Other", 1)}) is not None # type: ignore[arg-type]
assert device_registry.async_get_device({(DOMAIN, 1)}) is None # type: ignore[arg-type] assert device_registry.async_get_device({(DOMAIN, 1)}) is None # type: ignore[arg-type]
assert device_registry.async_get_device({(DOMAIN, "1")}) is not None assert device_registry.async_get_device({(DOMAIN, "1")}) is not None
assert device_registry.async_get_device({("Other", "1")}) is not None
async def test_device_id_migration_both_present(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry: MockConfigEntry,
) -> None:
"""Test that legacy non-string devices are removed when both devices present."""
config_entry.add_to_hass(hass)
# Create a device with a legacy identifier AND a new identifier
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, 1)}, # type: ignore[arg-type]
)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "1")}
)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
assert device_registry.async_get_device({(DOMAIN, 1)}) is None # type: ignore[arg-type]
assert device_registry.async_get_device({(DOMAIN, "1")}) is not None