Skip to content

Commit

Permalink
Merge pull request #7 from zweckj/feature/physical-buttons
Browse files Browse the repository at this point in the history
handle physical button presses
  • Loading branch information
zweckj authored Jan 2, 2025
2 parents a366620 + 07b4da5 commit bc97a8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion aioacaia/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit bc97a8e

Please sign in to comment.