ESPHome  2022.6.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 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 {
39 };
40 
41 std::string state_class_to_string(StateClass state_class);
42 
47 class Sensor : public EntityBase {
48  public:
49  explicit Sensor();
50  explicit Sensor(const std::string &name);
51 
53  std::string get_unit_of_measurement();
55  void set_unit_of_measurement(const std::string &unit_of_measurement);
56 
58  int8_t get_accuracy_decimals();
61 
63  std::string get_device_class();
65  void set_device_class(const std::string &device_class);
66 
71 
79  bool get_force_update() const { return force_update_; }
81  void set_force_update(bool force_update) { force_update_ = force_update; }
82 
84  void add_filter(Filter *filter);
85 
96  void add_filters(const std::vector<Filter *> &filters);
97 
99  void set_filters(const std::vector<Filter *> &filters);
100 
102  void clear_filters();
103 
105  float get_state() const;
107  float get_raw_state() const;
108 
116  void publish_state(float state);
117 
118  // ========== INTERNAL METHODS ==========
119  // (In most use cases you won't need these)
121  void add_on_state_callback(std::function<void(float)> &&callback);
123  void add_on_raw_state_callback(std::function<void(float)> &&callback);
124 
132  float state;
133 
138  float raw_state;
139 
141  bool has_state() const;
142 
148  virtual std::string unique_id();
149 
150  void internal_send_state_to_frontend(float state);
151 
152  protected:
157  virtual std::string unit_of_measurement(); // NOLINT
158 
163  virtual int8_t accuracy_decimals(); // NOLINT
164 
169  virtual std::string device_class(); // NOLINT
170 
175  virtual StateClass state_class(); // NOLINT
176 
179 
180  bool has_state_{false};
181  Filter *filter_list_{nullptr};
182 
187  bool force_update_{false};
188 };
189 
190 } // namespace sensor
191 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:79
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
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
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
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
void set_force_update(bool force_update)
Set force update mode.
Definition: sensor.h:81
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
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
bool force_update_
Force update mode.
Definition: sensor.h:187
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