From 70077511a31c65694b74e0f42e16e5fdb13342ec Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Wed, 24 Sep 2025 11:28:55 -0400 Subject: [PATCH] Unload ZHA integration before adapter migration (#152896) --- homeassistant/components/zha/config_flow.py | 4 ++++ tests/components/zha/test_config_flow.py | 22 +++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/zha/config_flow.py b/homeassistant/components/zha/config_flow.py index cb0b26d6ac0a..4aa5c95accc4 100644 --- a/homeassistant/components/zha/config_flow.py +++ b/homeassistant/components/zha/config_flow.py @@ -444,6 +444,10 @@ class BaseZhaFlow(ConfigEntryBaseFlow): assert len(config_entries) == 1 config_entry = config_entries[0] + # Unload ZHA before connecting to the old adapter + with suppress(OperationNotAllowed): + await self.hass.config_entries.async_unload(config_entry.entry_id) + # Create a radio manager to connect to the old stick to reset it temp_radio_mgr = ZhaRadioManager() temp_radio_mgr.hass = self.hass diff --git a/tests/components/zha/test_config_flow.py b/tests/components/zha/test_config_flow.py index 70419a4b503e..c5093dcd400c 100644 --- a/tests/components/zha/test_config_flow.py +++ b/tests/components/zha/test_config_flow.py @@ -5,7 +5,14 @@ from datetime import timedelta from ipaddress import ip_address import json from typing import Any -from unittest.mock import AsyncMock, MagicMock, PropertyMock, create_autospec, patch +from unittest.mock import ( + AsyncMock, + MagicMock, + PropertyMock, + call, + create_autospec, + patch, +) import uuid import pytest @@ -585,14 +592,21 @@ async def test_migration_strategy_recommended( assert result_confirm["step_id"] == "choose_migration_strategy" - with patch( - "homeassistant.components.zha.radio_manager.ZhaRadioManager.restore_backup", - ) as mock_restore_backup: + with ( + patch( + "homeassistant.components.zha.radio_manager.ZhaRadioManager.restore_backup", + ) as mock_restore_backup, + patch( + "homeassistant.config_entries.ConfigEntries.async_unload", + return_value=True, + ) as mock_async_unload, + ): result_recommended = await hass.config_entries.flow.async_configure( result_confirm["flow_id"], user_input={"next_step_id": config_flow.MIGRATION_STRATEGY_RECOMMENDED}, ) + assert mock_async_unload.mock_calls == [call(entry.entry_id)] assert result_recommended["type"] is FlowResultType.ABORT assert result_recommended["reason"] == "reconfigure_successful" mock_restore_backup.assert_called_once()