ESPHome  2022.11.4
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 namespace esphome {
10 namespace sensor {
11 
12 #define LOG_SENSOR(prefix, type, obj) \
13  if ((obj) != nullptr) { \
14  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
15  if (!(obj)->get_device_class().empty()) { \
16  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
17  } \
18  ESP_LOGCONFIG(TAG, "%s State Class: '%s'", prefix, state_class_to_string((obj)->get_state_class()).c_str()); \
19  ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, (obj)->get_unit_of_measurement().c_str()); \
20  ESP_LOGCONFIG(TAG, "%s Accuracy Decimals: %d", prefix, (obj)->get_accuracy_decimals()); \
21  if (!(obj)->get_icon().empty()) { \
22  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
23  } \
24  if (!(obj)->unique_id().empty()) { \
25  ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
26  } \
27  if ((obj)->get_force_update()) { \
28  ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
29  } \
30  }
31 
35 enum StateClass : uint8_t {
40 };
41 
42 std::string state_class_to_string(StateClass state_class);
43 
48 class Sensor : public EntityBase {
49  public:
50  explicit Sensor();
51  explicit Sensor(const std::string &name);
52 
54  std::string get_unit_of_measurement();
56  void set_unit_of_measurement(const std::string &unit_of_measurement);
57 
59  int8_t get_accuracy_decimals();
62 
64  std::string get_device_class();
66  void set_device_class(const std::string &device_class);
67 
72 
80  bool get_force_update() const { return force_update_; }
82  void set_force_update(bool force_update) { force_update_ = force_update; }
83 
85  void add_filter(Filter *filter);
86 
97  void add_filters(const std::vector<Filter *> &filters);
98 
100  void set_filters(const std::vector<Filter *> &filters);
101 
103  void clear_filters();
104 
106  float get_state() const;
108  float get_raw_state() const;
109 
117  void publish_state(float state);
118 
119  // ========== INTERNAL METHODS ==========
120  // (In most use cases you won't need these)
122  void add_on_state_callback(std::function<void(float)> &&callback);
124  void add_on_raw_state_callback(std::function<void(float)> &&callback);
125 
133  float state;
134 
139  float raw_state;
140 
142  bool has_state() const;
143 
149  virtual std::string unique_id();
150 
151  void internal_send_state_to_frontend(float state);
152 
153  protected:
158  virtual std::string unit_of_measurement(); // NOLINT
159 
164  virtual int8_t accuracy_decimals(); // NOLINT
165 
170  virtual std::string device_class(); // NOLINT
171 
176  virtual StateClass state_class(); // NOLINT
177 
180 
181  bool has_state_{false};
182  Filter *filter_list_{nullptr};
183 
188  bool force_update_{false};
189 };
190 
191 } // namespace sensor
192 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:80
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
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
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:139
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:82
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
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
bool force_update_
Force update mode.
Definition: sensor.h:188
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