From f06175af03c202c130d36b52ab47b09e2cd4c5c6 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Wed, 19 Feb 2025 17:23:14 +0100 Subject: [PATCH 1/5] Try negative amounts of pulses --- tests/stick_test_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stick_test_data.py b/tests/stick_test_data.py index e3623f61e..d009aab2b 100644 --- a/tests/stick_test_data.py +++ b/tests/stick_test_data.py @@ -689,8 +689,8 @@ b"000000C1", # Success ack b"0013" # msg_id + b"0098765432101234" # mac - + b"000A" # pulses 1s - + b"0066" # pulses 8s + + b"FFF6" # pulses 1s + + b"FF9A" # pulses 8s + b"00001234" + b"00000000" + b"0004", From 5c21e36d4e1162daf3c5464bfb408ec436bc4807 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Wed, 19 Feb 2025 17:27:02 +0100 Subject: [PATCH 2/5] Disable protection --- plugwise_usb/nodes/circle.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugwise_usb/nodes/circle.py b/plugwise_usb/nodes/circle.py index 5886f580b..abd165c9c 100644 --- a/plugwise_usb/nodes/circle.py +++ b/plugwise_usb/nodes/circle.py @@ -1039,16 +1039,16 @@ def _calc_watts(self, pulses: int, seconds: int, nano_offset: int) -> float | No ) # 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 + # 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 def _correct_power_pulses(self, pulses: int, offset: int) -> float: """Correct pulses based on given measurement time offset (ns).""" From 2c1a127012765d69966bc76670e9e2626a1b02ca Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Wed, 19 Feb 2025 19:23:43 +0100 Subject: [PATCH 3/5] Try method from PHP-code --- plugwise_usb/nodes/circle.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/plugwise_usb/nodes/circle.py b/plugwise_usb/nodes/circle.py index abd165c9c..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).""" From f3bed4d793510a43406bbec1f432f7ac7a9bc222 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Fri, 21 Feb 2025 18:46:41 +0100 Subject: [PATCH 4/5] Change to a production/negative power return and assert --- tests/stick_test_data.py | 2 +- tests/test_usb.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stick_test_data.py b/tests/stick_test_data.py index d009aab2b..8c7c57293 100644 --- a/tests/stick_test_data.py +++ b/tests/stick_test_data.py @@ -689,7 +689,7 @@ b"000000C1", # Success ack b"0013" # msg_id + b"0098765432101234" # mac - + b"FFF6" # pulses 1s + + b"000A" # pulses 1s + b"FF9A" # pulses 8s + b"00001234" + b"00000000" 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( From 427d625eb1b70f8bf41f3fff9531dd026c95c540 Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk Date: Fri, 21 Feb 2025 19:08:23 +0100 Subject: [PATCH 5/5] Bump to a33 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"