Skip to content

Commit

Permalink
fix bug in handling of gaps in Ahoy inverter number sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
dsteinkopf committed Sep 16, 2023
1 parent d70151a commit dddb39e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
6 changes: 4 additions & 2 deletions dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,15 @@ def check_and_enrich_ahoy_data(self, meter_data):
raise ValueError("Response from OpenDTU does not contain ch0_fld_names data")
# not needed: meter_data["record"] = self.fetch_ahoy_record_data()

# add the field "inverter" to meter_data:
# This will contain an array of the "iv" data from all inverters.
meter_data["inverter"] = []
for inverter_number in range(len(meter_data["iv"])):
if is_true(meter_data["iv"][inverter_number]):
iv_data = self.fetch_ahoy_iv_data(inverter_number)
while len(meter_data["inverter"]) < inverter_number:
# there was a gap
meter_data.append({})
# there was a gap in the sequence of inverter numbers -> fill in a dummy value
meter_data["inverter"].append({})
meter_data["inverter"].append(iv_data)

def check_opendtu_data(self, meter_data):
Expand Down
70 changes: 70 additions & 0 deletions docs/ahoy_0.5.93_inverter-id-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"id": 0,
"enabled": true,
"name": "MC1",
"serial": "116199999999",
"version": "10018",
"power_limit_read": 65535,
"ts_last_success": 1680176747,
"ch": [
[
229.5,
0.98,
223.7,
50,
0.997,
34.2,
422.603,
2223,
234.3,
95.476,
18.7
],
[
33.3,
1.75,
58.1,
547,
103.553,
15.493
],
[
33.3,
1.77,
59,
561,
106.497,
15.733
],
[
33.2,
1.76,
58.5,
558,
106.357,
15.6
],
[
33.2,
1.77,
58.7,
557,
106.196,
15.653
]
],
"ch_name": [
"AC",
"11",
"12",
"13",
"14"
],
"ch_max_pwr": [
null,
375,
375,
375,
375
]
}
66 changes: 66 additions & 0 deletions docs/ahoy_0.7.36_live_gap_in_inverter_sequence.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"generic": {
"wifi_rssi": -92,
"ts_uptime": 2777,
"ts_now": 1694698541,
"version": "0.7.36",
"build": "ba218ed",
"menu_prot": false,
"menu_mask": 61,
"menu_protEn": false,
"esp_type": "ESP8266"
},
"refresh": 30,
"ch0_fld_units": [
"V",
"A",
"W",
"Hz",
"",
"°C",
"kWh",
"Wh",
"W",
"%",
"var",
"W"
],
"ch0_fld_names": [
"U_AC",
"I_AC",
"P_AC",
"F_AC",
"PF_AC",
"Temp",
"YieldTotal",
"YieldDay",
"P_DC",
"Efficiency",
"Q_AC",
"MaxPower"
],
"fld_units": [
"V",
"A",
"W",
"Wh",
"kWh",
"%",
"W"
],
"fld_names": [
"U_DC",
"I_DC",
"P_DC",
"YieldDay",
"YieldTotal",
"Irradiation",
"MaxPower"
],
"iv": [
false,
true,
false,
false
]
}
13 changes: 11 additions & 2 deletions test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ def get_ahoy_meterdata(filename):
''' Load the meter data from the json file. '''
with open(filename, encoding="utf-8") as file_json:
json_meter_data = json.load(file_json)

# add the field "inverter" to meter_data:
# This will contain an array of the "iv" data from all inverters.
json_meter_data["inverter"] = []
for inverter_number in range(len(json_meter_data["iv"])):
if is_true(json_meter_data["iv"][inverter_number]):
iv_data = fetch_ahoy_iv_data(inverter_number)
while len(json_meter_data["inverter"]) < inverter_number:
# there was a gap
json_meter_data.append({})
# there was a gap in the sequence of inverter numbers -> fill in a dummy value
json_meter_data["inverter"].append({})
json_meter_data["inverter"].append(iv_data)

return json_meter_data
Expand Down Expand Up @@ -129,6 +132,12 @@ def test_get_ahoy_field_by_name(self):
self.assertEqual(get_ahoy_field_by_name(meter_data_ahoy, 0, "I_DC", False), 1.75)
self.assertEqual(get_ahoy_field_by_name(meter_data_ahoy, 0, "P_DC", False), 58.1)

def test_get_ahoy_gap_in_inverter_sequence(self):
''' Test the special case when there is a gap in the sequence of inverters IDs.'''
meter_data_ahoy_bad_sequence = get_ahoy_meterdata(
filename='./docs/ahoy_0.7.36_live_gap_in_inverter_sequence.json')
self.assertEqual(get_ahoy_field_by_name(meter_data_ahoy_bad_sequence, 1, "P_AC"), 223.7)

def test_is_true(self):
''' Test the is_true() function. '''
self.assertEqual(is_true("1"), True)
Expand Down

0 comments on commit dddb39e

Please sign in to comment.