async def test_cleanup_device_registry_removes_expired_orphaned_devices( hass, registry): """Test cleanup removes expired orphaned devices.""" config_entry = MockConfigEntry(domain="hue") config_entry.add_to_hass(hass) registry.async_get_or_create(identifiers={("hue", "d1")}, config_entry_id=config_entry.entry_id) registry.async_get_or_create(identifiers={("hue", "d2")}, config_entry_id=config_entry.entry_id) registry.async_get_or_create(identifiers={("hue", "d3")}, config_entry_id=config_entry.entry_id) registry.async_clear_config_entry(config_entry.entry_id) assert len(registry.devices) == 0 assert len(registry.deleted_devices) == 3 ent_reg = entity_registry.async_get(hass) device_registry.async_cleanup(hass, registry, ent_reg) assert len(registry.devices) == 0 assert len(registry.deleted_devices) == 3 future_time = time.time( ) + device_registry.ORPHANED_DEVICE_KEEP_SECONDS + 1 with patch("time.time", return_value=future_time): device_registry.async_cleanup(hass, registry, ent_reg) assert len(registry.devices) == 0 assert len(registry.deleted_devices) == 0
async def test_cleanup_device_registry(hass, registry): """Test cleanup works.""" config_entry = MockConfigEntry(domain="hue") config_entry.add_to_hass(hass) d1 = registry.async_get_or_create(identifiers={("hue", "d1")}, config_entry_id=config_entry.entry_id) registry.async_get_or_create(identifiers={("hue", "d2")}, config_entry_id=config_entry.entry_id) d3 = registry.async_get_or_create(identifiers={("hue", "d3")}, config_entry_id=config_entry.entry_id) registry.async_get_or_create(identifiers={("something", "d4")}, config_entry_id="non_existing") ent_reg = entity_registry.async_get(hass) ent_reg.async_get_or_create("light", "hue", "e1", device_id=d1.id) ent_reg.async_get_or_create("light", "hue", "e2", device_id=d1.id) ent_reg.async_get_or_create("light", "hue", "e3", device_id=d3.id) device_registry.async_cleanup(hass, registry, ent_reg) assert registry.async_get_device({("hue", "d1")}) is not None assert registry.async_get_device({("hue", "d2")}) is not None assert registry.async_get_device({("hue", "d3")}) is not None assert registry.async_get_device({("something", "d4")}) is None