jellyfin-qbittorrent-monitor: fix hysterisis
All checks were successful
Build and Deploy / mreow (push) Successful in 2m41s
Build and Deploy / yarn (push) Successful in 1m0s
Build and Deploy / muffin (push) Successful in 1m14s

This commit is contained in:
2026-05-14 21:11:21 -04:00
parent b2ad5abf1b
commit 958f373be6
2 changed files with 105 additions and 1 deletions

View File

@@ -61,6 +61,7 @@ class JellyfinQBittorrentMonitor:
self.streaming_start_delay = streaming_start_delay
self.streaming_stop_delay = streaming_stop_delay
self.last_state_change = 0
self._last_seen_state = None # tracks previous poll's streaming_active for flicker detection
# Webhook receiver: allows Jellyfin to push events instead of waiting for the poll
self.webhook_port = webhook_port
@@ -355,12 +356,24 @@ class JellyfinQBittorrentMonitor:
pass
def should_change_state(self, new_streaming_state: bool) -> bool:
"""Apply hysteresis to prevent rapid state changes"""
"""Apply hysteresis to prevent rapid state changes.
The delay must be contiguous: if the session flickers (absent→present
while a stop-countdown is in progress, or vice versa), the timer resets
so brief gaps don't accumulate into a spurious state transition.
Routine repeated presence (e.g. webhook PlaybackProgress every second)
does NOT reset the timer — only a direction reversal does.
"""
now = time.time()
if new_streaming_state == self.last_streaming_state:
if self._last_seen_state != self.last_streaming_state:
self.last_state_change = now
self._last_seen_state = new_streaming_state
return False
self._last_seen_state = new_streaming_state
time_since_change = now - self.last_state_change
if new_streaming_state and not self.last_streaming_state: