ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
text_sensor.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <vector>
9#include <memory>
10
11namespace esphome {
12namespace text_sensor {
13
14void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj);
15
16#define LOG_TEXT_SENSOR(prefix, type, obj) log_text_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
17
18#define SUB_TEXT_SENSOR(name) \
19 protected: \
20 text_sensor::TextSensor *name##_text_sensor_{nullptr}; \
21\
22 public: \
23 void set_##name##_text_sensor(text_sensor::TextSensor *text_sensor) { this->name##_text_sensor_ = text_sensor; }
24
26 public:
27 TextSensor() = default;
28
30 std::string get_state() const;
32 std::string get_raw_state() const;
33
34 void publish_state(const std::string &state);
35
37 void add_filter(Filter *filter);
38
40 void add_filters(const std::vector<Filter *> &filters);
41
43 void set_filters(const std::vector<Filter *> &filters);
44
46 void clear_filters();
47
48 void add_on_state_callback(std::function<void(std::string)> callback);
50 void add_on_raw_state_callback(std::function<void(std::string)> callback);
51
52 std::string state;
53 std::string raw_state;
54
55 // ========== INTERNAL METHODS ==========
56 // (In most use cases you won't need these)
57
58 void internal_send_state_to_frontend(const std::string &state);
59
60 protected:
61 std::unique_ptr<CallbackManager<void(std::string)>>
63 CallbackManager<void(std::string)> callback_;
64
65 Filter *filter_list_{nullptr};
66};
67
68} // namespace text_sensor
69} // namespace esphome
Apply a filter to text sensor values such as to_upper.
Definition filter.h:20
void internal_send_state_to_frontend(const std::string &state)
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
CallbackManager< void(std::string)> callback_
Storage for filtered state callbacks.
Definition text_sensor.h:63
std::unique_ptr< CallbackManager< void(std::string)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition text_sensor.h:62
void clear_filters()
Clear the entire filter chain.
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
std::string get_state() const
Getter-syntax for .state.
Filter * filter_list_
Store all active filters.
Definition text_sensor.h:65
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.
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
void add_on_state_callback(std::function< void(std::string)> callback)
std::string get_raw_state() const
Getter-syntax for .raw_state.
void publish_state(const std::string &state)
uint16_t type
void log_text_sensor(const char *tag, const char *prefix, const char *type, TextSensor *obj)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7