Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty value #1336

Merged
merged 5 commits into from
Sep 26, 2024
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
30 changes: 19 additions & 11 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,14 +1116,19 @@ def average_of_nonzero(values):
value = item.get(om.value.value, None)
unit = item.get(om.unit.value, None)
context = item.get(om.context.value, None)
# where an empty string is supplied convert to 0
try:
value = float(value)
except ValueError:
value = 0
if measurand is not None and phase is not None and unit is not None:
if measurand not in measurand_data:
measurand_data[measurand] = {}
measurand_data[measurand][om.unit.value] = unit
measurand_data[measurand][phase] = float(value)
measurand_data[measurand][phase] = value
self._metrics[measurand].unit = unit
self._metrics[measurand].extra_attr[om.unit.value] = unit
self._metrics[measurand].extra_attr[phase] = float(value)
self._metrics[measurand].extra_attr[phase] = value
self._metrics[measurand].extra_attr[om.context.value] = context

line_phases = [Phase.l1.value, Phase.l2.value, Phase.l3.value, Phase.n.value]
Expand Down Expand Up @@ -1226,6 +1231,11 @@ def on_meter_values(self, connector_id: int, meter_value: dict, **kwargs):
phase = sampled_value.get(om.phase.value, None)
location = sampled_value.get(om.location.value, None)
context = sampled_value.get(om.context.value, None)
# where an empty string is supplied convert to 0
try:
value = float(value)
except ValueError:
value = 0

if len(sampled_value.keys()) == 1: # Backwards compatibility
measurand = DEFAULT_MEASURAND
Expand All @@ -1240,35 +1250,33 @@ def on_meter_values(self, connector_id: int, meter_value: dict, **kwargs):

if phase is None:
if unit == DEFAULT_POWER_UNIT:
self._metrics[measurand].value = float(value) / 1000
self._metrics[measurand].value = value / 1000
self._metrics[measurand].unit = HA_POWER_UNIT
elif (
measurand == DEFAULT_MEASURAND
and self._charger_reports_session_energy
):
if transaction_matches:
if unit == DEFAULT_ENERGY_UNIT:
value = float(value) / 1000
value = value / 1000
unit = HA_ENERGY_UNIT
self._metrics[csess.session_energy.value].value = float(
value
)
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 unit == DEFAULT_ENERGY_UNIT:
value = float(value) / 1000
value = value / 1000
unit = HA_ENERGY_UNIT
self._metrics[measurand].value = float(value)
self._metrics[measurand].value = value
self._metrics[measurand].unit = unit
elif unit == DEFAULT_ENERGY_UNIT:
if transaction_matches:
self._metrics[measurand].value = float(value) / 1000
self._metrics[measurand].value = value / 1000
self._metrics[measurand].unit = HA_ENERGY_UNIT
else:
self._metrics[measurand].value = float(value)
self._metrics[measurand].value = value
self._metrics[measurand].unit = unit
if location is not None:
self._metrics[measurand].extra_attr[om.location.value] = (
Expand Down
4 changes: 2 additions & 2 deletions tests/test_charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,14 @@ async def send_meter_periodic_data(self):
"location": "Outlet",
},
{
"value": "0.000",
"value": "",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
"unit": "kW",
},
{
"value": "0.000",
"value": "",
"context": "Sample.Periodic",
"measurand": "Power.Active.Import",
"location": "Outlet",
Expand Down