ESPHome  2023.3.0
sensor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/log.h"
6 #include "esphome/core/helpers.h"
8 
9 #include <vector>
10 
11 namespace esphome {
12 namespace sensor {
13 
14 #define LOG_SENSOR(prefix, type, obj) \
15  if ((obj) != nullptr) { \
16  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17  if (!(obj)->get_device_class().empty()) { \
18  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
19  } \
20  ESP_LOGCONFIG(TAG, "%s State Class: '%s'", prefix, state_class_to_string((obj)->get_state_class()).c_str()); \
21  ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, (obj)->get_unit_of_measurement().c_str()); \
22  ESP_LOGCONFIG(TAG, "%s Accuracy Decimals: %d", prefix, (obj)->get_accuracy_decimals()); \
23  if (!(obj)->get_icon().empty()) { \
24  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
25  } \
26  if (!(obj)->unique_id().empty()) { \
27  ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
28  } \
29  if ((obj)->get_force_update()) { \
30  ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
31  } \
32  }
33 
34 #define SUB_SENSOR(name) \
35  protected: \
36  sensor::Sensor *name##_sensor_{nullptr}; \
37 \
38  public: \
39  void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
40 
44 enum StateClass : uint8_t {
49 };
50 
51 std::string state_class_to_string(StateClass state_class);
52 
57 class Sensor : public EntityBase {
58  public:
59  explicit Sensor();
60  explicit Sensor(const std::string &name);
61 
63  std::string get_unit_of_measurement();
65  void set_unit_of_measurement(const std::string &unit_of_measurement);
66 
68  int8_t get_accuracy_decimals();
71 
73  std::string get_device_class();
75  void set_device_class(const std::string &device_class);
76 
81 
89  bool get_force_update() const { return force_update_; }
91  void set_force_update(bool force_update) { force_update_ = force_update; }
92 
94  void add_filter(Filter *filter);
95 
106  void add_filters(const std::vector<Filter *> &filters);
107 
109  void set_filters(const std::vector<Filter *> &filters);
110 
112  void clear_filters();
113 
115  float get_state() const;
117  float get_raw_state() const;
118 
126  void publish_state(float state);
127 
128  // ========== INTERNAL METHODS ==========
129  // (In most use cases you won't need these)
131  void add_on_state_callback(std::function<void(float)> &&callback);
133  void add_on_raw_state_callback(std::function<void(float)> &&callback);
134 
142  float state;
143 
148  float raw_state;
149 
151  bool has_state() const;
152 
158  virtual std::string unique_id();
159 
160  void internal_send_state_to_frontend(float state);
161 
162  protected:
167  virtual std::string unit_of_measurement(); // NOLINT
168 
173  virtual int8_t accuracy_decimals(); // NOLINT
174 
179  virtual std::string device_class(); // NOLINT
180 
185  virtual StateClass state_class(); // NOLINT
186 
189 
190  bool has_state_{false};
191  Filter *filter_list_{nullptr};
192 
197  bool force_update_{false};
198 };
199 
200 } // namespace sensor
201 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:89
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:195
virtual std::string unit_of_measurement()
Override this to set the default unit of measurement.
Definition: sensor.cpp:37
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:187
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:194
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
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied...
Definition: sensor.h:148
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: sensor.cpp:90
void set_force_update(bool force_update)
Set force update mode.
Definition: sensor.h:91
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:142
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:193
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:72
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition: sensor.h:188
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:191
bool force_update_
Force update mode.
Definition: sensor.h:197
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:19
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:196
Base-class for all sensors.
Definition: sensor.h:57
virtual int8_t accuracy_decimals()
Override this to set the default accuracy in decimals.
Definition: sensor.cpp:48