Merge remote-tracking branch 'origin/ifdef-sensor-filter' into integration

This commit is contained in:
J. Nick Koston
2026-02-22 17:33:32 -06:00
6 changed files with 26 additions and 0 deletions

View File

@@ -905,6 +905,7 @@ async def setup_sensor_core_(var, config):
if config[CONF_FORCE_UPDATE]:
cg.add(var.set_force_update(True))
if config.get(CONF_FILTERS): # must exist and not be empty
cg.add_define("USE_SENSOR_FILTER")
filters = await build_filters(config[CONF_FILTERS])
cg.add(var.set_filters(filters))

View File

@@ -1,3 +1,6 @@
#include "esphome/core/defines.h"
#ifdef USE_SENSOR_FILTER
#include "filter.h"
#include <cmath>
#include "esphome/core/application.h"
@@ -581,3 +584,5 @@ void StreamingMovingAverageFilter::reset_batch() {
}
} // namespace esphome::sensor
#endif // USE_SENSOR_FILTER

View File

@@ -1,5 +1,8 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_SENSOR_FILTER
#include <queue>
#include <utility>
#include <vector>
@@ -638,3 +641,5 @@ class StreamingMovingAverageFilter : public StreamingFilter {
};
} // namespace esphome::sensor
#endif // USE_SENSOR_FILTER

View File

@@ -68,11 +68,15 @@ void Sensor::publish_state(float state) {
ESP_LOGV(TAG, "'%s': Received new state %f", this->name_.c_str(), state);
#ifdef USE_SENSOR_FILTER
if (this->filter_list_ == nullptr) {
#endif
this->internal_send_state_to_frontend(state);
#ifdef USE_SENSOR_FILTER
} else {
this->filter_list_->input(state);
}
#endif
}
void Sensor::add_on_state_callback(std::function<void(float)> &&callback) { this->callback_.add(std::move(callback)); }
@@ -80,6 +84,7 @@ void Sensor::add_on_raw_state_callback(std::function<void(float)> &&callback) {
this->raw_callback_.add(std::move(callback));
}
#ifdef USE_SENSOR_FILTER
void Sensor::add_filter(Filter *filter) {
// inefficient, but only happens once on every sensor setup and nobody's going to have massive amounts of
// filters
@@ -109,6 +114,7 @@ void Sensor::clear_filters() {
}
this->filter_list_ = nullptr;
}
#endif // USE_SENSOR_FILTER
float Sensor::get_state() const { return this->state; }
float Sensor::get_raw_state() const { return this->raw_state; }

View File

@@ -4,13 +4,17 @@
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_SENSOR_FILTER
#include "esphome/components/sensor/filter.h"
#endif
#include <initializer_list>
#include <memory>
namespace esphome::sensor {
class Sensor;
void log_sensor(const char *tag, const char *prefix, const char *type, Sensor *obj);
#define LOG_SENSOR(prefix, type, obj) log_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
@@ -67,6 +71,7 @@ class Sensor : public EntityBase {
/// Set force update mode.
void set_force_update(bool force_update) { sensor_flags_.force_update = force_update; }
#ifdef USE_SENSOR_FILTER
/// Add a filter to the filter chain. Will be appended to the back.
void add_filter(Filter *filter);
@@ -87,6 +92,7 @@ class Sensor : public EntityBase {
/// Clear the entire filter chain.
void clear_filters();
#endif
/// Getter-syntax for .state.
float get_state() const;
@@ -130,7 +136,9 @@ class Sensor : public EntityBase {
LazyCallbackManager<void(float)> raw_callback_; ///< Storage for raw state callbacks.
LazyCallbackManager<void(float)> callback_; ///< Storage for filtered state callbacks.
#ifdef USE_SENSOR_FILTER
Filter *filter_list_{nullptr}; ///< Store all active filters.
#endif
// Group small members together to avoid padding
int8_t accuracy_decimals_{-1}; ///< Accuracy in decimals (-1 = not set)

View File

@@ -116,6 +116,7 @@
#define USE_SELECT
#define USE_SENSOR
#define USE_SETUP_HEAP_STATS
#define USE_SENSOR_FILTER
#define USE_SETUP_PRIORITY_OVERRIDE
#define USE_STATUS_LED
#define USE_STATUS_SENSOR