Skip to content

Poll only while watching for the bus (v0.5.1) - #2

Merged
aniceberg merged 1 commit into
mainfrom
poll-only-in-window
Oct 29, 2026
Merged

aniceberg merged 1 commit into
mainfrom
poll-only-in-window

Conversation

@aniceberg

Copy link
Copy Markdown
Owner

What

Bus Monitor now calls the tracking provider (Bouncie or Traccar) only while a profile is watching: inside its watch window (days and times) and only until the bus reaches the stop that day.

  • Outside that it makes no provider requests and keeps the last known positions, so the map and device tracker still show where the bus was last seen.
  • "Where is the bus?" (describe_location) fetches once on demand when polling is paused, so the spoken answer is current.
  • Startup always does one fetch, so entities have a position after a restart.
  • The next day polling resumes when the window opens.
  • With several profiles in one entry, polling runs while any of them is watching.

Why

Before this, Bus Monitor polled all day, every day. With the Bouncie default of every 30 s, that's about 2,880 requests a day per account, almost all of them outside the times anyone cares about. A typical 1–2 hour window that stops at arrival cuts that by roughly 90%.

Reviewer notes

  • No false checkpoint alerts after a pause. The first fix in the window is compared with yesterday's last fix, but _crossed already ignores segments longer than MAX_SEGMENT_GAP (120 s). So that first fix is only checked against where the bus actually is.
  • Late and offline checks are unaffected: tick() still runs on every coordinator interval. It needs no provider call, and it already did nothing outside the window or after arrival.
  • "Final checkpoint" here means the stop, since arrival is what ends a trip. A profile without a stop never arrives, so it polls for the whole window.
  • Limitation: Use the bus's current position in the options flow and the map panel's "add at bus position" use the last polled position. Outside the window that may be old.
  • The settings label now says the polling interval applies "only while watching for the bus".

Tests

ruff, pytest (97 tests) and hassfest (run locally in Docker) all pass. The new tests/test_polling.py covers:

  • wants_position across the window, weekends, arrival and the next day
  • no polling outside the window (one fetch at startup only)
  • polling stops after arrival
  • describe_location fetching on demand.

The three HA-level tests fail without the change.

🤖 Generated with Claude Code

The provider is polled only while a profile is in its watch window and
the bus hasn't reached the stop yet today. Outside that, Bus Monitor
keeps the last known positions and makes no requests; "where is the bus?"
fetches once on demand so the answer is still current.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T14:41:40.490207Z 85427eb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@aniceberg
aniceberg merged commit 0fe2117 into main Oct 29, 2026
6 checks passed

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 85427eb779

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +180 to +184
if self.data is not None and not self.needs_positions(now):
# Nothing to watch: keep the last positions and spare the provider a request.
data = self.data
else:
data = await self._fetch_positions()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Discard stale heading history when polling resumes

When polling resumes the next morning, TripTracker.prev still contains yesterday's arrival position. Although _crossed() rejects segments older than MAX_SEGMENT_GAP, _heading() does not: it derives a heading from yesterday's stop to today's position, overriding the current GPS course. For a bus approaching a checkpoint south of the stop, this produces a southbound heading and suppresses the checkpoint alert even though the bus is traveling north toward the stop. I reproduced the missing alert with paused polling; feeding an intervening position, as continuous polling previously did, produces the alert. Clear or age-limit the previous position used for heading inference when resuming after a pause.

Useful? React with 👍 / 👎.

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