9static const char *
const TAG =
"sensor";
19 "%s State Class: '%s'\n"
20 "%s Unit of Measurement: '%s'\n"
21 "%s Accuracy Decimals: %d",
26 LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
27 LOG_ENTITY_ICON(tag, prefix, *obj);
30 ESP_LOGV(tag,
"%s Force Update: YES", prefix);
35PROGMEM_STRING_TABLE(StateClassStrings,
"",
"measurement",
"total_increasing",
"total",
"measurement_angle");
36static_assert(StateClassStrings::COUNT ==
STATE_CLASS_LAST + 1,
"StateClassStrings must match StateClass enum");
40 return StateClassStrings::get_log_str(
static_cast<uint8_t
>(state_class), 0);
69 ESP_LOGV(TAG,
"'%s': Received new state %f", this->
name_.
c_str(), state);
86 ESP_LOGVV(TAG,
"Sensor(%p)::add_filter(%p)",
this, filter);
91 while (last_filter->
next_ !=
nullptr)
92 last_filter = last_filter->
next_;
98 for (
Filter *filter : filters) {
108 ESP_LOGVV(TAG,
"Sensor(%p)::clear_filters()",
this);
121#if defined(USE_SENSOR) && defined(USE_CONTROLLER_REGISTRY)
static void notify_sensor_update(sensor::Sensor *obj)
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
void set_has_state(bool state)
constexpr const char * c_str() 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.
LazyCallbackManager< void(float)> callback_
Storage for filtered state callbacks.
void internal_send_state_to_frontend(float state)
float get_raw_state() const
Getter-syntax for .raw_state.
float get_state() const
Getter-syntax for .state.
void set_accuracy_decimals(int8_t accuracy_decimals)
Manually set the accuracy in decimals.
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.
LazyCallbackManager< void(float)> raw_callback_
Storage for raw state callbacks.
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_
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.
void set_filters(std::initializer_list< Filter * > filters)
Clear the filters and replace them by filters.
void add_filters(std::initializer_list< Filter * > filters)
Add a list of vectors to the back of the filter chain.
constexpr uint8_t STATE_CLASS_LAST
PROGMEM_STRING_TABLE(StateClassStrings, "", "measurement", "total_increasing", "total", "measurement_angle")
StateClass
Sensor state classes.
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj)
const LogString * state_class_to_string(StateClass state_class)
uint8_t has_state_class_override
uint8_t has_accuracy_override