Disable device availability checker during diagnostics regeneration#682
Disable device availability checker during diagnostics regeneration#682TheJulianJES wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #682 +/- ##
=======================================
Coverage 97.51% 97.51%
=======================================
Files 62 62
Lines 10949 10949
=======================================
Hits 10677 10677
Misses 272 272 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| # asynchronously mutate device availability while fixtures are being written. | ||
| zha_gateway.global_updater.stop() | ||
| zha_gateway._device_availability_checker.stop() # noqa: SLF001 | ||
| zha_gateway.config.allow_polling = False |
There was a problem hiding this comment.
zha_gateway.config.allow_polling will be set to True by fetch_updated_state:
zha/zha/application/gateway.py
Line 389 in 9d03d63
With #654, we make sure to run that before regenerating diagnostics but that also means it'll re-enable allow_polling still. So that's why we also stop the global_updater and _device_availability_checker to fix the issue.
Maybe we should just patch out fetch_updated_state entirely for diagnostics. EDIT: Eh, also not possible in a nice way.
There was a problem hiding this comment.
If we'd set allow_polling = False after this line, it should work without needing to stop the global_updater and _device_availability_checker:
zha/tools/import_diagnostics.py
Line 327 in 9d03d63
Hmm...
zigpy-review-bot
left a comment
There was a problem hiding this comment.
Tooling-only change, scoped to tools/regenerate_diagnostics.py — no runtime leakage. The motivation (background availability checker firing 30-45 s into a regen run and flipping every device's available to false in the regenerated fixtures) is real, and stopping global_updater + _device_availability_checker does fix it.
One thing worth tightening before this comes out of draft — the allow_polling = False assignment is effectively dead. TestGateway.__aenter__ calls async_initialize_devices_and_entities(), which schedules fetch_updated_state as a background task (async_create_background_task, not awaited by async_block_till_done since wait_background_tasks=False is the default). That background task ends with self.config.allow_polling = True (gateway.py:389). So as soon as the event loop yields inside the first join_zigpy_device / async_block_till_done call, allow_polling will flip back to True regardless of what the new code sets here — which is exactly what you flagged in your own self-review threads on line 45.
In practice the two .stop() calls are doing all the work; the periodic loops never see the allow_polling = True flip-back because their tasks have been cancelled. So the line is harmless, just misleading. Two options:
- Drop the
allow_polling = Falseline and let the.stop()calls stand on their own (cleanest given the comment already says "disable background tasks"). - Or, await the background task to settle first (
await zha_gateway.async_block_till_done(wait_background_tasks=True)before the three lines), then setallow_polling = False, and you can drop the.stop()calls entirely — which is the alternative your second self-review thread sketched.
No tests needed — one-off tooling script. The # noqa: SLF001 on the private-attribute access is fine for tooling; if a follow-up wants a Gateway.stop_periodic_tasks() helper that mirrors what shutdown() already does (gateway.py:862-863), that'd let this script and the shutdown path share a code path, but it's not a blocker.
Proposed change
This disable device availability checker during diagnostics regeneration.
At the moment, there's a rare issue where you can manage to regenerate diagnostics with
availablebeing set tofalseeverywhere, as the checker starts after 30 to 45 seconds of running diagnostics regeneration.It's possible that the implementation can be cleaned up. Just wanted to put it into a PR for now.