From 07b4da56a95dc85530b2518d0ec7c6da47309735 Mon Sep 17 00:00:00 2001 From: Josef Zweck <24647999+zweckj@users.noreply.github.com> Date: Thu, 2 Jan 2025 22:10:02 +0100 Subject: [PATCH] handle physical button presses --- aioacaia/acaiascale.py | 36 +++++++++++++++++++++++++++++++++++- pyproject.toml | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/aioacaia/acaiascale.py b/aioacaia/acaiascale.py index 709a50f..1adf069 100644 --- a/aioacaia/acaiascale.py +++ b/aioacaia/acaiascale.py @@ -86,6 +86,7 @@ def __init__( self.timer_running = False self._timer_start: float | None = None self._timer_stop: float | None = None + self._button_pressed = False # connection diagnostics self.connected = False @@ -406,7 +407,9 @@ async def start_stop_timer(self) -> None: (self._default_char_id, self._msg_types["startTimer"]) ) self.timer_running = True - if not self._timer_start: + if self._timer_start is not None and self._timer_stop is not None: + self._timer_start = time.time() - (self._timer_stop - self._timer_start) + else: self._timer_start = time.time() else: _LOGGER.debug('Sending "stop" message.') @@ -498,6 +501,37 @@ async def on_bluetooth_data_received( while self.weight_history and (timestamp - self.weight_history[0][0] > 5): self.weight_history.popleft() + # handle physical button presses + def reset() -> None: + """Physically reset the timer.""" + self._timer_start = None + self._timer_stop = None + self.timer_running = False + self._button_pressed = False + + def reset_on_power_button() -> None: + """Pressing the power button two consecutive times resets the timer.""" + if self._button_pressed: + reset() + else: + self._button_pressed = True + + if msg.button == "start": + self.timer_running = True + if self._timer_start is not None and self._timer_stop is not None: + self._timer_start = time.time() - ( + self._timer_stop - self._timer_start + ) + else: + self._timer_start = time.time() + reset_on_power_button() + elif msg.button == "stop": + self.timer_running = False + self._timer_stop = time.time() + reset_on_power_button() + elif msg.button == "reset": + reset() + if msg.timer_running is not None: self.timer_running = msg.timer_running _LOGGER.debug("Got weight %s", str(msg.value)) diff --git a/pyproject.toml b/pyproject.toml index 1791c93..48cdf28 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "aioacaia" -version = "0.1.12" +version = "0.1.13" license = { text = "MIT" } description = "An async implementation of PyAcaia" readme = "README.md"