diff --git a/plugwise_usb/nodes/circle.py b/plugwise_usb/nodes/circle.py index 5886f580b..7daa30265 100644 --- a/plugwise_usb/nodes/circle.py +++ b/plugwise_usb/nodes/circle.py @@ -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 * ( ( ( @@ -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).""" diff --git a/pyproject.toml b/pyproject.toml index f084e85d9..8f118df71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tests/stick_test_data.py b/tests/stick_test_data.py index e3623f61e..8c7c57293 100644 --- a/tests/stick_test_data.py +++ b/tests/stick_test_data.py @@ -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", diff --git a/tests/test_usb.py b/tests/test_usb.py index c96e5cc27..da959c3a4 100644 --- a/tests/test_usb.py +++ b/tests/test_usb.py @@ -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(