ESPHome 2025.5.0
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 this->raw_callback_.call(state);
42
43 ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
44
45 if (this->filter_list_ == nullptr) {
47 } else {
48 this->filter_list_->input(state);
49 }
50}
51
52void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
53void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
54 this->raw_callback_.add(std::move(callback));
55}
56
58 // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
59 // filters
60 ESP_LOGVV(TAG, "Sensor(%p)::add_filter(%p)", this, filter);
61 if (this->filter_list_ == nullptr) {
62 this->filter_list_ = filter;
63 } else {
64 Filter *last_filter = this->filter_list_;
65 while (last_filter->next_ != nullptr)
66 last_filter = last_filter->next_;
67 last_filter->initialize(this, filter);
68 }
69 filter->initialize(this, nullptr);
70}
71void Sensor::add_filters(const std::vector<Filter *> &filters) {
72 for (Filter *filter : filters) {
73 this->add_filter(filter);
74 }
75}
76void Sensor::set_filters(const std::vector<Filter *> &filters) {
77 this->clear_filters();
78 this->add_filters(filters);
79}
81 if (this->filter_list_ != nullptr) {
82 ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this);
83 }
84 this->filter_list_ = nullptr;
85}
86float Sensor::get_state() const { return this->state; }
87float Sensor::get_raw_state() const { return this->raw_state; }
88std::string Sensor::unique_id() { return ""; }
89
91 this->has_state_ = true;
92 this->state = state;
93 ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", this->get_name().c_str(), state,
94 this->get_unit_of_measurement().c_str(), this->get_accuracy_decimals());
95 this->callback_.call(state);
96}
97bool Sensor::has_state() const { return this->has_state_; }
98
99} // namespace sensor
100} // namespace esphome
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
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:57
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:90
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition sensor.h:156
float get_raw_state() const
Getter-syntax for .raw_state.
Definition sensor.cpp:87
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition sensor.h:152
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
Definition sensor.cpp:71
float get_state() const
Getter-syntax for .state.
Definition sensor.cpp:86
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:76
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition sensor.h:151
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:52
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet.
Definition sensor.cpp:97
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
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:154
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:137
optional< StateClass > state_class_
State class override.
Definition sensor.h:157
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition sensor.cpp:88
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:80
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:53
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:44
@ STATE_CLASS_TOTAL
Definition sensor.h:48
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:47
@ STATE_CLASS_MEASUREMENT
Definition sensor.h:46
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7