fahrenheit uom

This commit is contained in:
MichaelDvP
2022-02-16 18:59:22 +01:00
parent b2eaca27de
commit 3b41d6fff6
3 changed files with 15 additions and 20 deletions

View File

@@ -361,10 +361,10 @@ bool DallasSensor::command_info(const char * value, const int8_t id, JsonObject
JsonObject dataSensor = output.createNestedObject(sensor.name()); JsonObject dataSensor = output.createNestedObject(sensor.name());
dataSensor["id_str"] = sensor.id_str(); dataSensor["id_str"] = sensor.id_str();
if (Helpers::hasValue(sensor.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
dataSensor["temp"] = (float)(sensor.temperature_c) / 10; dataSensor["temp"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} else if (Helpers::hasValue(sensor.temperature_c)) { } else if (Helpers::hasValue(sensor.temperature_c)) {
output[sensor.name()] = (float)(sensor.temperature_c) / 10; output[sensor.name()] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} }
@@ -378,11 +378,11 @@ bool DallasSensor::get_value_info(JsonObject & output, const char * cmd, const i
output["id_str"] = sensor.id_str(); output["id_str"] = sensor.id_str();
output["name"] = sensor.name(); output["name"] = sensor.name();
if (Helpers::hasValue(sensor.temperature_c)) { if (Helpers::hasValue(sensor.temperature_c)) {
output["value"] = (float)(sensor.temperature_c) / 10; output["value"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
output["type"] = F_(number); output["type"] = F_(number);
output["min"] = -55; output["min"] = Helpers::round2(-55, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
output["max"] = 125; output["max"] = Helpers::round2(125, 0, EMSESP::system_.fahrenheit() ? 2 : 0);
output["unit"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES); output["unit"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
output["writeable"] = false; output["writeable"] = false;
return true; return true;
@@ -444,10 +444,10 @@ void DallasSensor::publish_values(const bool force) {
JsonObject dataSensor = doc.createNestedObject(sensor.id_str()); JsonObject dataSensor = doc.createNestedObject(sensor.id_str());
dataSensor["name"] = sensor.name(); dataSensor["name"] = sensor.name();
if (has_value) { if (has_value) {
dataSensor["temp"] = (float)(sensor.temperature_c) / 10; dataSensor["temp"] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
} else if (has_value) { } else if (has_value) {
doc[sensor.name()] = (float)(sensor.temperature_c) / 10; doc[sensor.name()] = Helpers::round2((float)(sensor.temperature_c), 10, EMSESP::system_.fahrenheit() ? 2 : 0);
} }
// create the HA MQTT config // create the HA MQTT config
@@ -463,11 +463,7 @@ void DallasSensor::publish_values(const bool force) {
snprintf(stat_t, sizeof(stat_t), "%s/dallassensor_data", Mqtt::base().c_str()); snprintf(stat_t, sizeof(stat_t), "%s/dallassensor_data", Mqtt::base().c_str());
config["stat_t"] = stat_t; config["stat_t"] = stat_t;
if (EMSESP::system_.fahrenheit()) { config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
config["unit_of_meas"] = FJSON("°F");
} else {
config["unit_of_meas"] = FJSON("°C");
}
char str[50]; char str[50];
snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id_str().c_str()); snprintf(str, sizeof(str), "{{value_json['%s'].temp}}", sensor.id_str().c_str());

View File

@@ -52,6 +52,9 @@ const std::string EMSdevice::tag_to_mqtt(uint8_t tag) {
} }
const std::string EMSdevice::uom_to_string(uint8_t uom) { const std::string EMSdevice::uom_to_string(uint8_t uom) {
if (EMSESP::system_.fahrenheit() && (uom == DeviceValueUOM::DEGREES || uom == DeviceValueUOM::DEGREES_R)) {
return read_flash_string(DeviceValue::DeviceValueUOM_s[DeviceValueUOM::FAHRENHEIT]);
}
return read_flash_string(DeviceValue::DeviceValueUOM_s[uom]); return read_flash_string(DeviceValue::DeviceValueUOM_s[uom]);
} }
@@ -306,15 +309,11 @@ void EMSdevice::list_device_entries(JsonObject & output) {
// add uom // add uom
if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") { if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") {
if (EMSESP::system_.fahrenheit() && (dv.uom == DeviceValueUOM::DEGREES || dv.uom == DeviceValueUOM::DEGREES_R)) {
details.add(EMSdevice::uom_to_string(DeviceValueUOM::FAHRENHEIT));
} else {
details.add(EMSdevice::uom_to_string(dv.uom)); details.add(EMSdevice::uom_to_string(dv.uom));
} }
} }
} }
} }
}
// list all the telegram type IDs for this device // list all the telegram type IDs for this device
void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) { void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) {
@@ -1024,7 +1023,7 @@ bool EMSdevice::get_value_info(JsonObject & output, const char * cmd, const int8
// add uom if it's not a " " (single space) // add uom if it's not a " " (single space)
if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") { if (!uom_to_string(dv.uom).empty() && uom_to_string(dv.uom) != " ") {
json["uom"] = fahrenheit ? "°F" : uom_to_string(dv.uom); json["uom"] = uom_to_string(dv.uom);
} }
json["writeable"] = dv.has_cmd; json["writeable"] = dv.has_cmd;