Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions plugwise_usb/nodes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,11 @@ def _calc_watts(self, pulses: int, seconds: int, nano_offset: int) -> float | No
return None

pulses_per_s = self._correct_power_pulses(pulses, nano_offset) / float(seconds)
negative = False
if pulses_per_s < 0:
negative = True
pulses_per_s = abs(pulses_per_s)

corrected_pulses = seconds * (
(
(
Expand All @@ -1037,18 +1042,10 @@ def _calc_watts(self, pulses: int, seconds: int, nano_offset: int) -> float | No
)
+ self._calibration.off_tot
)
if negative:
corrected_pulses = -corrected_pulses

# Fix minor miscalculations
if (
calc_value := corrected_pulses / PULSES_PER_KW_SECOND / seconds * (1000)
) >= 0.0:
return calc_value
_LOGGER.debug(
"Correct negative power %s to 0.0 for %s",
str(corrected_pulses / PULSES_PER_KW_SECOND / seconds * 1000),
self._mac_in_str,
)
return 0.0
return corrected_pulses / PULSES_PER_KW_SECOND / seconds * (1000)

def _correct_power_pulses(self, pulses: int, offset: int) -> float:
"""Correct pulses based on given measurement time offset (ns)."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "plugwise_usb"
version = "v0.40.0a31"
version = "v0.40.0a33"
license = {file = "LICENSE"}
description = "Plugwise USB (Stick) module for Python 3."
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion tests/stick_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@
b"0013" # msg_id
+ b"0098765432101234" # mac
+ b"000A" # pulses 1s
+ b"0066" # pulses 8s
+ b"FF9A" # pulses 8s
+ b"00001234"
+ b"00000000"
+ b"0004",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ async def fake_get_missing_energy_logs(address: int) -> None:
)
pu = await stick.nodes["0098765432101234"].power_update()
assert pu.last_second == 21.2780505980402
assert pu.last_8_seconds == 27.150578775440106
assert pu.last_8_seconds == -27.150578775440106

# Test energy state without request
assert stick.nodes["0098765432101234"].energy == pw_api.EnergyStatistics(
Expand Down
Loading