[text_sensor] Guard raw_callback_ behind USE_TEXT_SENSOR_FILTER

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.
This commit is contained in:
J. Nick Koston
2026-03-22 17:48:12 -10:00
parent fbe3e7d99c
commit efcb660691
2 changed files with 10 additions and 1 deletions
@@ -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
+8 -1
View File
@@ -64,8 +64,13 @@ class TextSensor : public EntityBase {
template<typename F> void add_on_state_callback(F &&callback) { this->callback_.add(std::forward<F>(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<typename F> void add_on_raw_state_callback(F &&callback) {
#ifdef USE_TEXT_SENSOR_FILTER
this->raw_callback_.add(std::forward<F>(callback));
#else
this->callback_.add(std::forward<F>(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<void(const std::string &)> raw_callback_; ///< Storage for raw state callbacks.
LazyCallbackManager<void(const std::string &)> callback_; ///< Storage for filtered state callbacks.
#endif
LazyCallbackManager<void(const std::string &)> callback_; ///< Storage for filtered state callbacks.
#ifdef USE_TEXT_SENSOR_FILTER
Filter *filter_list_{nullptr}; ///< Store all active filters.