ESPHome  2023.9.1
text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 
8 #include <vector>
9 
10 namespace esphome {
11 namespace text_sensor {
12 
13 #define LOG_TEXT_SENSOR(prefix, type, obj) \
14  if ((obj) != nullptr) { \
15  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
16  if (!(obj)->get_icon().empty()) { \
17  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
18  } \
19  if (!(obj)->unique_id().empty()) { \
20  ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
21  } \
22  }
23 
24 #define SUB_TEXT_SENSOR(name) \
25  protected: \
26  text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
27 \
28  public: \
29  void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
30 
31 class TextSensor : public EntityBase {
32  public:
34  std::string get_state() const;
36  std::string get_raw_state() const;
37 
38  void publish_state(const std::string &state);
39 
41  void add_filter(Filter *filter);
42 
44  void add_filters(const std::vector<Filter *> &filters);
45 
47  void set_filters(const std::vector<Filter *> &filters);
48 
50  void clear_filters();
51 
52  void add_on_state_callback(std::function<void(std::string)> callback);
54  void add_on_raw_state_callback(std::function<void(std::string)> callback);
55 
56  std::string state;
57  std::string raw_state;
58 
59  // ========== INTERNAL METHODS ==========
60  // (In most use cases you won't need these)
65  virtual std::string unique_id();
66 
67  bool has_state();
68 
69  void internal_send_state_to_frontend(const std::string &state);
70 
71  protected:
74 
75  Filter *filter_list_{nullptr};
76 
77  bool has_state_{false};
78 };
79 
80 } // namespace text_sensor
81 } // namespace esphome
void add_on_state_callback(std::function< void(std::string)> callback)
Definition: text_sensor.cpp:52
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: text_sensor.cpp:68
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: text_sensor.cpp:36
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
void add_on_raw_state_callback(std::function< void(std::string)> callback)
Add a callback that will be called every time the sensor sends a raw value.
Definition: text_sensor.cpp:55
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: text_sensor.cpp:22
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition: text_sensor.h:73
void internal_send_state_to_frontend(const std::string &state)
Definition: text_sensor.cpp:61
std::string get_state() const
Getter-syntax for .state.
Definition: text_sensor.cpp:59
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: text_sensor.cpp:41
Filter * filter_list_
Store all active filters.
Definition: text_sensor.h:75
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void clear_filters()
Clear the entire filter chain.
Definition: text_sensor.cpp:45
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:20
std::string get_raw_state() const
Getter-syntax for .raw_state.
Definition: text_sensor.cpp:60
CallbackManager< void(std::string)> raw_callback_
Storage for raw state callbacks.
Definition: text_sensor.h:72