15#define LOG_SENSOR(prefix, type, obj) \
16 if ((obj) != nullptr) { \
19 "%s State Class: '%s'\n" \
20 "%s Unit of Measurement: '%s'\n" \
21 "%s Accuracy Decimals: %d", \
22 prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str(), prefix, \
23 state_class_to_string((obj)->get_state_class()).c_str(), prefix, \
24 (obj)->get_unit_of_measurement().c_str(), prefix, (obj)->get_accuracy_decimals()); \
25 if (!(obj)->get_device_class().empty()) { \
26 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
28 if (!(obj)->get_icon().empty()) { \
29 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
31 if (!(obj)->unique_id().empty()) { \
32 ESP_LOGV(TAG, "%s Unique ID: '%s'", prefix, (obj)->unique_id().c_str()); \
34 if ((obj)->get_force_update()) { \
35 ESP_LOGV(TAG, "%s Force Update: YES", prefix); \
39#define SUB_SENSOR(name) \
41 sensor::Sensor *name##_sensor_{nullptr}; \
44 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_ = sensor; }
100 void add_filters(
const std::vector<Filter *> &filters);
103 void set_filters(
const std::vector<Filter *> &filters);
Apply a filter to sensor values such as moving average.
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)
optional< int8_t > accuracy_decimals_
Accuracy in decimals override.
float get_raw_state() const
Getter-syntax for .raw_state.
void set_force_update(bool force_update)
Set force update mode.
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.
bool force_update_
Force update mode.
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.
optional< StateClass > state_class_
State class override.
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
void clear_filters()
Clear the entire filter chain.
bool get_force_update() const
Get whether force update mode is enabled.
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.
std::string state_class_to_string(StateClass state_class)
StateClass
Sensor state classes.
@ STATE_CLASS_TOTAL_INCREASING
@ STATE_CLASS_MEASUREMENT
Providing packet encoding functions for exchanging data with a remote host.