Skip to content

Commit ae58e37

Browse files
committed
Fix set_weather_regulated_charge result detection
- normalize all arguments to a boolean - fix result code detection, some E3DCs report active weather regulation as 1, others as -1 - result code is the new state of weather regulated charging, so we deduce the success value bycomparing with the originally requested value. - Update source code comments accordingly.
1 parent 30cc0c2 commit ae58e37

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

e3dc/_e3dc.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,27 +2018,34 @@ def set_weather_regulated_charge(self, enable, keepAlive=False):
20182018
0 if success
20192019
-1 if error
20202020
"""
2021-
if enable:
2022-
res = self.sendRequest(
2023-
(
2024-
"EMS_REQ_SET_POWER_SETTINGS",
2025-
"Container",
2026-
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", 1)],
2027-
),
2028-
keepAlive=keepAlive,
2029-
)
2030-
else:
2031-
res = self.sendRequest(
2032-
(
2033-
"EMS_REQ_SET_POWER_SETTINGS",
2034-
"Container",
2035-
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", 0)],
2036-
),
2037-
keepAlive=keepAlive,
2038-
)
2021+
# Safely cast this to the boolean we need.
2022+
newVal: bool = enable != 0
2023+
res = self.sendRequest(
2024+
(
2025+
"EMS_REQ_SET_POWER_SETTINGS",
2026+
"Container",
2027+
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", newVal)],
2028+
),
2029+
keepAlive=keepAlive,
2030+
)
20392031

2040-
# validate return code for EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED is 0
2041-
if res[2][0][2] == 0:
2032+
# Returns the new value of EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED, we need
2033+
# to validate this against the desired value, the object looks like this:
2034+
# [ "EMS_SET_POWER_SETTINGS",
2035+
# "Container",
2036+
# [
2037+
# ["EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED", "Char8", 0]
2038+
# ]
2039+
# ]
2040+
# Unfortunately, various E3DC hardware is slightly inconsistent when it comes
2041+
# down to reporting results, we normalize this accordingly. Some E3DCs return
2042+
# 1 for enabled, others -1, thus we check for zero/nonzero. With this, we'll
2043+
# check if we did arraive at the right value.
2044+
2045+
retval: bool = (
2046+
rscpFindTagIndex(res, "EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED") != 0
2047+
)
2048+
if retval == newVal:
20422049
return 0
20432050
else:
20442051
return -1

0 commit comments

Comments
 (0)