Skip to content

Commit

Permalink
Fix set_weather_regulated_charge result detection
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
torbennehmer committed May 16, 2023
1 parent 30cc0c2 commit ae58e37
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions e3dc/_e3dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2018,27 +2018,34 @@ def set_weather_regulated_charge(self, enable, keepAlive=False):
0 if success
-1 if error
"""
if enable:
res = self.sendRequest(
(
"EMS_REQ_SET_POWER_SETTINGS",
"Container",
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", 1)],
),
keepAlive=keepAlive,
)
else:
res = self.sendRequest(
(
"EMS_REQ_SET_POWER_SETTINGS",
"Container",
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", 0)],
),
keepAlive=keepAlive,
)
# Safely cast this to the boolean we need.
newVal: bool = enable != 0
res = self.sendRequest(
(
"EMS_REQ_SET_POWER_SETTINGS",
"Container",
[("EMS_WEATHER_REGULATED_CHARGE_ENABLED", "UChar8", newVal)],
),
keepAlive=keepAlive,
)

# validate return code for EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED is 0
if res[2][0][2] == 0:
# Returns the new value of EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED, we need
# to validate this against the desired value, the object looks like this:
# [ "EMS_SET_POWER_SETTINGS",
# "Container",
# [
# ["EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED", "Char8", 0]
# ]
# ]
# Unfortunately, various E3DC hardware is slightly inconsistent when it comes
# down to reporting results, we normalize this accordingly. Some E3DCs return
# 1 for enabled, others -1, thus we check for zero/nonzero. With this, we'll
# check if we did arraive at the right value.

retval: bool = (
rscpFindTagIndex(res, "EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED") != 0
)
if retval == newVal:
return 0
else:
return -1

0 comments on commit ae58e37

Please sign in to comment.