From 301bc05143e6c5da2b736fc1abda820d8d064a38 Mon Sep 17 00:00:00 2001 From: Serge Date: Thu, 9 Jan 2025 12:20:07 +0100 Subject: [PATCH 1/5] Fixed #1466 --- custom_components/ocpp/chargepoint.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/custom_components/ocpp/chargepoint.py b/custom_components/ocpp/chargepoint.py index 7fd22e3f..e021023a 100644 --- a/custom_components/ocpp/chargepoint.py +++ b/custom_components/ocpp/chargepoint.py @@ -28,7 +28,7 @@ from ocpp.charge_point import ChargePoint as cp from ocpp.v16 import call as callv16 from ocpp.v16 import call_result as call_resultv16 -from ocpp.v16.enums import UnitOfMeasure, AuthorizationStatus, Measurand, Phase +from ocpp.v16.enums import UnitOfMeasure, AuthorizationStatus, Measurand, Phase, ReadingContext from ocpp.v201 import call as callv201 from ocpp.v201 import call_result as call_resultv201 from ocpp.messages import CallError @@ -766,6 +766,15 @@ def process_measurands( # Charger reports Energy.Active.Import.Register directly as Session energy for transactions. self._charger_reports_session_energy = True + # Only set the meter start value from Transaction begin context + if ( + is_transaction + and context == ReadingContext.transaction_begin.value + ): + self._metrics[csess.meter_start].value = value + self._metrics[csess.meter_start].unit == unit + continue + if phase is None: if ( measurand == DEFAULT_MEASURAND From 9866fb0dcebfc716eec9b04062fffbb4fc6173cb Mon Sep 17 00:00:00 2001 From: Serge Date: Thu, 9 Jan 2025 12:37:18 +0100 Subject: [PATCH 2/5] Fixed #1466, fixed linting issues --- custom_components/ocpp/chargepoint.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/custom_components/ocpp/chargepoint.py b/custom_components/ocpp/chargepoint.py index e021023a..a4b0a193 100644 --- a/custom_components/ocpp/chargepoint.py +++ b/custom_components/ocpp/chargepoint.py @@ -28,7 +28,13 @@ from ocpp.charge_point import ChargePoint as cp from ocpp.v16 import call as callv16 from ocpp.v16 import call_result as call_resultv16 -from ocpp.v16.enums import UnitOfMeasure, AuthorizationStatus, Measurand, Phase, ReadingContext +from ocpp.v16.enums import ( + UnitOfMeasure, + AuthorizationStatus, + Measurand, + Phase, + ReadingContext, +) from ocpp.v201 import call as callv201 from ocpp.v201 import call_result as call_resultv201 from ocpp.messages import CallError @@ -767,13 +773,10 @@ def process_measurands( self._charger_reports_session_energy = True # Only set the meter start value from Transaction begin context - if ( - is_transaction - and context == ReadingContext.transaction_begin.value - ): - self._metrics[csess.meter_start].value = value - self._metrics[csess.meter_start].unit == unit - continue + if is_transaction and context == ReadingContext.transaction_begin.value: + self._metrics[csess.meter_start].value = value + self._metrics[csess.meter_start].unit == unit + continue if phase is None: if ( From ad8e7031fbcc8c62effb470b7bc640bcb798f77a Mon Sep 17 00:00:00 2001 From: Serge Date: Thu, 9 Jan 2025 13:43:21 +0100 Subject: [PATCH 3/5] Attept to pass the automatic test suite --- custom_components/ocpp/chargepoint.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/custom_components/ocpp/chargepoint.py b/custom_components/ocpp/chargepoint.py index a4b0a193..dfc2ce70 100644 --- a/custom_components/ocpp/chargepoint.py +++ b/custom_components/ocpp/chargepoint.py @@ -772,11 +772,9 @@ def process_measurands( # Charger reports Energy.Active.Import.Register directly as Session energy for transactions. self._charger_reports_session_energy = True - # Only set the meter start value from Transaction begin context - if is_transaction and context == ReadingContext.transaction_begin.value: - self._metrics[csess.meter_start].value = value - self._metrics[csess.meter_start].unit == unit - continue + if measurand == DEFAULT_MEASURAND and is_transaction and context == ReadingContext.transaction_begin.value: + # Ignore energy report messages for Transaction.Begin context + continue if phase is None: if ( From 95dc1e16d9e503fef14903446fa19ee3a516fa36 Mon Sep 17 00:00:00 2001 From: Serge Date: Sun, 12 Jan 2025 15:56:31 +0100 Subject: [PATCH 4/5] More elegant way to fix the issue. Now the Active.Import.Register sensor also gets updated --- custom_components/ocpp/chargepoint.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/custom_components/ocpp/chargepoint.py b/custom_components/ocpp/chargepoint.py index dfc2ce70..bc486eb5 100644 --- a/custom_components/ocpp/chargepoint.py +++ b/custom_components/ocpp/chargepoint.py @@ -772,24 +772,24 @@ def process_measurands( # Charger reports Energy.Active.Import.Register directly as Session energy for transactions. self._charger_reports_session_energy = True - if measurand == DEFAULT_MEASURAND and is_transaction and context == ReadingContext.transaction_begin.value: - # Ignore energy report messages for Transaction.Begin context - continue - if phase is None: if ( measurand == DEFAULT_MEASURAND and self._charger_reports_session_energy ): - if is_transaction: - self._metrics[csess.session_energy.value].value = value - self._metrics[csess.session_energy.value].unit = unit - self._metrics[csess.session_energy.value].extra_attr[ - cstat.id_tag.name - ] = self._metrics[cstat.id_tag.value].value - else: + if context != ReadingContext.transaction_begin.value: + # Ignore messages with Transaction Begin context + if is_transaction: + self._metrics[csess.session_energy.value].value = value + self._metrics[csess.session_energy.value].unit = unit + self._metrics[csess.session_energy.value].extra_attr[ + cstat.id_tag.name + ] = self._metrics[cstat.id_tag.value].value + # Always update sensor related to DEFAULT_MEASURAND self._metrics[measurand].value = value self._metrics[measurand].unit = unit + else: + continue else: self._metrics[measurand].value = value self._metrics[measurand].unit = unit From 6dc5fb8837c78f6b5d418959c588dc71cae6e343 Mon Sep 17 00:00:00 2001 From: Serge Date: Mon, 13 Jan 2025 10:29:30 +0100 Subject: [PATCH 5/5] 1. Updated the fix to pass the automatic test system. 2. Fixed linting. --- custom_components/ocpp/chargepoint.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/ocpp/chargepoint.py b/custom_components/ocpp/chargepoint.py index bc486eb5..27334868 100644 --- a/custom_components/ocpp/chargepoint.py +++ b/custom_components/ocpp/chargepoint.py @@ -777,19 +777,19 @@ def process_measurands( measurand == DEFAULT_MEASURAND and self._charger_reports_session_energy ): - if context != ReadingContext.transaction_begin.value: # Ignore messages with Transaction Begin context + if context != ReadingContext.transaction_begin.value: if is_transaction: self._metrics[csess.session_energy.value].value = value self._metrics[csess.session_energy.value].unit = unit self._metrics[csess.session_energy.value].extra_attr[ cstat.id_tag.name ] = self._metrics[cstat.id_tag.value].value - # Always update sensor related to DEFAULT_MEASURAND - self._metrics[measurand].value = value - self._metrics[measurand].unit = unit + else: + self._metrics[measurand].value = value + self._metrics[measurand].unit = unit else: - continue + continue else: self._metrics[measurand].value = value self._metrics[measurand].unit = unit