ESPHome 2025.6.3
Loading...
Searching...
No Matches
sensor.cpp
Go to the documentation of this file.
1#include "sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sensor {
6
7static const char *const TAG = "sensor";
8
9std::string state_class_to_string(StateClass state_class) {
10 switch (state_class) {
12 return "measurement";
14 return "total_increasing";
16 return "total";
18 default:
19 return "";
20 }
21}
22
23Sensor::Sensor() : state(NAN), raw_state(NAN) {}
24
26 if (this->accuracy_decimals_.has_value())
27 return *this->accuracy_decimals_;
28 return 0;
29}
30void Sensor::set_accuracy_decimals(int8_t accuracy_decimals) { this->accuracy_decimals_ = accuracy_decimals; }
31
32void Sensor::set_state_class(StateClass state_class) { this->state_class_ = state_class; }
38
40 this->raw_state = state;
41 if (this->raw_callback_) {
42 this->raw_callback_->call(state);
43 }
44
45 ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
46
47 if (this->filter_list_ == nullptr) {
49 } else {
50 this->filter_list_->input(state);
51 }
52}
53
54void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
55void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
56 if (!this->raw_callback_) {
57 this->raw_callback_ = make_unique<CallbackManager<void(float)>>();
58 }
59 this->raw_callback_->add(std::move(callback));
60}
61
63 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
64 // filters
65 ESP_LOGVV(TAG, "Sensor(%p)::add_filter(%p)", this, filter);
66 if (this->filter_list_ == nullptr) {
67 this->filter_list_ = filter;
68 } else {
69 Filter *last_filter = this->filter_list_;
70 while (last_filter->next_ != nullptr)
71 last_filter = last_filter->next_;
72 last_filter->initialize(this, filter);
73 }
74 filter->initialize(this, nullptr);
75}
76void Sensor::add_filters(const std::vector<Filter *> &filters) {
77 for (Filter *filter : filters) {
78 this->add_filter(filter);
79 }
80}
81void Sensor::set_filters(const std::vector<Filter *> &filters) {
82 this->clear_filters();
83 this->add_filters(filters);
84}
86 if (this->filter_list_ != nullptr) {
87 ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this);
88 }
89 this->filter_list_ = nullptr;
90}
91float Sensor::get_state() const { return this->state; }
92float Sensor::get_raw_state() const { return this->raw_state; }
93std::string Sensor::unique_id() { return ""; }
94
96 this->set_has_state(true);
97 this->state = state;
98 ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", this->get_name().c_str(), state,
99 this->get_unit_of_measurement().c_str(), this->get_accuracy_decimals());
100 this->callback_.call(state);
101}
102
103} // namespace sensor
104} // namespace esphome
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const StringRef & get_name() const
void set_has_state(bool state)
Definition entity_base.h:56
constexpr const char * c_str() const
Definition string_ref.h:69
bool has_value() const
Definition optional.h:87
Apply a filter to sensor values such as moving average.
Definition filter.h:20
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition filter.cpp:28
void input(float value)
Definition filter.cpp:13
void set_state_class(StateClass state_class)
Manually set the state class.
Definition sensor.cpp:32
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition sensor.cpp:62
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void internal_send_state_to_frontend(float state)
Definition sensor.cpp:95
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition sensor.h:158
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:92
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:154
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:76
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:91
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition sensor.cpp:30
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
Definition sensor.cpp:81
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.cpp:54
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:136
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:33
Filter * filter_list_
Store all active filters.
Definition sensor.h:156
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:142
optional< StateClass > state_class_
State class override.
Definition sensor.h:159
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition sensor.cpp:93
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:25
void clear_filters()
Clear the entire filter chain.
Definition sensor.cpp:85
std::unique_ptr< CallbackManager< void(float)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
Definition sensor.h:153
void add_on_raw_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time the sensor sends a raw value.
Definition sensor.cpp:55
bool state
Definition fan.h:0
std::string state_class_to_string(StateClass state_class)
Definition sensor.cpp:9
StateClass
Sensor state classes.
Definition sensor.h:49
@ STATE_CLASS_TOTAL
Definition sensor.h:53
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:52
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:51
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7