Check the focus status when pushing sensors - #5712
Conversation
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
There was a problem hiding this comment.
🟡 Changes recommended
Regression coverage is needed for the new stale-status reconciliation behavior.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Updates Focus reporting to reconcile cached statuses with iOS’s live Focus state, clearing stale active states after the switch grace period.
Changes:
- Queries live Focus status on each report.
- Preserves Focus Filter and app-extension behavior.
- Adds diagnostic logging for live status.
File summaries
| File | Summary | Review |
|---|---|---|
Sources/Shared/Environment/FocusReport.swift |
Implements stale Focus status reconciliation and live-state logging. | Moderate: Add regression coverage for the cached-running/live-ended case and 30-second grace period. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
I suspect what failed was a flaky test, and merged |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5712 +/- ##
==========================================
- Coverage 42.85% 42.80% -0.05%
==========================================
Files 1124 1124
Lines 78170 78179 +9
==========================================
- Hits 33496 33461 -35
- Misses 44674 44718 +44
☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bgoncal
left a comment
There was a problem hiding this comment.
Thank you for looking into this, just minor comments, also, were you able to test this solution for a reasonable period of time? Focus reporting has presented itself a bit unreliable in iOS reporting system
| /// The bug behind #5711: iOS wakes us when a Focus starts but not reliably when one ends, so | ||
| /// a pushed "running" whose "ended" push never came stood until the next Focus started. iOS | ||
| /// only pushes "running" for a Focus whose status the user shares, so the live status is | ||
| /// truthful for it — but inside the switch window it can still describe the Focus that just | ||
| /// ended, so the push has to be older than that window before a live "not focused" ends it. | ||
| func testStaleReceivedRunningEndsOnceTheLiveStatusSaysNotFocused() throws { |
There was a problem hiding this comment.
Not sure how I feel about documenting fixes in test headers, but let's keep it for now, something for me to reflect on
Co-authored-by: Bruno Pantaleão Gonçalves <5808343+bgoncal@users.noreply.github.com>
Co-authored-by: Bruno Pantaleão Gonçalves <5808343+bgoncal@users.noreply.github.com>
|
The PR is not bad, I was jus curious if you were able to confirm the implementation, check our readme file on how to setup your xcode for your account, you can also remove some features that require entitlement and then it will build to your own device, I will test it myself of course in parallel. |
I can remove the entitlements, but from here, it seems like I need the Communication Notifications capability, which I can't sign for. Would you be able to do a testflight build? I'd be happy to run that on both devices to validate it works as expected. |

AI Policy
Select exactly one option that describes AI usage in this contribution:
Summary
Since #5471 the
focusbinary sensor reports the last Focus status iOS pushed to the app and only asksINFocusStatusCenterwhen nothing was ever pushed. iOS wakes the app when a Focus starts but not reliably when one ends, so when the "ended" push does not arrive the sensor staysonthrough every later sensor update, including app launches — on two phones here it satonfor 18 h and 26 h after a Sleep Focus ended (#5711).A pushed "running" only ever describes a Focus whose status the user shares with the app, and for such a Focus the live query answers truthfully. So
FocusReportnow asks iOS on every report and, when the stored status says a Focus is running but iOS says nothing is, treats the Focus as ended — provided the stored status is older than the existing switch grace period, so the tail of a Focus switch is still read as a switch. Everything else is unchanged: a pushedfalsestill wins for Focuses iOS cannot see, the Focus Filter path still stands on its own, and in app extensions the "live" query is the received status itself, so nothing changes there.Fixes #5711
Screenshots
No UI changes.
Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant# N/A
Any other notes
The live query is now made on every report rather than only as a fallback; it is a local
INFocusStatusCenterread. The Focus Filter branch deliberately keeps its current behaviour (#5592): a named filter run that nothing ended staysoneven if the live query says otherwise, because that branch also covers Focuses whose status is not shared.