mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 13:13:50 +00:00
[sensor] ESP8266: Use LogString for state_class_to_string() to save RAM (#10617)
This commit is contained in:
@@ -58,8 +58,13 @@ void MQTTSensorComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon
|
||||
if (this->sensor_->get_force_update())
|
||||
root[MQTT_FORCE_UPDATE] = true;
|
||||
|
||||
if (this->sensor_->get_state_class() != STATE_CLASS_NONE)
|
||||
root[MQTT_STATE_CLASS] = state_class_to_string(this->sensor_->get_state_class());
|
||||
if (this->sensor_->get_state_class() != STATE_CLASS_NONE) {
|
||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
|
||||
root[MQTT_STATE_CLASS] = (const __FlashStringHelper *) state_class_to_string(this->sensor_->get_state_class());
|
||||
#else
|
||||
root[MQTT_STATE_CLASS] = LOG_STR_ARG(state_class_to_string(this->sensor_->get_state_class()));
|
||||
#endif
|
||||
}
|
||||
|
||||
config.command_topic = false;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *o
|
||||
"%s State Class: '%s'\n"
|
||||
"%s Unit of Measurement: '%s'\n"
|
||||
"%s Accuracy Decimals: %d",
|
||||
prefix, type, obj->get_name().c_str(), prefix, state_class_to_string(obj->get_state_class()), prefix,
|
||||
prefix, type, obj->get_name().c_str(), prefix,
|
||||
LOG_STR_ARG(state_class_to_string(obj->get_state_class())), prefix,
|
||||
obj->get_unit_of_measurement_ref().c_str(), prefix, obj->get_accuracy_decimals());
|
||||
|
||||
if (!obj->get_device_class_ref().empty()) {
|
||||
@@ -33,17 +34,17 @@ void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *o
|
||||
}
|
||||
}
|
||||
|
||||
const char *state_class_to_string(StateClass state_class) {
|
||||
const LogString *state_class_to_string(StateClass state_class) {
|
||||
switch (state_class) {
|
||||
case STATE_CLASS_MEASUREMENT:
|
||||
return "measurement";
|
||||
return LOG_STR("measurement");
|
||||
case STATE_CLASS_TOTAL_INCREASING:
|
||||
return "total_increasing";
|
||||
return LOG_STR("total_increasing");
|
||||
case STATE_CLASS_TOTAL:
|
||||
return "total";
|
||||
return LOG_STR("total");
|
||||
case STATE_CLASS_NONE:
|
||||
default:
|
||||
return "";
|
||||
return LOG_STR("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ enum StateClass : uint8_t {
|
||||
STATE_CLASS_TOTAL = 3,
|
||||
};
|
||||
|
||||
const char *state_class_to_string(StateClass state_class);
|
||||
const LogString *state_class_to_string(StateClass state_class);
|
||||
|
||||
/** Base-class for all sensors.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user