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

[text_sensor] Convert LOG_TEXT_SENSOR macro to function to reduce flash usage

This commit is contained in:
J. Nick Koston
2025-09-25 11:08:45 -05:00
parent 74f09a2b59
commit 56c16e6893
3 changed files with 21 additions and 11 deletions

View File

@@ -6,6 +6,23 @@ namespace text_sensor {
static const char *const TAG = "text_sensor";
// Function implementation of LOG_TEXT_SENSOR macro to reduce code size
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj) {
if (obj == nullptr) {
return;
}
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
if (!obj->get_device_class_ref().empty()) {
ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
}
if (!obj->get_icon_ref().empty()) {
ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
}
}
void TextSensor::publish_state(const std::string &state) {
this->raw_state = state;
if (this->raw_callback_) {

View File

@@ -11,16 +11,9 @@
namespace esphome {
namespace text_sensor {
#define LOG_TEXT_SENSOR(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
if (!(obj)->get_device_class_ref().empty()) { \
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
} \
if (!(obj)->get_icon_ref().empty()) { \
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
} \
}
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj);
#define LOG_TEXT_SENSOR(prefix, type, obj) log_text_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
#define SUB_TEXT_SENSOR(name) \
protected: \