Skip to content

fix(sensor): restore delta semantics for latest_events + fire HA event on new earthquakes (v1.2.6/v1.2.7)#39

Merged
Geek-MD merged 4 commits into
mainfrom
copilot/fix-quake-events-empty
Mar 16, 2026
Merged

fix(sensor): restore delta semantics for latest_events + fire HA event on new earthquakes (v1.2.6/v1.2.7)#39
Geek-MD merged 4 commits into
mainfrom
copilot/fix-quake-events-empty

Conversation

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

v1.2.5 introduced a conceptual error: latest_events became a cumulative list that grew on every poll, always exposing all historical events. The correct behaviour is that latest_events is a delta — only events new since the last cycle — so automations trigger exclusively on genuinely new earthquakes.

Behavioral contract (v1.2.6+)

Scenario latest_events Sensor state changes Automation fires
First run / HA restart All feed events matching filters
Re-poll, no new events []
New earthquake detected New event(s) only

Changes

sensor.py — delta tracking (_seen_ids)

  • Replaced the cumulative _latest_events.extend() accumulator with an internal _seen_ids: set[str]. Each cycle, only events absent from _seen_ids are emitted as latest_events; _seen_ids is then updated.
  • native_value (TIMESTAMP state) is updated only when new events arrive, so trigger: state fires only on genuine new earthquakes.
  • Removed the now-unreachable MAX_EVENTS = 50 constant.
# Before (v1.2.5) — cumulative, never empty after first run
existing_ids = {e["id"] for e in self._latest_events}
self._latest_events.extend([e for e in new_events if e["id"] not in existing_ids])
self._latest_events = sorted(self._latest_events, ...)[:MAX_EVENTS]

# After (v1.2.6) — delta only, backed by a seen-IDs set
filtered_events = [e for e in new_events if e["id"] not in self._seen_ids]
self._seen_ids.update(e["id"] for e in filtered_events)
self._latest_events = sorted(filtered_events, key=parse_event_time, reverse=True)

sensor.py + const.py — HA event bus notification (v1.2.7)

  • Added EVENT_NEW_QUAKES = "usgs_earthquakes_feed_new_events" to const.py.
  • Sensor fires this event on the HA bus whenever latest_events is non-empty, enabling event-based automations without relying on sensor state polling:
trigger:
  - platform: event
    event_type: usgs_earthquakes_feed_new_events
# event.data: { entry_id, count, events[] }

README

  • Replaced stale events / "last 50 events" references with accurate latest_events delta semantics.
  • Added dedicated 📣 Event section documenting the new event, its payload, and an example automation.
  • Updated 📓 Notes to describe first-run vs. re-poll vs. new-event behaviour.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

… attribute (v1.2.5)

Co-authored-by: Geek-MD <25725990+Geek-MD@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix empty quake_events variable in automation fix(sensor): replace dual-list pattern with single latest_events accumulator (v1.2.5) Mar 16, 2026
Copilot AI requested a review from Geek-MD March 16, 2026 16:51
…ia _seen_ids (v1.2.6)

Co-authored-by: Geek-MD <25725990+Geek-MD@users.noreply.github.com>
Copilot AI changed the title fix(sensor): replace dual-list pattern with single latest_events accumulator (v1.2.5) fix(sensor): restore delta semantics for latest_events (v1.2.6) Mar 16, 2026
Co-authored-by: Geek-MD <25725990+Geek-MD@users.noreply.github.com>
Copilot AI changed the title fix(sensor): restore delta semantics for latest_events (v1.2.6) fix(sensor): restore delta semantics for latest_events + fire HA event on new earthquakes (v1.2.6/v1.2.7) Mar 16, 2026
@Geek-MD Geek-MD marked this pull request as ready for review March 16, 2026 23:32
@Geek-MD Geek-MD merged commit c740dec into main Mar 16, 2026
3 checks passed
@Geek-MD Geek-MD deleted the copilot/fix-quake-events-empty branch March 16, 2026 23:32
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.

2 participants