ESPHome  2022.11.3
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(const std::string &name) : EntityBase(name), state(NAN), raw_state(NAN) {}
25 
27  if (this->unit_of_measurement_.has_value())
28  return *this->unit_of_measurement_;
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31  return this->unit_of_measurement();
32 #pragma GCC diagnostic pop
33 }
36 }
37 std::string Sensor::unit_of_measurement() { return ""; }
38 
40  if (this->accuracy_decimals_.has_value())
41  return *this->accuracy_decimals_;
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
44  return this->accuracy_decimals();
45 #pragma GCC diagnostic pop
46 }
48 int8_t Sensor::accuracy_decimals() { return 0; }
49 
50 std::string Sensor::get_device_class() {
51  if (this->device_class_.has_value())
52  return *this->device_class_;
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
55  return this->device_class();
56 #pragma GCC diagnostic pop
57 }
58 void Sensor::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
59 std::string Sensor::device_class() { return ""; }
60 
63  if (this->state_class_.has_value())
64  return *this->state_class_;
65 #pragma GCC diagnostic push
66 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
67  return this->state_class();
68 #pragma GCC diagnostic pop
69 }
71 
73  this->raw_state = state;
74  this->raw_callback_.call(state);
75 
76  ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
77 
78  if (this->filter_list_ == nullptr) {
80  } else {
81  this->filter_list_->input(state);
82  }
83 }
84 
85 void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
86 void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
87  this->raw_callback_.add(std::move(callback));
88 }
89 
90 void Sensor::add_filter(Filter *filter) {
91  // inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
92  // filters
93  ESP_LOGVV(TAG, "Sensor(%p)::add_filter(%p)", this, filter);
94  if (this->filter_list_ == nullptr) {
95  this->filter_list_ = filter;
96  } else {
97  Filter *last_filter = this->filter_list_;
98  while (last_filter->next_ != nullptr)
99  last_filter = last_filter->next_;
100  last_filter->initialize(this, filter);
101  }
102  filter->initialize(this, nullptr);
103 }
104 void Sensor::add_filters(const std::vector<Filter *> &filters) {
105  for (Filter *filter : filters) {
106  this->add_filter(filter);
107  }
108 }
109 void Sensor::set_filters(const std::vector<Filter *> &filters) {
110  this->clear_filters();
111  this->add_filters(filters);
112 }
114  if (this->filter_list_ != nullptr) {
115  ESP_LOGVV(TAG, "Sensor(%p)::clear_filters()", this);
116  }
117  this->filter_list_ = nullptr;
118 }
119 float Sensor::get_state() const { return this->state; }
120 float Sensor::get_raw_state() const { return this->raw_state; }
121 std::string Sensor::unique_id() { return ""; }
122 
124  this->has_state_ = true;
125  this->state = state;
126  ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", this->get_name().c_str(), state,
127  this->get_unit_of_measurement().c_str(), this->get_accuracy_decimals());
128  this->callback_.call(state);
129 }
130 bool Sensor::has_state() const { return this->has_state_; }
131 
132 } // namespace sensor
133 } // namespace esphome
const char * name
Definition: stm32flash.h:78
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:85
void clear_filters()
Clear the entire filter chain.
Definition: sensor.cpp:113
optional< std::string > device_class_
Device class override.
Definition: sensor.h:186
virtual std::string unit_of_measurement()
Override this to set the default unit of measurement.
Definition: sensor.cpp:37
void input(float value)
Definition: filter.cpp:13
virtual std::string device_class()
Override this to set the default device class.
Definition: sensor.cpp:59
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition: sensor.h:178
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: sensor.cpp:109
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition: sensor.h:185
std::string name_
Definition: entity_base.h:54
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:86
const std::string & get_name() const
Definition: entity_base.cpp:11
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied...
Definition: sensor.h:139
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: sensor.cpp:90
bool has_value() const
Definition: optional.h:87
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: sensor.cpp:26
virtual std::string unique_id()
A unique ID for this sensor, empty for no unique id.
Definition: sensor.cpp:121
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition: sensor.cpp:47
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:133
void set_unit_of_measurement(const std::string &unit_of_measurement)
Manually set the unit of measurement.
Definition: sensor.cpp:34
optional< std::string > unit_of_measurement_
Unit of measurement override.
Definition: sensor.h:184
std::string state_class_to_string(StateClass state_class)
Definition: sensor.cpp:9
StateClass
Sensor state classes.
Definition: sensor.h:35
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
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:179
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:62
virtual StateClass state_class()
Override this to set the default state class.
Definition: sensor.cpp:70
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: sensor.cpp:50
float get_raw_state() const
Getter-syntax for .raw_state.
Definition: sensor.cpp:120
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: sensor.cpp:104
Filter * filter_list_
Store all active filters.
Definition: sensor.h:182
float get_state() const
Getter-syntax for .state.
Definition: sensor.cpp:119
void set_device_class(const std::string &device_class)
Manually set the device class.
Definition: sensor.cpp:58
void internal_send_state_to_frontend(float state)
Definition: sensor.cpp:123
Apply a filter to sensor values such as moving average.
Definition: filter.h:18
Definition: a4988.cpp:4
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:130
void set_state_class(StateClass state_class)
Manually set the state class.
Definition: sensor.cpp:61
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:39
optional< StateClass > state_class_
State class override.
Definition: sensor.h:187
Base-class for all sensors.
Definition: sensor.h:48
virtual int8_t accuracy_decimals()
Override this to set the default accuracy in decimals.
Definition: sensor.cpp:48
bool state
Definition: fan.h:34