ESPHome  2022.6.2
text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 namespace text_sensor {
10 
11 #define LOG_TEXT_SENSOR(prefix, type, obj) \
12  if ((obj) != nullptr) { \
13  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
14  if (!(obj)->get_icon().empty()) { \
15  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
16  } \
17  if (!(obj)->unique_id().empty()) { \
18  ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
19  } \
20  }
21 
22 class TextSensor : public EntityBase {
23  public:
24  explicit TextSensor();
25  explicit TextSensor(const std::string &name);
26 
28  std::string get_state() const;
30  std::string get_raw_state() const;
31 
32  void publish_state(const std::string &state);
33 
35  void add_filter(Filter *filter);
36 
38  void add_filters(const std::vector<Filter *> &filters);
39 
41  void set_filters(const std::vector<Filter *> &filters);
42 
44  void clear_filters();
45 
46  void add_on_state_callback(std::function<void(std::string)> callback);
48  void add_on_raw_state_callback(std::function<void(std::string)> callback);
49 
50  std::string state;
51  std::string raw_state;
52 
53  // ========== INTERNAL METHODS ==========
54  // (In most use cases you won't need these)
55  virtual std::string unique_id();
56 
57  bool has_state();
58 
59  void internal_send_state_to_frontend(const std::string &state);
60 
61  protected:
64 
65  Filter *filter_list_{nullptr};
66 
67  bool has_state_{false};
68 };
69 
70 } // namespace text_sensor
71 } // namespace esphome
const char * name
Definition: stm32flash.h:78
void add_on_state_callback(std::function< void(std::string)> callback)
Definition: text_sensor.cpp:55
virtual std::string unique_id()
Definition: text_sensor.cpp:71
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: text_sensor.cpp:39
void publish_state(const std::string &state)
Definition: text_sensor.cpp:12
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:58
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: text_sensor.cpp:25
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition: text_sensor.h:63
void internal_send_state_to_frontend(const std::string &state)
Definition: text_sensor.cpp:64
std::string get_state() const
Getter-syntax for .state.
Definition: text_sensor.cpp:62
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: text_sensor.cpp:44
Filter * filter_list_
Store all active filters.
Definition: text_sensor.h:65
Definition: a4988.cpp:4
void clear_filters()
Clear the entire filter chain.
Definition: text_sensor.cpp:48
Apply a filter to text sensor values such as to_upper.
Definition: filter.h:19
std::string get_raw_state() const
Getter-syntax for .raw_state.
Definition: text_sensor.cpp:63
CallbackManager< void(std::string)> raw_callback_
Storage for raw state callbacks.
Definition: text_sensor.h:62