ESPHome  1.15.2
Go to the documentation of this file.
1 #pragma once
4 #include "esphome/core/helpers.h"
7 namespace esphome {
8 namespace sensor {
10 #define LOG_SENSOR(prefix, type, obj) \
11  if (obj != nullptr) { \
12  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, obj->get_name().c_str()); \
13  ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, obj->get_unit_of_measurement().c_str()); \
14  ESP_LOGCONFIG(TAG, "%s Accuracy Decimals: %d", prefix, obj->get_accuracy_decimals()); \
15  if (!obj->get_icon().empty()) { \
16  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, obj->get_icon().c_str()); \
17  } \
18  if (!obj->unique_id().empty()) { \
19  ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, obj->unique_id().c_str()); \
20  } \
21  if (obj->get_force_update()) { \
22  ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
23  } \
24  }
30 class Sensor : public Nameable {
31  public:
32  explicit Sensor();
33  explicit Sensor(const std::string &name);
40  void set_unit_of_measurement(const std::string &unit_of_measurement);
46  void set_icon(const std::string &icon);
56  void add_filter(Filter *filter);
68  void add_filters(const std::vector<Filter *> &filters);
71  void set_filters(const std::vector<Filter *> &filters);
74  void clear_filters();
77  float get_value() const ESPDEPRECATED(".value is deprecated, please use .state");
79  float get_state() const;
81  float get_raw_value() const ESPDEPRECATED(".raw_value is deprecated, please use .raw_state");
83  float get_raw_state() const;
86  int8_t get_accuracy_decimals();
89  std::string get_unit_of_measurement();
92  std::string get_icon();
101  void publish_state(float state);
107  void push_new_value(float state) ESPDEPRECATED("push_new_value is deprecated. Please use .publish_state instead");
109  // ========== INTERNAL METHODS ==========
110  // (In most use cases you won't need these)
112  void add_on_state_callback(std::function<void(float)> &&callback);
114  void add_on_raw_state_callback(std::function<void(float)> &&callback);
123  float state;
128  float raw_state;
131  bool has_state() const;
138  virtual std::string unique_id();
141  virtual uint32_t update_interval();
146  void internal_send_state_to_frontend(float state);
148  bool get_force_update() const { return force_update_; }
155  void set_force_update(bool force_update) { force_update_ = force_update; }
157  protected:
164  virtual std::string unit_of_measurement(); // NOLINT
172  virtual std::string icon(); // NOLINT
175  virtual int8_t accuracy_decimals(); // NOLINT
177  uint32_t hash_base() override;
187  Filter *filter_list_{nullptr};
188  bool has_state_{false};
189  bool force_update_{false};
190 };
193  public:
194  explicit PollingSensorComponent(const std::string &name, uint32_t update_interval);
196  uint32_t update_interval() override;
197 };
199 } // namespace sensor
200 } // namespace esphome
bool get_force_update() const
Definition: sensor.h:148
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:34
void clear_filters()
Clear the entire filter chain.
Definition: sensor.cpp:76
virtual std::string unit_of_measurement()
Override this to set the Home Assistant unit of measurement for this sensor.
Definition: sensor.cpp:22
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
Definition: sensor.h:179
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
Definition: sensor.cpp:72
optional< int8_t > accuracy_decimals_
Override the accuracy in decimals, otherwise the sensor&#39;s values will be used.
Definition: sensor.h:186
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:35
virtual uint32_t update_interval()
Return with which interval the sensor is polled. Return 0 for non-polling mode.
Definition: sensor.cpp:24
STL namespace.
float please use state
Definition: sensor.h:77
This class simplifies creating components that periodically check a state.
Definition: component.h:210
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
Definition: sensor.cpp:53
void set_force_update(bool force_update)
Set this sensor&#39;s force_update mode.
Definition: sensor.h:155
float get_value() const ESPDEPRECATED(".value is deprecated
Getter-syntax for .value. Please use .state instead.
Definition: sensor.cpp:82
float please use raw_state
Definition: sensor.h:81
std::string get_unit_of_measurement()
Get the unit of measurement. Uses the manual override if specified or the default value instead...
Definition: sensor.cpp:43
virtual std::string unique_id()
A unique ID for this sensor, empty for no unique id.
Definition: sensor.cpp:86
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals for this sensor.
Definition: sensor.cpp:33
void set_unit_of_measurement(const std::string &unit_of_measurement)
Manually set the unit of measurement of this sensor.
Definition: sensor.cpp:29
optional< std::string > unit_of_measurement_
Override the unit of measurement.
Definition: sensor.h:182
uint32_t calculate_expected_filter_update_interval()
Calculate the expected update interval for values that pass through all filters.
Definition: sensor.cpp:96
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:9
virtual std::string icon()
Override this to set the Home Assistant icon for this sensor.
Definition: sensor.cpp:23
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
Definition: sensor.h:180
void push_new_value(float state) ESPDEPRECATED("push_new_value is deprecated. Please use .publish_state instead")
Push a new value to the MQTT front-end.
Definition: sensor.cpp:21
float get_raw_state() const
Getter-syntax for .raw_state.
Definition: sensor.cpp:85
Helper class that enables naming of objects so that it doesn&#39;t have to be re-implement every time...
Definition: component.h:244
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Definition: sensor.cpp:67
Filter * filter_list_
Store all active filters.
Definition: sensor.h:187
float get_state() const
Getter-syntax for .state.
Definition: sensor.cpp:83
void internal_send_state_to_frontend(float state)
Definition: sensor.cpp:88
uint32_t hash_base() override
Definition: sensor.cpp:108
void set_icon(const std::string &icon)
Manually set the icon of this sensor.
Definition: sensor.cpp:32
Apply a filter to sensor values such as moving average.
Definition: filter.h:17
float get_raw_value() const ESPDEPRECATED(".raw_value is deprecated
Getter-syntax for .raw_value. Please use .raw_state instead.
Definition: sensor.cpp:84
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:95
optional< std::string > icon_
Override the icon advertised to Home Assistant, otherwise sensor&#39;s icon will be used.
Definition: sensor.h:184
int8_t get_accuracy_decimals()
Get the accuracy in decimals. Uses the manual override if specified or the default value instead...
Definition: sensor.cpp:48
std::string get_icon()
Get the Home Assistant Icon. Uses the manual override if specified or the default value instead...
Definition: sensor.cpp:38
Base-class for all sensors.
Definition: sensor.h:30
virtual int8_t accuracy_decimals()
Return the accuracy in decimals for this sensor.
Definition: sensor.cpp:25