Skip to content

Harden device entity lifecycle and command forwarding#689

Open
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-device-entity-lifecycle-forwarding
Open

Harden device entity lifecycle and command forwarding#689
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-device-entity-lifecycle-forwarding

Conversation

@dmulcahey
Copy link
Contributor

@dmulcahey dmulcahey commented Feb 27, 2026

This pull request introduces important improvements to entity and device lifecycle management in the Zigbee integration, focusing on robust cleanup, initialization, and manufacturer-specific command handling. The main goals are to prevent stale state accumulation, ensure proper error handling during entity and endpoint removal, and guarantee that manufacturer codes are correctly forwarded in cluster commands.

Entity and Device Lifecycle Improvements:

  • Added exception handling for on_remove callbacks in platform entities, preventing a single callback failure from aborting entity cleanup and ensuring all tracked tasks are cancelled and removed properly (zha/application/platforms/__init__.py). [1] [2]
  • Modified device initialization to drain and clean up stale pending entities before starting a new discovery cycle, logging any failures instead of aborting initialization (zha/zigbee/device.py).
  • Ensured that repeated async_initialize passes do not accumulate pending entities by clearing the pending list after each initialization, and keeping entity instances stable across re-initialization (zha/zigbee/device.py).
  • Prevented false-positive device status transitions by tracking endpoint initialization failures and only setting status to INITIALIZED if all endpoints initialize successfully (zha/zigbee/device.py). [1] [2]

Cluster Command Handling:

  • Updated issue_cluster_command to correctly forward manufacturer codes in both args and params invocation paths, ensuring manufacturer-specific commands are properly framed for compatibility and reliability (zha/zigbee/device.py).

Testing Enhancements:

  • Added comprehensive tests to validate correct manufacturer forwarding, proper stale entity cleanup, prevention of pending entity growth, and robust error handling in initialization and removal flows (tests/test_device.py). [1] [2]

These changes collectively strengthen the reliability and maintainability of device and entity management in the Zigbee integration, reducing lifecycle errors and improving support for manufacturer-specific device interactions.## Summary
Hardens device/entity lifecycle initialization and cluster command forwarding.

Failing tests addressed

  • tests/test_device.py::test_issue_cluster_command
  • tests/test_device.py::test_platform_entity_on_remove_callback_failure_does_not_abort_cleanup
  • tests/test_device.py::test_async_initialize_does_not_grow_pending_entities_between_passes
  • tests/test_device.py::test_async_initialize_does_not_mark_initialized_if_endpoint_init_fails
  • tests/test_sensor.py::test_device_counter_sensors
  • tests/test_sensor.py::test_device_unavailable_or_disabled_skips_entity_polling
  • tests/test_light.py::test_light_refresh

Verification

  • pytest (branch-local targeted run): all listed tests pass.

@dmulcahey dmulcahey force-pushed the pr/fix-device-entity-lifecycle-forwarding branch from 5e4503b to 25a3046 Compare February 27, 2026 16:04
@dmulcahey dmulcahey changed the base branch from dm/codex-issue-exploration to dev February 27, 2026 16:04
@dmulcahey dmulcahey closed this Feb 27, 2026
@dmulcahey dmulcahey reopened this Feb 27, 2026
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.55%. Comparing base (9d03d63) to head (1722d3e).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #689      +/-   ##
==========================================
+ Coverage   97.51%   97.55%   +0.04%     
==========================================
  Files          62       62              
  Lines       10949    10975      +26     
==========================================
+ Hits        10677    10707      +30     
+ Misses        272      268       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant