Unsubscribe ZHA device proxy events during teardown (#192859)

This commit is contained in:
Jonathan Currie
2026-11-24 07:55:29 +00:00
committed by GitHub
parent 99b5812bad
commit 89ae104da7
2 changed files with 82 additions and 4 deletions
+10 -4
View File
@@ -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:
+72
View File
@@ -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