Files
rickandCursor f5c9019802 feat: detect keg change by volume jump + manual reset button
Keg-change detection previously fired only when the pour counter reset
to 0 while the keg read near full. That misses the common case where the
user swaps the keg and pours immediately, so pours never reads 0.

Detection now also fires on two volume-based signals (independent of the
pour counter): (b) remaining volume jumping from below 80% to at least
95%, and (c) any upward jump of at least 50 percentage points. The
decision logic is extracted to a dependency-free keg_detection module
with unit tests covering each trigger and the swap-then-pour case.

Adds a "Mark Keg Changed" button as a deterministic manual override,
wired to the freshness sensor via a dispatcher signal; the sensor now
tracks and restores last_volume alongside last_pours.

Bumps version to 0.3.0.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-31 21:04:44 +02:00

54 lines
1.9 KiB
Python

"""Pure keg-change detection logic — no Home Assistant dependencies.
Kept import-free so the decision logic can be unit-tested standalone,
without a Home Assistant test harness.
"""
from __future__ import annotations
KEG_TOTAL_VOLUME = 6.0 # litres — a full keg
KEG_NEW_VOLUME_THRESHOLD = 5.5 # litres — "near full" for the pour-reset signal
# Volume-jump thresholds, expressed as percent of a full keg.
KEG_REFILL_LOW_PCT = 80 # previous level below this counts as "low" (4.8 L)
KEG_REFILL_FULL_PCT = 95 # current level at/above this counts as "near full" (5.7 L)
KEG_JUMP_PCT = 50 # an upward step at least this large implies a swap (3.0 L)
def detect_keg_change(
*,
last_pours: int | None,
last_volume: float | None,
pours: int,
volume: float,
) -> bool:
"""Return True when the readings indicate a fresh keg was inserted.
Any single trigger is sufficient:
(a) pour counter reads 0 while the keg is near full (the original signal);
(b) remaining volume jumped from below 80% to at least 95% of capacity;
(c) remaining volume jumped up by at least 50 percentage points.
Triggers (b) and (c) compare against the previous reading, so they are
skipped on the first reading after install/restart (``last_volume is None``)
to avoid a false positive.
"""
# (a) Pour-reset signal — unchanged from the original behaviour.
if pours == 0 and volume > KEG_NEW_VOLUME_THRESHOLD and last_pours != 0:
return True
if last_volume is None:
return False
pct = volume / KEG_TOTAL_VOLUME * 100
last_pct = last_volume / KEG_TOTAL_VOLUME * 100
# (b) Refilled to near-full from a low level.
if last_pct < KEG_REFILL_LOW_PCT and pct >= KEG_REFILL_FULL_PCT:
return True
# (c) Large upward jump in remaining volume.
if pct - last_pct >= KEG_JUMP_PCT:
return True
return False