1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-02 19:32:19 +01:00

[sensor] Convert LOG_SENSOR macro to function to reduce flash usage

This commit is contained in:
J. Nick Koston
2025-08-18 18:00:46 -05:00
parent 44bd8e5b54
commit e7fadef15c
2 changed files with 32 additions and 20 deletions

View File

@@ -6,6 +6,33 @@ namespace sensor {
static const char *const TAG = "sensor";
// Function implementation of LOG_SENSOR macro to reduce code size
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj) {
if (obj == nullptr) {
return;
}
ESP_LOGCONFIG(tag,
"%s%s '%s'\n"
"%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()).c_str(),
prefix, obj->get_unit_of_measurement().c_str(), prefix, obj->get_accuracy_decimals());
if (!obj->get_device_class().empty()) {
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class().c_str());
}
if (!obj->get_icon().empty()) {
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon().c_str());
}
if (obj->get_force_update()) {
ESP_LOGV(tag, "%s Force Update: YES", prefix);
}
}
std::string state_class_to_string(StateClass state_class) {
switch (state_class) {
case STATE_CLASS_MEASUREMENT:

View File

@@ -12,26 +12,11 @@
namespace esphome {
namespace sensor {
#define LOG_SENSOR(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, \
"%s%s '%s'\n" \
"%s State Class: '%s'\n" \
"%s Unit of Measurement: '%s'\n" \
"%s Accuracy Decimals: %d", \
prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str(), prefix, \
state_class_to_string((obj)->get_state_class()).c_str(), prefix, \
(obj)->get_unit_of_measurement().c_str(), prefix, (obj)->get_accuracy_decimals()); \
if (!(obj)->get_device_class().empty()) { \
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
} \
if (!(obj)->get_icon().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
} \
if ((obj)->get_force_update()) { \
ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
} \
}
// Forward declaration
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
// Macro that calls the function - kept for backward compatibility
#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
#define SUB_SENSOR(name) \
protected: \