Merge sensor_value_list_static_array into integration

This commit is contained in:
J. Nick Koston
2026-03-28 08:46:22 -10:00
3 changed files with 18 additions and 19 deletions
+11 -9
View File
@@ -222,15 +222,6 @@ MultiplyFilter::MultiplyFilter(TemplatableValue<float> multiplier) : multiplier_
optional<float> MultiplyFilter::new_value(float value) { return value * this->multiplier_.value(); }
bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_inputs) {
const uint32_t now = App.get_loop_component_start_time();
if (last_input == 0 || now - last_input >= min_time_between_inputs) {
last_input = now;
return true;
}
return false;
}
// ValueListFilter helper (non-template, shared by all ValueListFilter<N> instantiations)
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableValue<float> *values, size_t count) {
int8_t accuracy = parent->get_accuracy_decimals();
@@ -266,6 +257,17 @@ optional<float> ThrottleFilter::new_value(float value) {
return {};
}
// ThrottleWithPriorityFilter helper (non-template, keeps App access in .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) {
const uint32_t now = App.get_loop_component_start_time();
if (last_input == 0 || now - last_input >= min_time_between_inputs ||
value_list_matches_any(parent, value, values, count)) {
last_input = now;
return value;
}
return {};
}
// DeltaFilter
DeltaFilter::DeltaFilter(float min_a0, float min_a1, float max_a0, float max_a1)
: min_a0_(min_a0), min_a1_(min_a1), max_a0_(max_a0), max_a1_(max_a1) {}
+6 -9
View File
@@ -332,10 +332,6 @@ class MultiplyFilter : public Filter {
/// 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);
/// Returns true if throttle should allow the value through (time expired or first input).
/// Updates last_input in-place. Implementation in filter.cpp (accesses App without circular include).
bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_inputs);
/** Base class for filters that compare sensor values against a fixed list of configured values.
*
* Templated on N (the number of values) so the list is stored inline in a std::array,
@@ -382,6 +378,10 @@ class ThrottleFilter : public Filter {
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`.
template<size_t N> class ThrottleWithPriorityFilter : public ValueListFilter<N> {
public:
@@ -390,11 +390,8 @@ template<size_t N> class ThrottleWithPriorityFilter : public ValueListFilter<N>
: ValueListFilter<N>(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {}
optional<float> new_value(float value) override {
if (throttle_check_and_update(this->last_input_, this->min_time_between_inputs_) ||
this->value_matches_any_(value)) {
return value;
}
return {};
return throttle_with_priority_new_value(this->parent_, value, this->values_.data(), N, this->last_input_,
this->min_time_between_inputs_);
}
protected:
+1 -1
View File
@@ -503,7 +503,7 @@ template<typename T, size_t MAX_CAPACITY = std::numeric_limits<uint16_t>::max()>
/// 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; ESPHOME_DEBUG_ASSERT catches mismatches in debug/integration tests.
/// 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);