Skip to content

Commit 8f7214f

Browse files
committed
fix: don't re-fire the same Splitwise notification event every poll
The splitwise library's getNotifications() accepts updated_since/limit params but never actually applies them (unimplemented upstream) - every poll re-fetches the same "recent notifications" window regardless of what was already seen. Without dedup, the same notification would fire its splitwise_notification_event_* bus event again every SCAN_INTERVAL for as long as it stayed in that window, not just once when it appeared. Track notification ids seen on the previous poll and only fire events for ones not seen before. Note: this is in-memory only, so a HA restart still causes one re-fire of whatever's currently in the recent window.
1 parent e465b89 commit 8f7214f

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

custom_components/splitwise/coordinator.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def __init__(
7070
self.entry = entry
7171
self.session = session
7272
self.client = client
73+
# The splitwise library's getNotifications() ignores its own
74+
# updated_since/limit params (unimplemented upstream), so every poll
75+
# re-fetches the same "recent notifications" window. Track which ids
76+
# we've already fired events for so we don't re-fire the same
77+
# notification every SCAN_INTERVAL for as long as it stays recent.
78+
self._seen_notification_ids: set[int] = set()
7379

7480
def _fetch_splitwise_data(self, token):
7581
"""Run on the executor: sync the token into the client and fetch data."""
@@ -190,7 +196,12 @@ async def _async_update_data(self) -> SplitwiseData:
190196
)
191197

192198
def _emit_notifications(self, notifications):
193-
for n in notifications:
199+
current_ids = {n.getId() for n in notifications}
200+
new_notifications = [
201+
n for n in notifications if n.getId() not in self._seen_notification_ids
202+
]
203+
204+
for n in new_notifications:
194205
self.hass.bus.fire(
195206
"splitwise_notification_event_" + str(n.getType()),
196207
{
@@ -209,3 +220,5 @@ def _emit_notifications(self, notifications):
209220
},
210221
origin="REMOTE",
211222
)
223+
224+
self._seen_notification_ids = current_ids

custom_components/splitwise/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/sriramsv/custom_component_splitwise/issues",
1010
"requirements": ["splitwise==3.0.0"],
11-
"version": "0.3.0"
11+
"version": "0.3.1"
1212
}

0 commit comments

Comments
 (0)