#pragma once #include "esphome/core/component.h" #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" #include "esphome/components/text_sensor/filter.h" #include #include namespace esphome { namespace text_sensor { 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: \ text_sensor::TextSensor *name##_text_sensor_{nullptr}; \ \ public: \ void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; } class TextSensor : public EntityBase, public EntityBase_DeviceClass { public: TextSensor() = default; /// Getter-syntax for .state. std::string get_state() const; /// Getter-syntax for .raw_state std::string get_raw_state() const; void publish_state(const std::string &state); /// Add a filter to the filter chain. Will be appended to the back. void add_filter(Filter *filter); /// Add a list of vectors to the back of the filter chain. void add_filters(const std::vector &filters); /// Clear the filters and replace them by filters. void set_filters(const std::vector &filters); /// Clear the entire filter chain. void clear_filters(); void add_on_state_callback(std::function callback); /// Add a callback that will be called every time the sensor sends a raw value. void add_on_raw_state_callback(std::function callback); std::string state; std::string raw_state; // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) void internal_send_state_to_frontend(const std::string &state); protected: std::unique_ptr> raw_callback_; ///< Storage for raw state callbacks (lazy allocated). CallbackManager callback_; ///< Storage for filtered state callbacks. Filter *filter_list_{nullptr}; ///< Store all active filters. }; } // namespace text_sensor } // namespace esphome