From 9ba60569350741695f3694e3fab2529feadc4ad4 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Tue, 15 Jul 2025 19:49:44 +0000 Subject: [PATCH] Improve tests for migration --- tests/components/mqtt/test_repairs.py | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/components/mqtt/test_repairs.py b/tests/components/mqtt/test_repairs.py index 6f01d82cf64..1b9bb0d1f16 100644 --- a/tests/components/mqtt/test_repairs.py +++ b/tests/components/mqtt/test_repairs.py @@ -90,6 +90,7 @@ async def test_subentry_reconfigure_export_settings( # assert we have a device for the subentry device = device_registry.async_get_device(identifiers={(mqtt.DOMAIN, subentry_id)}) + assert device.config_entries_subentries[config_entry.entry_id] == {subentry_id} assert device is not None # assert we entity for all subentry components @@ -124,15 +125,20 @@ async def test_subentry_reconfigure_export_settings( "url": "https://www.home-assistant.io/integrations/mqtt/" } - # Copy the suggested values for an export + # Copy the exported config suggested values for an export suggested_values_from_schema = { field: field.description["suggested_value"] for field in result["data_schema"].schema } - # Try to set up the exported config + # Try to set up the exported config with a changed device name events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED) await setup_helper(hass, suggested_values_from_schema) + # Assert the subentry device was not effected by the exported configs + device = device_registry.async_get_device(identifiers={(mqtt.DOMAIN, subentry_id)}) + assert device.config_entries_subentries[config_entry.entry_id] == {subentry_id} + assert device is not None + # Assert a repair flow was created assert len(events) == 1 issue_id = events[0].data["issue_id"] @@ -151,3 +157,21 @@ async def test_subentry_reconfigure_export_settings( data = await process_repair_fix_flow(client, flow_id) assert data["type"] == "create_entry" + + # Assert the subentry is removed and no other entity has linked the device + device = device_registry.async_get_device(identifiers={(mqtt.DOMAIN, subentry_id)}) + assert device is None + + await hass.async_block_till_done(wait_background_tasks=True) + assert len(config_entry.subentries) == 0 + + # Try to set up the exported config again + events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED) + await setup_helper(hass, suggested_values_from_schema) + assert len(events) == 0 + + # The MQTT device was now set up from the new source + await hass.async_block_till_done(wait_background_tasks=True) + device = device_registry.async_get_device(identifiers={(mqtt.DOMAIN, subentry_id)}) + assert device.config_entries_subentries[config_entry.entry_id] == {None} + assert device is not None