diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 61059af64f1..1a033efd9df 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -246,13 +246,15 @@ bool value_list_matches_any(Sensor *parent, float sensor_value, const Templatabl return false; } -bool throttle_check_with_priority(uint32_t &last_input, uint32_t min_time_between_inputs, bool is_prioritized) { +optional throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue *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 || is_prioritized) { + if (last_input == 0 || now - last_input >= min_time_between_inputs || + value_list_matches_any(parent, value, values, count)) { last_input = now; - return true; + return value; } - return false; + return {}; } // ThrottleFilter diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 1f2b721b76d..80fa14742c2 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -378,9 +378,9 @@ class ThrottleFilter : public Filter { uint32_t min_time_between_inputs_; }; -/// Check throttle and optionally allow prioritized values through. -/// Always updates last_input when returning true. Implementation in filter.cpp. -bool throttle_check_with_priority(uint32_t &last_input, uint32_t min_time_between_inputs, bool is_prioritized); +/// Non-template helper for ThrottleWithPriorityFilter (implementation in filter.cpp) +optional throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableValue *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 class ThrottleWithPriorityFilter : public ValueListFilter { @@ -390,11 +390,8 @@ template class ThrottleWithPriorityFilter : public ValueListFilter : ValueListFilter(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {} optional new_value(float value) override { - if (throttle_check_with_priority(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: