Skip to content

Prevent duplicate poll tasks in pollable sensors#693

Open
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-pollable-sensor-task-idempotency
Open

Prevent duplicate poll tasks in pollable sensors#693
dmulcahey wants to merge 2 commits intodevfrom
pr/fix-pollable-sensor-task-idempotency

Conversation

@dmulcahey
Copy link
Contributor

@dmulcahey dmulcahey commented Feb 27, 2026

This pull request improves the lifecycle management of polling tasks for PolledElectricalMeasurement sensors in the ZHA integration. The main focus is to prevent orphaned or duplicate polling tasks by making polling enable/disable operations idempotent and ensuring completed tasks are properly cleaned up. Additionally, new tests have been added to validate these behaviors.

Polling task lifecycle improvements:

  • Updated maybe_start_polling() in zha/application/platforms/sensor/__init__.py to avoid creating duplicate polling tasks and to remove completed tasks from _tracked_tasks before starting a new one. This ensures only one active polling task exists and prevents stale task accumulation.
  • Modified disable() in zha/application/platforms/sensor/__init__.py to safely remove the polling task from _tracked_tasks before cancelling, preventing orphaned tasks and ensuring proper cleanup.

Test coverage enhancements:

  • Added test_pollable_sensor_enable_non_idempotent_disable_leaves_orphan_poll_task and test_pollable_sensor_replaces_completed_polling_task to tests/test_sensor.py to verify that enabling/disabling sensors does not leak or duplicate polling tasks, and that completed tasks are replaced and cleaned up as expected.## Summary
    Prevents duplicate poll task creation for pollable sensors on repeated enable calls and hardens disable cleanup.

Failing tests addressed

  • tests/test_sensor.py::test_pollable_sensor_enable_non_idempotent_disable_leaves_orphan_poll_task

Verification

  • pytest (branch-local targeted run): listed test passes.

@dmulcahey dmulcahey force-pushed the pr/fix-pollable-sensor-task-idempotency branch from 16b7900 to db67e88 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 (1e10bd5).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #693   +/-   ##
=======================================
  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