Webui small fixes (#3713)

This commit is contained in:
anatoly-savchenkov 2022-08-15 01:28:29 +03:00 committed by Jesse Hills
parent a67d58948d
commit 0d0733dd94
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
5 changed files with 604 additions and 589 deletions

View file

@ -1,19 +1,10 @@
#include "climate_traits.h" #include "climate_traits.h"
#include <cstdio>
namespace esphome { namespace esphome {
namespace climate { namespace climate {
int8_t ClimateTraits::get_temperature_accuracy_decimals() const { int8_t ClimateTraits::get_temperature_accuracy_decimals() const {
// use printf %g to find number of digits based on temperature step return step_to_accuracy_decimals(this->visual_temperature_step_);
char buf[32];
sprintf(buf, "%.5g", this->visual_temperature_step_);
std::string str{buf};
size_t dot_pos = str.find('.');
if (dot_pos == std::string::npos)
return 0;
return str.length() - dot_pos - 1;
} }
} // namespace climate } // namespace climate

File diff suppressed because it is too large Load diff

View file

@ -360,9 +360,14 @@ void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlM
} }
std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config) { std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config) {
return json::build_json([obj, value, start_config](JsonObject root) { return json::build_json([obj, value, start_config](JsonObject root) {
std::string state = value_accuracy_to_string(value, obj->get_accuracy_decimals()); std::string state;
if (!obj->get_unit_of_measurement().empty()) if (isnan(value)) {
state += " " + obj->get_unit_of_measurement(); state = "NA";
} else {
state = value_accuracy_to_string(value, obj->get_accuracy_decimals());
if (!obj->get_unit_of_measurement().empty())
state += " " + obj->get_unit_of_measurement();
}
set_json_icon_state_value(root, obj, "sensor-" + obj->get_object_id(), state, value, start_config); set_json_icon_state_value(root, obj, "sensor-" + obj->get_object_id(), state, value, start_config);
}); });
} }
@ -719,12 +724,15 @@ std::string WebServer::number_json(number::Number *obj, float value, JsonDetail
root["step"] = obj->traits.get_step(); root["step"] = obj->traits.get_step();
root["mode"] = (int) obj->traits.get_mode(); root["mode"] = (int) obj->traits.get_mode();
} }
std::string state = str_sprintf("%f", value);
root["state"] = state;
if (isnan(value)) { if (isnan(value)) {
root["value"] = "\"NaN\""; root["value"] = "\"NaN\"";
root["state"] = "NA";
} else { } else {
root["value"] = value; root["value"] = value;
std::string state = value_accuracy_to_string(value, step_to_accuracy_decimals(obj->traits.get_step()));
if (!obj->traits.get_unit_of_measurement().empty())
state += " " + obj->traits.get_unit_of_measurement();
root["state"] = state;
} }
}); });
} }

View file

@ -258,6 +258,19 @@ std::string value_accuracy_to_string(float value, int8_t accuracy_decimals) {
return std::string(tmp); return std::string(tmp);
} }
int8_t step_to_accuracy_decimals(float step) {
// use printf %g to find number of digits based on temperature step
char buf[32];
sprintf(buf, "%.5g", step);
std::string str{buf};
size_t dot_pos = str.find('.');
if (dot_pos == std::string::npos)
return 0;
return str.length() - dot_pos - 1;
}
// Colors // Colors
float gamma_correct(float value, float gamma) { float gamma_correct(float value, float gamma) {

View file

@ -415,6 +415,9 @@ ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const ch
/// Create a string from a value and an accuracy in decimals. /// Create a string from a value and an accuracy in decimals.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals); std::string value_accuracy_to_string(float value, int8_t accuracy_decimals);
/// Derive accuracy in decimals from an increment step.
int8_t step_to_accuracy_decimals(float step);
///@} ///@}
/// @name Colors /// @name Colors