Fix on_arrival() to always cancel departure timer on device re-arrival

- Bug: device departs → timer starts, but last_departed_time not set yet
  If device re-arrives before timer fires, timer never cancelled
  Result: false DEPARTED alert sent after re-arrival debounce expires

- Fix: Cancel any pending departure timer immediately on re-arrival,
  before checking last_departed_time

- Test: Added regression test that verifies timer is cancelled when
  device re-arrives before 15-min debounce expires, preventing false alerts

- All 10 tests passing
This commit is contained in:
Cobra
2026-04-09 13:28:34 -04:00
parent 504be80325
commit 67cf907f74
2 changed files with 70 additions and 4 deletions
+6 -4
View File
@@ -423,14 +423,16 @@ def on_arrival(mac: str, ip: str, hostname: str = "") -> None:
# Check re-arrival suppression BEFORE acquiring lock (avoid nested locks)
with departure_lock:
# ALWAYS cancel any pending departure timer when device re-arrives
if mac in departure_timers:
departure_timers[mac].cancel()
del departure_timers[mac]
logging.debug(f"Cancelled departure timer for {mac} — device re-arrived before debounce expired")
if mac in last_departed_time:
time_since_departure = now - last_departed_time[mac]
if time_since_departure < 300: # 5 min
logging.debug(f"Suppressing re-arrival alert for {mac} (departed {int(time_since_departure)}s ago)")
# Clean up departure timer if still pending
if mac in departure_timers:
departure_timers[mac].cancel()
del departure_timers[mac]
# Re-add to known_devices to track presence
with known_lock:
if mac not in known_devices: