diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index f3b1e4df9..8326e545a 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -1383,11 +1383,12 @@ bool Thermostat::set_wwcharge(const char * value, const int8_t id) { // Set ww charge duration in steps of 15 min, ems+ bool Thermostat::set_wwchargeduration(const char * value, const int8_t id) { - uint8_t t = 0xFF; - if (!Helpers::value2enum(value, t, FL_(enum_wwChargeDuration))) { + int t = 0xFF; + if (!Helpers::value2number(value, t)) { LOG_WARNING(F("Set warm water charge duration: Invalid value")); return false; } + t = (t + 8) / 15; LOG_INFO(F("Setting warm water charge duration to %d min"), t * 15); write_command(0x2F5, 10, t, 0x02F5); return true; @@ -2420,10 +2421,10 @@ void Thermostat::register_device_values() { TAG_THERMOSTAT_DATA, &wwCircMode_, DeviceValueType::ENUM, FL_(enum_wwCircMode), FL_(wwCircMode), DeviceValueUOM::LIST, MAKE_CF_CB(set_wwcircmode)); register_device_value(TAG_THERMOSTAT_DATA, &wwChargeDuration_, - DeviceValueType::ENUM, - FL_(enum_wwChargeDuration), + DeviceValueType::UINT, + FL_(mul15), FL_(wwChargeDuration), - DeviceValueUOM::LIST, + DeviceValueUOM::MINUTES, MAKE_CF_CB(set_wwchargeduration)); register_device_value(TAG_THERMOSTAT_DATA, &wwCharge_, DeviceValueType::BOOL, nullptr, FL_(wwCharge), DeviceValueUOM::BOOLEAN, MAKE_CF_CB(set_wwcharge)); register_device_value(TAG_THERMOSTAT_DATA, &wwExtra1_, DeviceValueType::UINT, nullptr, FL_(wwExtra1), DeviceValueUOM::DEGREES); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index c1eb97c4c..41e9b3dae 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -603,27 +603,36 @@ void EMSdevice::generate_values_json_web(JsonObject & json) { // If a divider is specified, do the division to 2 decimals places and send back as double/float // otherwise force as an integer whole // the nested if's is necessary due to the way the ArduinoJson templates are pre-processed by the compiler - uint8_t divider = (dv.options_size == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0; + uint8_t divider = 0; + uint8_t factor = 1; + if (dv.options_size == 1) { + const char * s = uuid::read_flash_string(dv.options[0]).c_str(); + if (s[0] == '*') { + factor = Helpers::atoint(&s[1]); + } else { + divider = Helpers::atoint(s); + } + } if ((dv.type == DeviceValueType::INT) && Helpers::hasValue(*(int8_t *)(dv.value_p))) { obj = data.createNestedObject(); - obj["v"] = (divider) ? Helpers::round2(*(int8_t *)(dv.value_p), divider) : *(int8_t *)(dv.value_p); + obj["v"] = (divider) ? Helpers::round2(*(int8_t *)(dv.value_p), divider) : *(int8_t *)(dv.value_p) * factor; } else if ((dv.type == DeviceValueType::UINT) && Helpers::hasValue(*(uint8_t *)(dv.value_p))) { obj = data.createNestedObject(); - obj["v"] = (divider) ? Helpers::round2(*(uint8_t *)(dv.value_p), divider) : *(uint8_t *)(dv.value_p); + obj["v"] = (divider) ? Helpers::round2(*(uint8_t *)(dv.value_p), divider) : *(uint8_t *)(dv.value_p) * factor; } else if ((dv.type == DeviceValueType::SHORT) && Helpers::hasValue(*(int16_t *)(dv.value_p))) { obj = data.createNestedObject(); - obj["v"] = (divider) ? Helpers::round2(*(int16_t *)(dv.value_p), divider) : *(int16_t *)(dv.value_p); + obj["v"] = (divider) ? Helpers::round2(*(int16_t *)(dv.value_p), divider) : *(int16_t *)(dv.value_p) * factor; } else if ((dv.type == DeviceValueType::USHORT) && Helpers::hasValue(*(uint16_t *)(dv.value_p))) { obj = data.createNestedObject(); - obj["v"] = (divider) ? Helpers::round2(*(uint16_t *)(dv.value_p), divider) : *(uint16_t *)(dv.value_p); + obj["v"] = (divider) ? Helpers::round2(*(uint16_t *)(dv.value_p), divider) : *(uint16_t *)(dv.value_p) * factor; } else if ((dv.type == DeviceValueType::ULONG) && Helpers::hasValue(*(uint32_t *)(dv.value_p))) { obj = data.createNestedObject(); - obj["v"] = (divider) ? Helpers::round2(*(uint32_t *)(dv.value_p), divider) : *(uint32_t *)(dv.value_p); + obj["v"] = divider ? Helpers::round2(*(uint32_t *)(dv.value_p), divider) : *(uint32_t *)(dv.value_p) * factor; } else if ((dv.type == DeviceValueType::TIME) && Helpers::hasValue(*(uint32_t *)(dv.value_p))) { uint32_t time_value = *(uint32_t *)(dv.value_p); obj = data.createNestedObject(); - obj["v"] = (divider) ? time_value / divider : time_value; // sometimes we need to divide by 60 + obj["v"] = (divider > 0) ? time_value / divider : time_value * factor; // sometimes we need to divide by 60 } } @@ -684,11 +693,20 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t // search device value with this tag for (auto & dv : devicevalues_) { if (strcmp(cmd, Helpers::toLower(uuid::read_flash_string(dv.short_name)).c_str()) == 0 && (tag <= 0 || tag == dv.tag)) { - uint8_t divider = (dv.options_size == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0; - const char * type = "type"; - const char * min = "min"; - const char * max = "max"; - const char * value = "value"; + uint8_t divider = 0; + uint8_t factor = 1; + if (dv.options_size == 1) { + const char * s = uuid::read_flash_string(dv.options[0]).c_str(); + if (s[0] == '*') { + factor = Helpers::atoint(&s[1]); + } else { + divider = Helpers::atoint(s); + } + } + const char * type = "type"; + const char * min = "min"; + const char * max = "max"; + const char * value = "value"; json["name"] = dv.short_name; // prefix tag if it's included @@ -726,7 +744,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::USHORT: if (Helpers::hasValue(*(uint16_t *)(dv.value_p))) { - json[value] = Helpers::round2(*(uint16_t *)(dv.value_p), divider); + json[value] = divider ? Helpers::round2(*(uint16_t *)(dv.value_p), divider) : *(uint16_t *)(dv.value_p) * factor; } json[type] = F_(number); json[min] = 0; @@ -735,7 +753,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::UINT: if (Helpers::hasValue(*(uint8_t *)(dv.value_p))) { - json[value] = Helpers::round2(*(uint8_t *)(dv.value_p), divider); + json[value] = divider ? Helpers::round2(*(uint8_t *)(dv.value_p), divider) : *(uint8_t *)(dv.value_p) * factor; } json[type] = F_(number); json[min] = 0; @@ -748,7 +766,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::SHORT: if (Helpers::hasValue(*(int16_t *)(dv.value_p))) { - json[value] = Helpers::round2(*(int16_t *)(dv.value_p), divider); + json[value] = divider ? Helpers::round2(*(int16_t *)(dv.value_p), divider) : *(int16_t *)(dv.value_p) * factor; } json[type] = F_(number); json[min] = divider ? -EMS_VALUE_SHORT_NOTSET / divider : -EMS_VALUE_SHORT_NOTSET; @@ -757,7 +775,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::INT: if (Helpers::hasValue(*(int8_t *)(dv.value_p))) { - json[value] = Helpers::round2(*(int8_t *)(dv.value_p), divider); + json[value] = divider ? Helpers::round2(*(int8_t *)(dv.value_p), divider) : *(int8_t *)(dv.value_p) * factor; } json[type] = F_(number); if (dv.uom == DeviceValueUOM::PERCENT) { @@ -771,7 +789,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::ULONG: if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) { - json[value] = Helpers::round2(*(uint32_t *)(dv.value_p), divider); + json[value] = divider ? Helpers::round2(*(uint32_t *)(dv.value_p), divider) : *(uint32_t *)(dv.value_p) * factor; } json[type] = F_(number); json[min] = 0; @@ -797,7 +815,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t case DeviceValueType::TIME: if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) { - json[value] = (divider) ? *(uint32_t *)(dv.value_p) / divider : *(uint32_t *)(dv.value_p); + json[value] = (divider) ? *(uint32_t *)(dv.value_p) / divider : *(uint32_t *)(dv.value_p) * factor; } json[type] = F_(number); json[min] = 0; @@ -918,47 +936,56 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter // If a divider is specified, do the division to 2 decimals places and send back as double/float // otherwise force as an integer whole // the nested if's is necessary due to the way the ArduinoJson templates are pre-processed by the compiler - uint8_t divider = (dv.options_size == 1) ? Helpers::atoint(uuid::read_flash_string(dv.options[0]).c_str()) : 0; + uint8_t divider = 0; + uint8_t factor = 1; + if (dv.options_size == 1) { + const char * s = uuid::read_flash_string(dv.options[0]).c_str(); + if (s[0] == '*') { + factor = Helpers::atoint(&s[1]); + } else { + divider = Helpers::atoint(s); + } + } // INT if ((dv.type == DeviceValueType::INT) && Helpers::hasValue(*(int8_t *)(dv.value_p))) { if (divider) { json[name] = Helpers::round2(*(int8_t *)(dv.value_p), divider); } else { - json[name] = *(int8_t *)(dv.value_p); + json[name] = *(int8_t *)(dv.value_p) * factor; } has_value = true; } else if ((dv.type == DeviceValueType::UINT) && Helpers::hasValue(*(uint8_t *)(dv.value_p))) { if (divider) { json[name] = Helpers::round2(*(uint8_t *)(dv.value_p), divider); } else { - json[name] = *(uint8_t *)(dv.value_p); + json[name] = *(uint8_t *)(dv.value_p) * factor; } has_value = true; } else if ((dv.type == DeviceValueType::SHORT) && Helpers::hasValue(*(int16_t *)(dv.value_p))) { if (divider) { json[name] = Helpers::round2(*(int16_t *)(dv.value_p), divider); } else { - json[name] = *(int16_t *)(dv.value_p); + json[name] = *(int16_t *)(dv.value_p) * factor; } has_value = true; } else if ((dv.type == DeviceValueType::USHORT) && Helpers::hasValue(*(uint16_t *)(dv.value_p))) { if (divider) { json[name] = Helpers::round2(*(uint16_t *)(dv.value_p), divider); } else { - json[name] = *(uint16_t *)(dv.value_p); + json[name] = *(uint16_t *)(dv.value_p) * factor; } has_value = true; } else if ((dv.type == DeviceValueType::ULONG) && Helpers::hasValue(*(uint32_t *)(dv.value_p))) { if (divider) { json[name] = Helpers::round2(*(uint32_t *)(dv.value_p), divider); } else { - json[name] = *(uint32_t *)(dv.value_p); + json[name] = *(uint32_t *)(dv.value_p) * factor; } has_value = true; } else if ((dv.type == DeviceValueType::TIME) && Helpers::hasValue(*(uint32_t *)(dv.value_p))) { uint32_t time_value = *(uint32_t *)(dv.value_p); - time_value = (divider) ? time_value / divider : time_value; // sometimes we need to divide by 60 + time_value = (divider) ? time_value / divider : time_value * factor; // sometimes we need to divide by 60 if (console) { char time_s[40]; snprintf_P(time_s, sizeof(time_s), "%d days %d hours %d minutes", (time_value / 1440), ((time_value % 1440) / 60), (time_value % 60)); diff --git a/src/helpers.cpp b/src/helpers.cpp index 949326686..bc1f41d88 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -350,7 +350,7 @@ uint32_t Helpers::hextoint(const char * hex) { // quick char to long uint16_t Helpers::atoint(const char * value) { unsigned int x = 0; - while (*value != '\0') { + while (*value >= '0' && *value <= '9') { x = (x * 10) + (*value - '0'); ++value; } diff --git a/src/locale_EN.h b/src/locale_EN.h index a3837591f..a32081665 100644 --- a/src/locale_EN.h +++ b/src/locale_EN.h @@ -162,6 +162,8 @@ MAKE_PSTR_LIST(div2, F_(2)) MAKE_PSTR_LIST(div10, F_(10)) MAKE_PSTR_LIST(div100, F_(100)) MAKE_PSTR_LIST(div60, F_(60)) +MAKE_PSTR_LIST(mul10, F("*10")) +MAKE_PSTR_LIST(mul15, F("*15")) // Unit Of Measurement mapping - maps to DeviceValueUOM_s in emsdevice.cpp // uom - also used with HA see https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L384 @@ -352,7 +354,7 @@ MAKE_PSTR_LIST(enum_control, F_(off), F_(rc20), F_(rc3x)) MAKE_PSTR_LIST(enum_wwProgMode, F("std prog"), F_(own_prog)) MAKE_PSTR_LIST(enum_dayOfWeek, F("mo"), F("tu"), F("we"), F("th"), F("fr"), F("sa"), F("so"), F("all")) -MAKE_PSTR_LIST(enum_wwChargeDuration, F_(off), F("15min"), F("30min"), F("45min"), F("60min"), F("75min"), F("90min"), F("105min"), F("120min")) +// MAKE_PSTR_LIST(enum_wwChargeDuration, F_(off), F("15min"), F("30min"), F("45min"), F("60min"), F("75min"), F("90min"), F("105min"), F("120min")) // solar list MAKE_PSTR_LIST(enum_solarmode, F_(constant), F("pwm"), F("analog"))