Skip to content

Commit

Permalink
Fix v34 negative power value parsing (squishykid#51)
Browse files Browse the repository at this point in the history
* Make failing smoke test easier to debug.

* Fix v34 negative power value parsing.

* Ingore VIM swap files.

* Fix lint issue.

* Try squishykid#2 at fixing lint issue.
  • Loading branch information
ppetru authored Dec 19, 2021
1 parent 5b58e37 commit b0d315b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/build/**
.venv/**
.coverage
.*.swp
18 changes: 9 additions & 9 deletions solax/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from voluptuous.humanize import humanize_error
from solax.utils import (
div10, div100, energy,
consumption, twoway_current, to_signed
consumption, twoway_div10, twoway_div100, to_signed
)


Expand Down Expand Up @@ -335,13 +335,13 @@ class X3V34(InverterPost):
'Network Voltage Phase 2': (1, 'V', div10),
'Network Voltage Phase 3': (2, 'V', div10),

'Output Current Phase 1': (3, 'A', div10),
'Output Current Phase 2': (4, 'A', div10),
'Output Current Phase 3': (5, 'A', div10),
'Output Current Phase 1': (3, 'A', twoway_div10),
'Output Current Phase 2': (4, 'A', twoway_div10),
'Output Current Phase 3': (5, 'A', twoway_div10),

'Power Now Phase 1': (6, 'W'),
'Power Now Phase 2': (7, 'W'),
'Power Now Phase 3': (8, 'W'),
'Power Now Phase 1': (6, 'W', to_signed),
'Power Now Phase 2': (7, 'W', to_signed),
'Power Now Phase 3': (8, 'W', to_signed),

'PV1 Voltage': (9, 'V', div10),
'PV2 Voltage': (10, 'V', div10),
Expand All @@ -358,7 +358,7 @@ class X3V34(InverterPost):
'Today\'s Energy': (21, 'kWh', div10),

'Battery Voltage': (24, 'V', div100),
'Battery Current': (25, 'A', twoway_current),
'Battery Current': (25, 'A', twoway_div100),
'Battery Power': (26, 'W', to_signed),
'Battery Temperature': (27, 'C'),
'Battery Remaining Capacity': (28, '%'),
Expand All @@ -369,7 +369,7 @@ class X3V34(InverterPost):
'Total Consumption': (69, 'kWh', consumption),
'Total Consumption Resets': (70, ''),

'AC Power': (181, 'W'),
'AC Power': (181, 'W', to_signed),
}

@classmethod
Expand Down
12 changes: 8 additions & 4 deletions solax/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ def consumption(value, mapped_sensor_data, *_args, **_kwargs):
return value


def twoway_current(val, *_args, **_kwargs):
return to_signed(val, None) / 10


def to_signed(val, *_args, **_kwargs):
if val > 32767:
val -= 65535
return val


def twoway_div10(val, *_args, **_kwargs):
return to_signed(val, None) / 10


def twoway_div100(val, *_args, **_kwargs):
return to_signed(val, None) / 100
73 changes: 72 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@
"Information": [8.000, 5, "XXXXXXXX", 1, 4.47, 0.00, 4.34, 1.05, 0.0, 1]
}

X3_HYBRID_G3_2X_MPPT_RESPONSE_V34_NEGATIVE_POWER = {
"type": 5,
"sn": "XXXXXXXXXX",
"ver": "2.034.06",
"Data": [2364, 2431, 2386, 65463, 65464, 65464, 63798, 63769, 63807, 0, 0,
0, 0, 0, 0, 4998, 4998, 4998, 2, 19251, 1, 84, 0, 0, 20460, 2500,
5117, 24, 20, 0, 24696, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 100, 0, 36, 7577, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59792, 65535, 31674, 5, 29027, 1, 510, 36, 256, 2352, 1568, 310,
350, 217, 205, 36, 36, 258, 1, 1, 13, 0, 28876, 0, 30616, 1, 21,
0, 5236, 0, 65535, 65535, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 936, 0,
0, 21, 0, 98, 40, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 256, 4392,
7702, 5386, 1107, 512, 8481, 8481, 0, 0, 4369, 0, 273, 2037, 243,
4949, 3645, 3638, 12, 100, 21302, 19778, 18003, 12355, 16697,
12354, 14132, 21302, 13110, 12338, 12337, 14386, 12354, 12852,
21302, 13110, 12338, 12337, 14386, 12354, 12340, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 60302, 1, 257, 257, 770, 1028, 0, 20460,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"Information": [8.000, 5, "XXXXXXXX", 1, 4.60, 0.00, 4.42, 1.05, 0.00, 1]
}

X3_VALUES = {
'PV1 Current': 0,
'PV2 Current': 1,
Expand Down Expand Up @@ -334,7 +355,7 @@
'Today\'s Energy': 10.3,

'Battery Voltage': 229.3,
'Battery Current': 9,
'Battery Current': 0.9,
'Battery Power': 229,
'Battery Temperature': 22,
'Battery Remaining Capacity': 99,
Expand All @@ -348,6 +369,48 @@
'AC Power': 686,
}

X3V34_HYBRID_VALUES_NEGATIVE_POWER = {
'Network Voltage Phase 1': 236.4,
'Network Voltage Phase 2': 243.1,
'Network Voltage Phase 3': 238.6,

'Output Current Phase 1': -7.2,
'Output Current Phase 2': -7.1,
'Output Current Phase 3': -7.1,

'Power Now Phase 1': -1737,
'Power Now Phase 2': -1766,
'Power Now Phase 3': -1728,

'PV1 Voltage': 0,
'PV2 Voltage': 0,
'PV1 Current': 0,
'PV2 Current': 0,
'PV1 Power': 0,
'PV2 Power': 0,

'Grid Frequency Phase 1': 49.98,
'Grid Frequency Phase 2': 49.98,
'Grid Frequency Phase 3': 49.98,

'Total Energy': 1925.1,
'Today\'s Energy': 8.4,

'Battery Voltage': 204.6,
'Battery Current': 25,
'Battery Power': 5117,
'Battery Temperature': 24,
'Battery Remaining Capacity': 20,

'Exported Power': -5743,
'Total Feed-in Energy': 3593.49,
'Total Feed-in Energy Resets': 5,
'Total Consumption': 945.62,
'Total Consumption Resets': 1,

'AC Power': -5233,
}

X1_VALUES = {
'PV1 Current': 0,
'PV2 Current': 1,
Expand Down Expand Up @@ -521,6 +584,14 @@ def simple_http_fixture(httpserver):
response=X3_HYBRID_G3_2X_MPPT_RESPONSE_V34,
inverter=inverter.X3V34,
values=X3V34_HYBRID_VALUES,
),
InverterUnderTest(
uri="/",
method='POST',
query_string='optType=ReadRealTimeData',
response=X3_HYBRID_G3_2X_MPPT_RESPONSE_V34_NEGATIVE_POWER,
inverter=inverter.X3V34,
values=X3V34_HYBRID_VALUES_NEGATIVE_POWER,
)
]

Expand Down
4 changes: 3 additions & 1 deletion tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def test_inverter_sensors_match():
)
for i, expected_values in test_values:
sensor_map = i.sensor_map()
assert len(sensor_map) == len(expected_values)
msg = f"""{sorted(sensor_map.keys())} vs
{sorted(expected_values.keys())}"""
assert len(sensor_map) == len(expected_values), msg
for name, _ in sensor_map.items():
assert name in expected_values

0 comments on commit b0d315b

Please sign in to comment.