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

47 lines
1.7 KiB
Python

"""Button entities for PerfectDraft."""
from __future__ import annotations
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, keg_changed_signal
from .coordinator import PerfectDraftDataUpdateCoordinator
from .sensor import _device_info
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up PerfectDraft button entities."""
coordinator: PerfectDraftDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([PerfectDraftKegResetButton(coordinator)])
class PerfectDraftKegResetButton(
CoordinatorEntity[PerfectDraftDataUpdateCoordinator], ButtonEntity
):
"""Button that marks the current keg as freshly changed as of now."""
_attr_has_entity_name = True
_attr_translation_key = "mark_keg_changed"
_attr_icon = "mdi:keg"
def __init__(
self,
coordinator: PerfectDraftDataUpdateCoordinator,
) -> None:
super().__init__(coordinator)
self._machine_id = (coordinator.data or {}).get("_machine_id", "unknown")
self._attr_unique_id = f"{self._machine_id}_mark_keg_changed"
self._attr_device_info = _device_info(coordinator)
async def async_press(self) -> None:
"""Tell the freshness sensor to set the keg insertion date to now."""
async_dispatcher_send(self.hass, keg_changed_signal(self._machine_id))