From efcb660691bb875af561548f1da44b61b539a908 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Mar 2026 17:48:12 -1000 Subject: [PATCH 1/2] [text_sensor] Guard raw_callback_ behind USE_TEXT_SENSOR_FILTER MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same pattern as sensor: raw_callback_ is only needed when filters are configured. add_on_raw_state_callback remains always available — without filters it delegates to callback_ since raw == filtered. Saves 4 bytes per TextSensor instance on builds without filters. --- esphome/components/text_sensor/text_sensor.cpp | 2 ++ esphome/components/text_sensor/text_sensor.h | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/esphome/components/text_sensor/text_sensor.cpp b/esphome/components/text_sensor/text_sensor.cpp index aa49a85d26..0dc29f9a94 100644 --- a/esphome/components/text_sensor/text_sensor.cpp +++ b/esphome/components/text_sensor/text_sensor.cpp @@ -31,7 +31,9 @@ void TextSensor::publish_state(const char *state, size_t len) { if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) { this->state.assign(state, len); } +#ifdef USE_TEXT_SENSOR_FILTER this->raw_callback_.call(this->state); +#endif ESP_LOGV(TAG, "'%s': Received new state %s", this->name_.c_str(), this->state.c_str()); this->notify_frontend_(); #ifdef USE_TEXT_SENSOR_FILTER diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index 8941790e7c..be3d94028d 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -64,8 +64,13 @@ class TextSensor : public EntityBase { template void add_on_state_callback(F &&callback) { this->callback_.add(std::forward(callback)); } /// Add a callback that will be called every time the sensor sends a raw value. + /// Without filters, raw state equals filtered state so this delegates to the regular callback. template void add_on_raw_state_callback(F &&callback) { +#ifdef USE_TEXT_SENSOR_FILTER this->raw_callback_.add(std::forward(callback)); +#else + this->callback_.add(std::forward(callback)); +#endif } // ========== INTERNAL METHODS ========== @@ -77,8 +82,10 @@ class TextSensor : public EntityBase { protected: /// Notify frontend that state has changed (assumes this->state is already set) void notify_frontend_(); +#ifdef USE_TEXT_SENSOR_FILTER LazyCallbackManager raw_callback_; ///< Storage for raw state callbacks. - LazyCallbackManager callback_; ///< Storage for filtered state callbacks. +#endif + LazyCallbackManager callback_; ///< Storage for filtered state callbacks. #ifdef USE_TEXT_SENSOR_FILTER Filter *filter_list_{nullptr}; ///< Store all active filters. From 936764b89a63d1c7a59d17f908caab1a8b2680ef Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Mar 2026 18:33:51 -1000 Subject: [PATCH 2/2] [text_sensor] Clarify compile-time vs runtime in raw_callback comment --- esphome/components/text_sensor/text_sensor.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index be3d94028d..3f69e91c8d 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -64,7 +64,8 @@ class TextSensor : public EntityBase { template void add_on_state_callback(F &&callback) { this->callback_.add(std::forward(callback)); } /// Add a callback that will be called every time the sensor sends a raw value. - /// Without filters, raw state equals filtered state so this delegates to the regular callback. + /// When USE_TEXT_SENSOR_FILTER is not enabled, delegates to the regular callback + /// since raw state equals filtered state without filter support compiled in. template void add_on_raw_state_callback(F &&callback) { #ifdef USE_TEXT_SENSOR_FILTER this->raw_callback_.add(std::forward(callback));