7static const char *
const TAG =
"sensor";
17 "%s State Class: '%s'\n"
18 "%s Unit of Measurement: '%s'\n"
19 "%s Accuracy Decimals: %d",
33 ESP_LOGV(tag,
"%s Force Update: YES", prefix);
38 switch (state_class) {
40 return LOG_STR(
"measurement");
42 return LOG_STR(
"total_increasing");
44 return LOG_STR(
"total");
79 ESP_LOGV(TAG,
"'%s': Received new state %f", this->
name_.
c_str(), state);
99 ESP_LOGVV(TAG,
"Sensor(%p)::add_filter(%p)",
this, filter);
104 while (last_filter->
next_ !=
nullptr)
105 last_filter = last_filter->
next_;
111 for (
Filter *filter : filters) {
121 ESP_LOGVV(TAG,
"Sensor(%p)::clear_filters()",
this);
131 ESP_LOGD(TAG,
"'%s': Sending state %.5f %s with %d decimals of accuracy", this->
get_name().c_str(),
state,
StringRef get_device_class_ref() const
Get the device class as StringRef.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
void set_has_state(bool state)
constexpr const char * c_str() const
constexpr bool empty() const
Apply a filter to sensor values such as moving average.
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Base-class for all sensors.
void set_state_class(StateClass state_class)
Manually set the state class.
void add_filter(Filter *filter)
Add a filter to the filter chain. Will be appended to the back.
void publish_state(float state)
Publish a new state to the front-end.
void internal_send_state_to_frontend(float state)
float get_raw_state() const
Getter-syntax for .raw_state.
CallbackManager< void(float)> callback_
Storage for filtered state callbacks.
void add_filters(const std::vector< Filter * > &filters)
Add a list of vectors to the back of the filter chain.
float get_state() const
Getter-syntax for .state.
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
void set_filters(const std::vector< Filter * > &filters)
Clear the filters and replace them by filters.
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
float state
This member variable stores the last state that has passed through all filters.
StateClass get_state_class()
Get the state class, using the manual override if set.
Filter * filter_list_
Store all active filters.
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
void clear_filters()
Clear the entire filter chain.
int8_t accuracy_decimals_
Accuracy in decimals (-1 = not set)
StateClass state_class_
State class (STATE_CLASS_NONE = not set)
bool get_force_update() const
Get whether force update mode is enabled.
struct esphome::sensor::Sensor::SensorFlags sensor_flags_
std::unique_ptr< CallbackManager< void(float)> > raw_callback_
Storage for raw state callbacks (lazy allocated).
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.
StateClass
Sensor state classes.
@ STATE_CLASS_TOTAL_INCREASING
@ STATE_CLASS_MEASUREMENT
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj)
const LogString * state_class_to_string(StateClass state_class)
Providing packet encoding functions for exchanging data with a remote host.
uint8_t has_state_class_override
uint8_t has_accuracy_override