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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user