ESPHome  2021.11.3
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:
154  virtual std::string unit_of_measurement(); // NOLINT
155 
157  virtual int8_t accuracy_decimals(); // NOLINT
158 
160  virtual std::string device_class(); // NOLINT
161 
163  virtual StateClass state_class(); // NOLINT
164 
165  uint32_t hash_base() override;
166 
169 
170  bool has_state_{false};
171  Filter *filter_list_{nullptr};
172 
177  bool force_update_{false};
178 };
179 
180 } // namespace sensor
181 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:79
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:71
void clear_filters()
Clear the entire filter chain.
Definition: sensor.cpp:99
optional< std::string > device_class_
Device class override.
Definition: sensor.h:175
virtual std::string unit_of_measurement()
Override this to set the default unit of measurement.
Definition: sensor.cpp:32
virtual std::string device_class()
Override this to set the default device class.
Definition: sensor.cpp:48
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition: sensor.h:167
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: sensor.cpp:95
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
Definition: sensor.h:174
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:72
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:76
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:107
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
Definition: sensor.cpp:39
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:29
optional< std::string > unit_of_measurement_
Unit of measurement override.
Definition: sensor.h:173
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:58
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition: sensor.h:168
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:51
virtual StateClass state_class()
Override this to set the default state class.
Definition: sensor.cpp:56
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: sensor.cpp:42
float get_raw_state() const
Getter-syntax for .raw_state.
Definition: sensor.cpp:106
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: sensor.cpp:90
Filter * filter_list_
Store all active filters.
Definition: sensor.h:171
bool force_update_
Force update mode.
Definition: sensor.h:177
float get_state() const
Getter-syntax for .state.
Definition: sensor.cpp:105
void set_device_class(const std::string &device_class)
Manually set the device class.
Definition: sensor.cpp:47
void internal_send_state_to_frontend(float state)
Definition: sensor.cpp:109
uint32_t hash_base() override
Definition: sensor.cpp:117
Apply a filter to sensor values such as moving average.
Definition: filter.h:18
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
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:116
void set_state_class(StateClass state_class)
Manually set the state class.
Definition: sensor.cpp:50
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:34
optional< StateClass > state_class_
State class override.
Definition: sensor.h:176
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:40