You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep X-ray's unreachable moments honest around reloads, starts and stops - #2648
Follow-ups to the review of #2637, which merged with ten open threads. Seven were real, this fixes them.
A reload of Spook starts every device's minute anew. The time a device reported was kept over a reload, so the time Spook was not loaded counted as reporting. A device written down as gone that reports across a reload was dropped too early, and one going right after the reload got an exact moment it did not earn. Stopping the tracker now clears it.
A registry change while Home Assistant starts waits for settling. Disabling and enabling a device during the start wiped its stored moment. Settling decides, like for everything else then.
Nothing is noted or forgotten while Home Assistant stops. Settling, or the timer for a device that is back, could still change what the final write keeps. The check moved out of the tracker into the memory: async_note and async_forget refuse while Home Assistant stops. Every path that changes what is written down ends in one of those two, so a new path cannot forget it.
The benchmark times what it claims. The crossing it timed hit a device that still reported. It now times the last entity of a device going, and coming back.
Three threads are declined, with the reason on each: callbacks run right away in core (no queued flap), a store file without a version raises for every store in Home Assistant, and a device that is not back for a minute keeps its old moment on purpose.
Motivation and Context
The moment X-ray shows has to be honest. These were the edges where it was not.
How has this been tested?
Each fix has a test that fails without it, checked by taking the fix out again:
test_a_reload_of_spook_starts_the_minute_anew: dropped a minute after the reload, not at it and not never.
test_nothing_is_noted_or_forgotten_while_home_assistant_stops: a device going, one coming back, one removed, one disabled, and the recovery timer, all during a stop.
Follow-ups to the review of #2637:
- A reload of Spook starts every device's minute anew.
- A registry change while Home Assistant starts waits for settling.
- Nothing is noted or forgotten while Home Assistant stops: one guard,
in the memory.
- The benchmark times a device going and coming back.
No actionable comments were generated in the recent review.
Walkthrough
The unreachable tracker now prevents memory changes and return-timer scheduling while Home Assistant stops. It defers device-registry changes until settling completes and resets reporting timestamps when stopped. Tests cover shutdown, reload timing, device returns, malformed memory, and large-home transitions.
Changes: Unreachable Tracker
Layer / File(s)
Summary
Memory lifecycle and shutdown custom_components/spook/xray_unreachable.py, tests/test_xray_unreachable.py
_Memory retains Home Assistant and blocks note, forget, and timer scheduling while it is stopping. Stopping clears reporting timestamps. Tests cover shutdown behavior, malformed memory input, and stale-device cleanup.
Startup settling and reload timing custom_components/spook/xray_unreachable.py, tests/test_xray_unreachable.py
Device-registry changes are ignored until settling completes. Tests cover startup and reload timing, including preservation of stored unreachable moments.
Return handling and performance checks tests/test_xray_unreachable.py
Tests check batched and staggered device returns, lookup counts, and separate timing for state changes and settling in a large home.
frenck/spook#2637: Adds the persistent unreachable tracker and state model that this PR updates.
Merge Risk: Minimal · up to fdf84
This change makes recorded unreachable moments more accurate around Spook reloads and Home Assistant starts and stops. The supplied evidence shows targeted tests for these flows and no remaining actionable merge risk.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main theme of the change: X-ray's unreachable moments are kept accurate across Spook reloads, Home Assistant starts, and stops. The changeset covers each of these: stop-time guards…
Description check
The description relates directly to the changeset. It lists the seven review fixes, including reload handling, registry changes during startup, the stop-time guard in async_note and async_forget, and …
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main theme of the change: X-ray's unreachable moments are kept accurate across Spook reloads, Home Assistant starts, and stops. The changeset covers each of these: stop-time guards in _Memory, clearing reporting state on Spook stop, registry changes during startup settling, and the benchmark. The title is specific, though long at 72 characters.
Full details: Description check
Explanation
The description relates directly to the changeset. It lists the seven review fixes, including reload handling, registry changes during startup, the stop-time guard in async_note and async_forget, and the benchmark change. It names the tests that cover each fix and states the declined review threads.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A rabbit checks the clock at night, Then lets shutdown pass without a write. Devices settle, returns appear, The tracker waits, then marks them clear. One quiet hop, and tests take flight.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: d06bbc5a-e0e4-457e-aa2d-35bc583d97d3
Commits
Reviewing files that changed from the base of the PR and between 6de19c1 and fdf8441.
Files selected for processing: 1
tests/test_xray_unreachable.py
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Reloading before startup settles can erase reporting history, causing a device that recovers and then becomes unavailable to retain an outdated outage timestamp.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Seed reporting history on startup to catch pre-settle outages
custom_components/spook/xray_unreachable.py:398
If Spook reloads before startup settling while a recorded device is already reporting, clearing this history leaves its recovery unobserved until settling unless another relevant event arrives. A device that reports for several minutes after reload and goes unavailable within a minute of settling therefore keeps its old outage timestamp. Seed reporting history from current device states in async_start, without changing stored moments before settling, and add a regression test for this reload case.
On the review note about a reload before settling: real, and a side effect of clearing who reports when Spook stops. Fixed in 6de19c1: starting the tracker now notes every device that reports at that moment, so a reload while Home Assistant starts sees them from the reload on. test_a_reload_of_spook_while_starting_sees_who_reports covers it, and failed before the fix.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The early return preserves stale reporting history across startup disable/re-enable events, which can clear a stored outage or mark a later departure exact before a fresh minute of reporting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bugfixInconsistencies or issues which will cause a problem for users or implementors.
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Follow-ups to the review of #2637, which merged with ten open threads. Seven were real, this fixes them.
async_noteandasync_forgetrefuse while Home Assistant stops. Every path that changes what is written down ends in one of those two, so a new path cannot forget it.Three threads are declined, with the reason on each: callbacks run right away in core (no queued flap), a store file without a version raises for every store in Home Assistant, and a device that is not back for a minute keeps its old moment on purpose.
Motivation and Context
The moment X-ray shows has to be honest. These were the edges where it was not.
How has this been tested?
Each fix has a test that fails without it, checked by taking the fix out again:
test_a_reload_of_spook_starts_the_minute_anew: dropped a minute after the reload, not at it and not never.test_a_registry_change_while_starting_keeps_the_momenttest_nothing_is_noted_or_forgotten_while_home_assistant_stops: a device going, one coming back, one removed, one disabled, and the recovery timer, all during a stop.test_settling_while_home_assistant_stops_notes_nothingtest_devices_back_at_once_wait_for_one_timer: 20 devices back together look for the next one back twice; one by one, at most once more each.The benchmark, locally: a value 0.12 µs, a device going 7.4 µs, coming back 5.9 µs, settling 2.9 ms. Targeted tests and ruff locally, the rest in CI.
Screenshots (if appropriate):
Types of changes
Checklist
documentation/translations/output/.