ESPHome  2024.2.2
sensor.cpp
Go to the documentation of this file.
1 #include "sensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace sensor {
6 
7 static const char *const TAG = "sensor";
8 
9 std::string state_class_to_string(StateClass state_class) {
10  switch (state_class) {
12  return "measurement";
14  return "total_increasing";
15  case STATE_CLASS_TOTAL:
16  return "total";
17  case STATE_CLASS_NONE:
18  default:
19  return "";
20  }
21 }
22 
23 Sensor::Sensor() : state(NAN), raw_state(NAN) {}
24 
26  if (this->accuracy_decimals_.has_value())
27  return *this->accuracy_decimals_;
28  return 0;
29 }
30 void Sensor::set_accuracy_decimals(int8_t accuracy_decimals) { this->accuracy_decimals_ = accuracy_decimals; }
31 
32 void Sensor::set_state_class(StateClass state_class) { this->state_class_ = state_class; }
34  if (this->state_class_.has_value())
35  return *this->state_class_;
37 }
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 
52 void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
53 void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
54  this->raw_callback_.add(std::move(callback));
55 }
56 
57 void Sensor::add_filter(Filter *filter) {
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 }
71 void Sensor::add_filters(const std::vector<Filter *> &filters) {
72  for (Filter *filter : filters) {
73  this->add_filter(filter);
74  }
75 }
76 void 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 }
86 float Sensor::get_state() const { return this->state; }
87 float Sensor::get_raw_state() const { return this->raw_state; }
88 std::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 }
97 bool Sensor::has_state() const { return this->has_state_; }
98 
99 } // namespace sensor
100 } // namespace esphome
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
void clear_filters()
Clear the entire filter chain.
Definition: sensor.cpp:80
void input(float value)
Definition: filter.cpp:13
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition: sensor.h:151
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: sensor.cpp:76
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition: sensor.h:156
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
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied...
Definition: sensor.h:137
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: sensor.cpp:57
bool has_value() const
Definition: optional.h:87
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: sensor.cpp:88
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition: sensor.cpp:30
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
std::string state_class_to_string(StateClass state_class)
Definition: sensor.cpp:9
StateClass
Sensor state classes.
Definition: sensor.h:44
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: entity_base.cpp:87
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition: filter.cpp:28
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition: sensor.h:152
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:33
float get_raw_state() const
Getter-syntax for .raw_state.
Definition: sensor.cpp:87
constexpr const char * c_str() const
Definition: string_ref.h:68
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: sensor.cpp:71
Filter * filter_list_
Store all active filters.
Definition: sensor.h:154
float get_state() const
Getter-syntax for .state.
Definition: sensor.cpp:86
void internal_send_state_to_frontend(float state)
Definition: sensor.cpp:90
Apply a filter to sensor values such as moving average.
Definition: filter.h:19
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
void set_state_class(StateClass state_class)
Manually set the state class.
Definition: sensor.cpp:32
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:25
optional< StateClass > state_class_
State class override.
Definition: sensor.h:157
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34