Skip to content

Make light polling and transition listener cleanup idempotent#692

Open
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-light-polling-transition-idempotency
Open

Make light polling and transition listener cleanup idempotent#692
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-light-polling-transition-idempotency

Conversation

@dmulcahey
Copy link
Contributor

@dmulcahey dmulcahey commented Feb 27, 2026

This pull request introduces several improvements and bug fixes to the polling and transition lifecycle management for ZHA light entities. The changes focus on ensuring that task and handle tracking is robust, preventing resource leaks and stale bookkeeping, especially during repeated enable/disable and transition operations. The update also adds comprehensive tests to validate these behaviors.

Lifecycle and resource management improvements:

  • Modified start_polling in zha/application/platforms/light/__init__.py to ensure that starting polling is idempotent: it does not create duplicate polling tasks, and it properly cleans up completed tasks from _tracked_tasks before starting a new one.
  • Updated disable in zha/application/platforms/light/__init__.py to always remove the refresh task from _tracked_tasks before cancelling it, suppressing errors if the task is not present.
  • Improved _async_unsub_transition_listener in zha/application/platforms/light/__init__.py to correctly remove the transition listener handle from _tracked_handles by referencing the handle before setting it to None.

Testing and validation:

  • Added new async tests in tests/test_light.py to verify that enabling a light multiple times does not orphan polling tasks, that completed polling tasks are properly replaced, and that transition listener handles are correctly unsubscribed and removed from tracking.## Summary
    Makes light polling start/stop idempotent and fixes transition listener handle cleanup.

Failing tests addressed

  • tests/test_light.py::test_light_refresh
  • tests/test_light.py::test_light_enable_second_pass_is_idempotent_for_polling_tasks
  • tests/test_light.py::test_async_unsub_transition_listener_second_pass_removes_old_handle

Verification

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

@dmulcahey dmulcahey force-pushed the pr/fix-light-polling-transition-idempotency branch from a6f223e to cb6dd2f 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.51%. Comparing base (9d03d63) to head (cbdd2f8).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #692   +/-   ##
=======================================
  Coverage   97.51%   97.51%           
=======================================
  Files          62       62           
  Lines       10949    10956    +7     
=======================================
+ Hits        10677    10684    +7     
  Misses        272      272           

☔ 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