7 static const char *
const TAG =
"sensor";
10 switch (state_class) {
14 return "total_increasing";
29 #pragma GCC diagnostic push 30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 32 #pragma GCC diagnostic pop 42 #pragma GCC diagnostic push 43 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 45 #pragma GCC diagnostic pop 53 #pragma GCC diagnostic push 54 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 56 #pragma GCC diagnostic pop 65 #pragma GCC diagnostic push 66 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 68 #pragma GCC diagnostic pop 76 ESP_LOGV(TAG,
"'%s': Received new state %f", this->
name_.c_str(),
state);
93 ESP_LOGVV(TAG,
"Sensor(%p)::add_filter(%p)",
this, filter);
98 while (last_filter->
next_ !=
nullptr)
99 last_filter = last_filter->
next_;
105 for (
Filter *filter : filters) {
115 ESP_LOGVV(TAG,
"Sensor(%p)::clear_filters()",
this);
126 ESP_LOGD(TAG,
"'%s': Sending state %.5f %s with %d decimals of accuracy", this->
get_name().c_str(), state,
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
void clear_filters()
Clear the entire filter chain.
optional< std::string > device_class_
Device class override.
virtual std::string unit_of_measurement()
Override this to set the default unit of measurement.
virtual std::string device_class()
Override this to set the default device class.
CallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
void set_filters(const std::vector< Filter *> &filters)
Clear the filters and replace them by filters.
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
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.
const std::string & get_name() const
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied...
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
virtual std::string unique_id()
A unique ID for this sensor, empty for no unique id.
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
float state
This member variable stores the last state that has passed through all filters.
void set_unit_of_measurement(const std::string &unit_of_measurement)
Manually set the unit of measurement.
optional< std::string > unit_of_measurement_
Unit of measurement override.
std::string state_class_to_string(StateClass state_class)
StateClass
Sensor state classes.
void publish_state(float state)
Publish a new state to the front-end.
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
StateClass get_state_class()
Get the state class, using the manual override if set.
virtual StateClass state_class()
Override this to set the default state class.
std::string get_device_class()
Get the device class, using the manual override if set.
float get_raw_state() const
Getter-syntax for .raw_state.
void add_filters(const std::vector< Filter *> &filters)
Add a list of vectors to the back of the filter chain.
Filter * filter_list_
Store all active filters.
float get_state() const
Getter-syntax for .state.
void set_device_class(const std::string &device_class)
Manually set the device class.
void internal_send_state_to_frontend(float state)
Apply a filter to sensor values such as moving average.
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
void set_state_class(StateClass state_class)
Manually set the state class.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
optional< StateClass > state_class_
State class override.
Base-class for all sensors.
virtual int8_t accuracy_decimals()
Override this to set the default accuracy in decimals.