check min/max before activating

This commit is contained in:
MichaelDvP
2022-09-28 17:26:49 +02:00
parent 4c728cf777
commit d3053d8ce2
2 changed files with 14 additions and 6 deletions

View File

@@ -327,29 +327,37 @@ bool DeviceValue::get_min_max(int16_t & dv_set_min, int16_t & dv_set_max) {
}
// extract custom min from custom_fullname
bool DeviceValue::get_custom_min(int16_t & v) {
bool DeviceValue::get_custom_min(int16_t & val) {
auto min_pos = custom_fullname.find('>');
bool has_min = (min_pos != std::string::npos);
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
if (has_min) {
v = Helpers::atoint(custom_fullname.substr(min_pos + 1).c_str());
int v = Helpers::atoint(custom_fullname.substr(min_pos + 1).c_str());
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
if (v > max) {
return false;
}
val = v;
}
return has_min;
}
// extract custom max from custom_fullname
bool DeviceValue::get_custom_max(uint16_t & v) {
bool DeviceValue::get_custom_max(uint16_t & val) {
auto max_pos = custom_fullname.find('<');
bool has_max = (max_pos != std::string::npos);
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
if (has_max) {
v = Helpers::atoint(custom_fullname.substr(max_pos + 1).c_str());
int v = Helpers::atoint(custom_fullname.substr(max_pos + 1).c_str());
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
if (v < 0 || v < min) {
return false;
}
val = v;
}
return has_max;
}

View File

@@ -180,8 +180,8 @@ class DeviceValue {
bool get_min_max(int16_t & dv_set_min, int16_t & dv_set_max);
void set_custom_minmax();
bool get_custom_min(int16_t & v);
bool get_custom_max(uint16_t & v);
bool get_custom_min(int16_t & val);
bool get_custom_max(uint16_t & val);
std::string get_custom_fullname() const;
std::string get_fullname() const;