Skip to content

Check the focus status when pushing sensors - #5712

Open
g3rv4 wants to merge 7 commits into
home-assistant:mainfrom
g3rv4:focus-live-ended
Open

Check the focus status when pushing sensors#5712
g3rv4 wants to merge 7 commits into
home-assistant:mainfrom
g3rv4:focus-live-ended

Conversation

@g3rv4

@g3rv4 g3rv4 commented Sep 9, 2026

Copy link
Copy Markdown

AI Policy

Select exactly one option that describes AI usage in this contribution:

  • I have not used AI for this contribution.
  • AI assistance was used for this contribution.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

Summary

Since #5471 the focus binary sensor reports the last Focus status iOS pushed to the app and only asks INFocusStatusCenter when 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 stays on through every later sensor update, including app launches — on two phones here it sat on for 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 FocusReport now 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 pushed false still 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 INFocusStatusCenter read. The Focus Filter branch deliberately keeps its current behaviour (#5592): a named filter run that nothing ended stays on even if the live query says otherwise, because that branch also covers Focuses whose status is not shared.

Copilot AI lite review requested due to automatic review settings September 9, 2026 19:35

@home-assistant home-assistant Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @g3rv4

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
home-assistant Bot marked this pull request as draft September 9, 2026 19:35
@home-assistant

home-assistant Bot commented Sep 9, 2026

Copy link
Copy Markdown

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread Sources/Shared/Environment/FocusReport.swift Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues were identified in the reviewed changes.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@g3rv4

g3rv4 commented Sep 9, 2026

Copy link
Copy Markdown
Author

I suspect what failed was a flaky test, and merged main into the branch hoping efb087b fixes it

@codecov

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.80%. Comparing base (0ddf102) to head (adea52a).

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     
Files with missing lines Coverage Δ
Sources/Shared/Environment/FocusReport.swift 100.00% <100.00%> (ø)

... and 8 files with indirect coverage changes

☔ View full report in Codecov by Harness.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bgoncal bgoncal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread Sources/Shared/Environment/FocusReport.swift Outdated
Comment thread Sources/Shared/Environment/FocusReport.swift Outdated
Comment thread Sources/Shared/Environment/FocusReport.swift Outdated
Comment on lines +147 to +152
/// 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about documenting fixes in test headers, but let's keep it for now, something for me to reflect on

@home-assistant
home-assistant Bot marked this pull request as draft September 10, 2026 12:18
g3rv4 and others added 3 commits September 10, 2026 09:31
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>
@g3rv4

g3rv4 commented Sep 10, 2026

Copy link
Copy Markdown
Author

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

I haven't tested it. I don't have an Apple dev account, and my personal account can't build it because the app uses capabilities that aren't available for personal accounts:

image

I trust your experience with the iOS focus reporting limitations, I'm happy to close this PR and rely on an shortcut automation to keep tabs of my focus state if you'd prefer to go that route.

@g3rv4
g3rv4 marked this pull request as ready for review September 10, 2026 12:56
@home-assistant
home-assistant Bot requested a review from bgoncal September 10, 2026 12:56
@bgoncal

bgoncal commented Sep 10, 2026

Copy link
Copy Markdown
Member

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.

@g3rv4

g3rv4 commented Sep 10, 2026

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Focus binary sensor stays on for hours after the Focus ends since 2026.9.0

3 participants