From 89ae104da7f98f8624bf775c1444ba3274db0193 Mon Sep 17 00:00:00 2001 From: Jonathan Currie Date: Tue, 24 Nov 2026 20:55:29 +1300 Subject: [PATCH] Unsubscribe ZHA device proxy events during teardown (#192859) --- homeassistant/components/zha/helpers.py | 14 +++-- tests/components/zha/test_helpers.py | 72 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/zha/helpers.py b/homeassistant/components/zha/helpers.py index adcd1537e379..09ffa0e65222 100644 --- a/homeassistant/components/zha/helpers.py +++ b/homeassistant/components/zha/helpers.py @@ -457,13 +457,16 @@ class ZHADeviceProxy(EventBase): self._unsubs.append(self.device.on_all_events(self._handle_event_protocol)) @callback - def async_rebind_device(self, device: BaseDevice) -> None: - """Repoint this proxy to a replacement device object after a re-interview.""" + def async_cleanup_handles(self) -> None: + """Unsubscribe from device events.""" for unsub in self._unsubs: unsub() - self._unsubs.clear() + @callback + def async_rebind_device(self, device: BaseDevice) -> None: + """Repoint this proxy to a replacement device object after a re-interview.""" + self.async_cleanup_handles() self.device = device self._unsubs.append(self.device.on_all_events(self._handle_event_protocol)) self.attach_event_handlers() @@ -877,7 +880,7 @@ class ZHAGatewayProxy(EventBase): entity_refs = self._ha_entity_refs.pop(event.device_info.ieee, None) if zha_device_proxy is not None: device_info = zha_device_proxy.zha_device_info - # zha_device_proxy.async_cleanup_handles() + zha_device_proxy.async_cleanup_handles() async_dispatcher_send( self.hass, f"{SIGNAL_REMOVE_ENTITIES}_{zha_device_proxy.device.ieee!s}", @@ -1038,6 +1041,9 @@ class ZHAGatewayProxy(EventBase): """Shutdown the gateway proxy.""" for unsub in self._unsubs: unsub() + self._unsubs.clear() + for device_proxy in self.device_proxies.values(): + device_proxy.async_cleanup_handles() await self.gateway.shutdown() def get_device_proxy(self, ieee: EUI64) -> ZHADeviceProxy | None: diff --git a/tests/components/zha/test_helpers.py b/tests/components/zha/test_helpers.py index 3e9110c2472c..6b78518d62f5 100644 --- a/tests/components/zha/test_helpers.py +++ b/tests/components/zha/test_helpers.py @@ -11,6 +11,7 @@ from probatio import to_field_list import pytest from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed from zha.application.const import ATTR_TYPE, ZHA_GW_MSG +from zha.zigbee.device import DeviceFirmwareInfoUpdatedEvent from zigpy.application import ControllerApplication import zigpy.device from zigpy.profiles import zha @@ -57,6 +58,77 @@ from .conftest import ( _LOGGER = logging.getLogger(__name__) +@pytest.mark.usefixtures("zigpy_app_controller_with_switches") +async def test_device_events_unsubscribed_on_unload( + hass: HomeAssistant, + setup_zha: Callable[..., Coroutine[Any, Any, None]], + config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test retained ZHA devices cannot update HA after the entry is unloaded.""" + await setup_zha() + gateway_proxy = get_zha_gateway_proxy(hass) + device_proxy = gateway_proxy.get_device_proxy( + zigpy.types.EUI64.convert("01:2d:6f:00:0a:90:69:e1") + ) + assert device_proxy is not None + device = device_proxy.device + device_id = device_proxy.device_id + event = DeviceFirmwareInfoUpdatedEvent( + old_firmware_version=None, new_firmware_version="test-before-unload" + ) + device.emit(event.event_type, event) + await hass.async_block_till_done() + assert device_registry.async_get(device_id).sw_version == "test-before-unload" + + assert await hass.config_entries.async_unload(config_entry.entry_id) + event = DeviceFirmwareInfoUpdatedEvent( + old_firmware_version="test-before-unload", + new_firmware_version="test-after-unload", + ) + device.emit(event.event_type, event) + await hass.async_block_till_done() + assert device_registry.async_get(device_id).sw_version == "test-before-unload" + + +@pytest.mark.usefixtures("zigpy_app_controller_with_switches") +async def test_device_events_unsubscribed_on_removal( + hass: HomeAssistant, + setup_zha: Callable[..., Coroutine[Any, Any, None]], + device_registry: dr.DeviceRegistry, +) -> None: + """Test retained ZHA devices cannot update HA after device removal.""" + await setup_zha() + gateway_proxy = get_zha_gateway_proxy(hass) + device_proxy = gateway_proxy.get_device_proxy( + zigpy.types.EUI64.convert("01:2d:6f:00:0a:90:69:e1") + ) + assert device_proxy is not None + device = device_proxy.device + device_id = device_proxy.device_id + event = DeviceFirmwareInfoUpdatedEvent( + old_firmware_version=None, new_firmware_version="test-before-removal" + ) + device.emit(event.event_type, event) + await hass.async_block_till_done() + assert device_registry.async_get(device_id).sw_version == "test-before-removal" + + gateway_proxy.gateway.device_removed(device.device) + await hass.async_block_till_done() + assert gateway_proxy.get_device_proxy(device.ieee) is None + assert device_registry.async_get(device_id) is None + + with patch.object(device_registry, "async_update_device") as update_device: + event = DeviceFirmwareInfoUpdatedEvent( + old_firmware_version="test-before-removal", + new_firmware_version="test-after-removal", + ) + device.emit(event.event_type, event) + await hass.async_block_till_done() + + update_device.assert_not_called() + + async def test_zcl_schema_conversions(hass: HomeAssistant) -> None: """Test ZHA ZCL schema conversion helpers.""" command_schema = lighting.Color.ServerCommandDefs.color_loop_set.schema