Skip to content

Commit

Permalink
fix weight adding
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Nov 25, 2024
1 parent 3b2a753 commit 6f6aacd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
37 changes: 20 additions & 17 deletions aioacaia/acaiascale.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def __init__(
self._weight: float | None = None

# flow rate
self.weight_history: deque[tuple[float, float]] = deque(maxlen=20) # Limit to 20 entries

self.weight_history: deque[tuple[float, float]] = deque(
maxlen=20
) # Limit to 20 entries

# queue
self._queue: asyncio.Queue = asyncio.Queue()
Expand Down Expand Up @@ -143,17 +144,12 @@ def timer(self) -> int:
return 0

return int(self._timer_stop - self._timer_start)

@property
def flow_rate(self) -> float | None:
now = time.time()
"""Calculate the current flow rate."""
flows = []

# Remove old readings (more than 5 seconds)
self.weight_history = [
(t, w) for t, w in self.weight_history if now - t <= 5
]

if len(self.weight_history) < 4:
return None

Expand All @@ -165,7 +161,6 @@ def flow_rate(self) -> float | None:
time_diff = curr_time - prev_time
weight_diff = curr_weight - prev_weight


# Validate weight difference and flow rate limits
flow = weight_diff / time_diff
if flow <= 20.0: # Flow rate limit
Expand Down Expand Up @@ -485,13 +480,21 @@ async def on_bluetooth_data_received(
self._weight = msg.value
timestamp = time.time()

# Check if weight is increasing before appending
if not self.weight_history or msg.value > self.weight_history[-1][1]:
self.weight_history.append((timestamp, msg.value))
elif msg.value < self.weight_history[-1][1] - 1:
# Clear history if weight decreases (1gr margin error)
self.weight_history.clear()
self.weight_history.append((timestamp, msg.value))
# add to weight history for flow rate calculation
if msg.value:
if self.weight_history:
# Check if weight is increasing before appending
if msg.value > self.weight_history[-1][1]:
self.weight_history.append((timestamp, msg.value))
elif msg.value < self.weight_history[-1][1] - 1:
# Clear history if weight decreases (1gr margin error)
self.weight_history.clear()
self.weight_history.append((timestamp, msg.value))
else:
self.weight_history.append((timestamp, msg.value))
# Remove old readings (more than 5 seconds)
while self.weight_history and (timestamp - self.weight_history[0][0] > 5):
self.weight_history.popleft()

if msg.timer_running is not None:
self.timer_running = msg.timer_running
Expand Down
20 changes: 11 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[project]
name = "aioacaia"
version = "0.1.8"
license = {text = "MIT License"}
version = "0.1.9"
license = { text = "MIT License" }
description = "An async implementation of PyAcaia"
readme = "README.md"
authors = [{name = "Josef Zweck", email = "[email protected]"}]
maintainers = [{name = "Josef Zweck", email = "[email protected]"}]
authors = [
{ name = "Josef Zweck", email = "[email protected]" },
]
maintainers = [
{ name = "Josef Zweck", email = "[email protected]" },
]
keywords = ["Acaia", "Bluetooth", "api", "async", "client"]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand All @@ -16,10 +20,8 @@ classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"bleak >= 0.20.2",
]
requires-python = "~= 3.12"
dependencies = ["bleak >= 0.20.2"]
requires-python = ">= 3.12"

[project.urls]
Homepage = "https://github.com/zweckj/aioacaia"
Expand All @@ -34,4 +36,4 @@ dev = [
"pytest == 8.3.3",
"pytest-asyncio == 0.24.0",
"pytest-cov == 6.0.0",
]
]

0 comments on commit 6f6aacd

Please sign in to comment.