ESPHome  2024.12.2
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 
58  public:
59  explicit Sensor();
60 
62  int8_t get_accuracy_decimals();
64  void set_accuracy_decimals(int8_t accuracy_decimals);
65 
69  void set_state_class(StateClass state_class);
70 
78  bool get_force_update() const { return force_update_; }
80  void set_force_update(bool force_update) { force_update_ = force_update; }
81 
83  void add_filter(Filter *filter);
84 
95  void add_filters(const std::vector<Filter *> &filters);
96 
98  void set_filters(const std::vector<Filter *> &filters);
99 
101  void clear_filters();
102 
104  float get_state() const;
106  float get_raw_state() const;
107 
115  void publish_state(float state);
116 
117  // ========== INTERNAL METHODS ==========
118  // (In most use cases you won't need these)
120  void add_on_state_callback(std::function<void(float)> &&callback);
122  void add_on_raw_state_callback(std::function<void(float)> &&callback);
123 
131  float state;
132 
137  float raw_state;
138 
140  bool has_state() const;
141 
146  virtual std::string unique_id();
147 
148  void internal_send_state_to_frontend(float state);
149 
150  protected:
153 
154  Filter *filter_list_{nullptr};
155 
158  bool force_update_{false};
159  bool has_state_{false};
160 };
161 
162 } // namespace sensor
163 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.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:52
void clear_filters()
Clear the entire filter chain.
Definition: sensor.cpp:80
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
void set_force_update(bool force_update)
Set force update mode.
Definition: sensor.h:80
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
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
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
bool force_update_
Force update mode.
Definition: sensor.h:158
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:20
Implementation of SPI Controller mode.
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
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38