mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[sensor] Use std::array in ValueList/FilterOut/ThrottleWithPriority filters (#15265)
This commit is contained in:
@@ -381,7 +381,7 @@ async def filter_out_filter_to_code(config, filter_id):
|
|||||||
if not isinstance(config, list):
|
if not isinstance(config, list):
|
||||||
config = [config]
|
config = [config]
|
||||||
template_ = [await cg.templatable(x, [], float) for x in config]
|
template_ = [await cg.templatable(x, [], float) for x in config]
|
||||||
return cg.new_Pvariable(filter_id, template_)
|
return cg.new_Pvariable(filter_id, cg.TemplateArguments(len(template_)), template_)
|
||||||
|
|
||||||
|
|
||||||
QUANTILE_SCHEMA = cv.All(
|
QUANTILE_SCHEMA = cv.All(
|
||||||
@@ -650,7 +650,9 @@ async def throttle_with_priority_filter_to_code(config, filter_id):
|
|||||||
if not isinstance(config[CONF_VALUE], list):
|
if not isinstance(config[CONF_VALUE], list):
|
||||||
config[CONF_VALUE] = [config[CONF_VALUE]]
|
config[CONF_VALUE] = [config[CONF_VALUE]]
|
||||||
template_ = [await cg.templatable(x, [], float) for x in config[CONF_VALUE]]
|
template_ = [await cg.templatable(x, [], float) for x in config[CONF_VALUE]]
|
||||||
return cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], template_)
|
return cg.new_Pvariable(
|
||||||
|
filter_id, cg.TemplateArguments(len(template_)), config[CONF_TIMEOUT], template_
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
HEARTBEAT_SCHEMA = cv.Schema(
|
HEARTBEAT_SCHEMA = cv.Schema(
|
||||||
|
|||||||
@@ -222,16 +222,14 @@ MultiplyFilter::MultiplyFilter(TemplatableValue<float> multiplier) : multiplier_
|
|||||||
|
|
||||||
optional<float> MultiplyFilter::new_value(float value) { return value * this->multiplier_.value(); }
|
optional<float> MultiplyFilter::new_value(float value) { return value * this->multiplier_.value(); }
|
||||||
|
|
||||||
// ValueListFilter (base class)
|
// ValueListFilter helper (non-template, shared by all ValueListFilter<N> instantiations)
|
||||||
ValueListFilter::ValueListFilter(std::initializer_list<TemplatableValue<float>> values) : values_(values) {}
|
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableValue<float> *values, size_t count) {
|
||||||
|
int8_t accuracy = parent->get_accuracy_decimals();
|
||||||
bool ValueListFilter::value_matches_any_(float sensor_value) {
|
|
||||||
int8_t accuracy = this->parent_->get_accuracy_decimals();
|
|
||||||
float accuracy_mult = pow10_int(accuracy);
|
float accuracy_mult = pow10_int(accuracy);
|
||||||
float rounded_sensor = roundf(accuracy_mult * sensor_value);
|
float rounded_sensor = roundf(accuracy_mult * sensor_value);
|
||||||
|
|
||||||
for (auto &filter_value : this->values_) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
float fv = filter_value.value();
|
float fv = values[i].value();
|
||||||
|
|
||||||
// Handle NaN comparison
|
// Handle NaN comparison
|
||||||
if (std::isnan(fv)) {
|
if (std::isnan(fv)) {
|
||||||
@@ -248,16 +246,6 @@ bool ValueListFilter::value_matches_any_(float sensor_value) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterOutValueFilter
|
|
||||||
FilterOutValueFilter::FilterOutValueFilter(std::initializer_list<TemplatableValue<float>> values_to_filter_out)
|
|
||||||
: ValueListFilter(values_to_filter_out) {}
|
|
||||||
|
|
||||||
optional<float> FilterOutValueFilter::new_value(float value) {
|
|
||||||
if (this->value_matches_any_(value))
|
|
||||||
return {}; // Filter out
|
|
||||||
return value; // Pass through
|
|
||||||
}
|
|
||||||
|
|
||||||
// ThrottleFilter
|
// ThrottleFilter
|
||||||
ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {}
|
ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {}
|
||||||
optional<float> ThrottleFilter::new_value(float value) {
|
optional<float> ThrottleFilter::new_value(float value) {
|
||||||
@@ -269,17 +257,13 @@ optional<float> ThrottleFilter::new_value(float value) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// ThrottleWithPriorityFilter
|
// ThrottleWithPriorityFilter helper (non-template, keeps App access in .cpp)
|
||||||
ThrottleWithPriorityFilter::ThrottleWithPriorityFilter(
|
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue<float> *values,
|
||||||
uint32_t min_time_between_inputs, std::initializer_list<TemplatableValue<float>> prioritized_values)
|
size_t count, uint32_t &last_input, uint32_t min_time_between_inputs) {
|
||||||
: ValueListFilter(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {}
|
|
||||||
|
|
||||||
optional<float> ThrottleWithPriorityFilter::new_value(float value) {
|
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
// Allow value through if: no previous input, time expired, or is prioritized
|
if (last_input == 0 || now - last_input >= min_time_between_inputs ||
|
||||||
if (this->last_input_ == 0 || now - this->last_input_ >= min_time_between_inputs_ ||
|
value_list_matches_any(parent, value, values, count)) {
|
||||||
this->value_matches_any_(value)) {
|
last_input = now;
|
||||||
this->last_input_ = now;
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#ifdef USE_SENSOR_FILTER
|
#ifdef USE_SENSOR_FILTER
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
@@ -328,28 +329,42 @@ class MultiplyFilter : public Filter {
|
|||||||
TemplatableValue<float> multiplier_;
|
TemplatableValue<float> multiplier_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Base class for filters that compare sensor values against a list of configured values.
|
/// Non-template helper for value matching (implementation in filter.cpp)
|
||||||
|
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableValue<float> *values, size_t count);
|
||||||
|
|
||||||
|
/** Base class for filters that compare sensor values against a fixed list of configured values.
|
||||||
*
|
*
|
||||||
* This base class provides common functionality for filters that need to check if a sensor
|
* Templated on N (the number of values) so the list is stored inline in a std::array,
|
||||||
* value matches any value in a configured list, with proper handling of NaN values and
|
* avoiding heap allocation and the overhead of FixedVector.
|
||||||
* accuracy-based rounding for comparisons.
|
*
|
||||||
|
* @tparam N Number of values in the filter list, set by code generation to match
|
||||||
|
* the exact number of values configured in YAML.
|
||||||
*/
|
*/
|
||||||
class ValueListFilter : public Filter {
|
template<size_t N> class ValueListFilter : public Filter {
|
||||||
protected:
|
protected:
|
||||||
explicit ValueListFilter(std::initializer_list<TemplatableValue<float>> values);
|
explicit ValueListFilter(std::initializer_list<TemplatableValue<float>> values) {
|
||||||
|
init_array_from(this->values_, values);
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if sensor value matches any configured value (with accuracy rounding)
|
/// Check if sensor value matches any configured value (with accuracy rounding)
|
||||||
bool value_matches_any_(float sensor_value);
|
bool value_matches_any_(float sensor_value) {
|
||||||
|
return value_list_matches_any(this->parent_, sensor_value, this->values_.data(), N);
|
||||||
|
}
|
||||||
|
|
||||||
FixedVector<TemplatableValue<float>> values_;
|
std::array<TemplatableValue<float>, N> values_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A simple filter that only forwards the filter chain if it doesn't receive `value_to_filter_out`.
|
/// A simple filter that only forwards the filter chain if it doesn't receive `value_to_filter_out`.
|
||||||
class FilterOutValueFilter : public ValueListFilter {
|
template<size_t N> class FilterOutValueFilter : public ValueListFilter<N> {
|
||||||
public:
|
public:
|
||||||
explicit FilterOutValueFilter(std::initializer_list<TemplatableValue<float>> values_to_filter_out);
|
explicit FilterOutValueFilter(std::initializer_list<TemplatableValue<float>> values_to_filter_out)
|
||||||
|
: ValueListFilter<N>(values_to_filter_out) {}
|
||||||
|
|
||||||
optional<float> new_value(float value) override;
|
optional<float> new_value(float value) override {
|
||||||
|
if (this->value_matches_any_(value))
|
||||||
|
return {}; // Filter out
|
||||||
|
return value; // Pass through
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ThrottleFilter : public Filter {
|
class ThrottleFilter : public Filter {
|
||||||
@@ -363,13 +378,21 @@ class ThrottleFilter : public Filter {
|
|||||||
uint32_t min_time_between_inputs_;
|
uint32_t min_time_between_inputs_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Non-template helper for ThrottleWithPriorityFilter (implementation in filter.cpp)
|
||||||
|
optional<float> throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue<float> *values,
|
||||||
|
size_t count, uint32_t &last_input, uint32_t min_time_between_inputs);
|
||||||
|
|
||||||
/// Same as 'throttle' but will immediately publish values contained in `value_to_prioritize`.
|
/// Same as 'throttle' but will immediately publish values contained in `value_to_prioritize`.
|
||||||
class ThrottleWithPriorityFilter : public ValueListFilter {
|
template<size_t N> class ThrottleWithPriorityFilter : public ValueListFilter<N> {
|
||||||
public:
|
public:
|
||||||
explicit ThrottleWithPriorityFilter(uint32_t min_time_between_inputs,
|
explicit ThrottleWithPriorityFilter(uint32_t min_time_between_inputs,
|
||||||
std::initializer_list<TemplatableValue<float>> prioritized_values);
|
std::initializer_list<TemplatableValue<float>> prioritized_values)
|
||||||
|
: ValueListFilter<N>(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {}
|
||||||
|
|
||||||
optional<float> new_value(float value) override;
|
optional<float> new_value(float value) override {
|
||||||
|
return throttle_with_priority_new_value(this->parent_, value, this->values_.data(), N, this->last_input_,
|
||||||
|
this->min_time_between_inputs_);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t last_input_{0};
|
uint32_t last_input_{0};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -497,6 +498,23 @@ template<typename T, size_t MAX_CAPACITY = std::numeric_limits<uint16_t>::max()>
|
|||||||
index_type capacity_{0};
|
index_type capacity_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Initialize a std::array from an initializer_list. Uses memcpy for trivially copyable types (optimal codegen),
|
||||||
|
/// falls back to element-wise copy for non-trivially copyable types (e.g. TemplatableValue).
|
||||||
|
/// N is set by code generation; assert catches mismatches in debug/integration tests.
|
||||||
|
template<typename T, size_t N> inline void init_array_from(std::array<T, N> &dest, std::initializer_list<T> src) {
|
||||||
|
#ifdef ESPHOME_DEBUG
|
||||||
|
assert(src.size() == N);
|
||||||
|
#endif
|
||||||
|
if constexpr (std::is_trivially_copyable_v<T>) {
|
||||||
|
__builtin_memcpy(dest.data(), src.begin(), N * sizeof(T));
|
||||||
|
} else {
|
||||||
|
size_t i = 0;
|
||||||
|
for (const auto &v : src) {
|
||||||
|
dest[i++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Fixed-capacity vector - allocates once at runtime, never reallocates
|
/// Fixed-capacity vector - allocates once at runtime, never reallocates
|
||||||
/// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append)
|
/// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append)
|
||||||
/// when size is known at initialization but not at compile time
|
/// when size is known at initialization but not at compile time
|
||||||
|
|||||||
Reference in New Issue
Block a user