From ae58e37c43c8614855051856844cfd2b82047129 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Fri, 28 Apr 2023 12:26:01 +0200 Subject: [PATCH] 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. --- e3dc/_e3dc.py | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/e3dc/_e3dc.py b/e3dc/_e3dc.py index 2b71676..9a8072c 100644 --- a/e3dc/_e3dc.py +++ b/e3dc/_e3dc.py @@ -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