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