diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 9a4275d57a..61059af64f 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -246,17 +246,15 @@ bool value_list_matches_any(Sensor *parent, float sensor_value, const Templatabl return false; } -bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_inputs) { +bool throttle_check_with_priority(uint32_t &last_input, uint32_t min_time_between_inputs, bool is_prioritized) { const uint32_t now = App.get_loop_component_start_time(); - if (last_input == 0 || now - last_input >= min_time_between_inputs) { + if (last_input == 0 || now - last_input >= min_time_between_inputs || is_prioritized) { last_input = now; return true; } return false; } -void throttle_update_timestamp(uint32_t &last_input) { last_input = App.get_loop_component_start_time(); } - // ThrottleFilter ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {} optional ThrottleFilter::new_value(float value) { diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 50c3e2cf71..1f2b721b76 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -378,12 +378,9 @@ class ThrottleFilter : public Filter { uint32_t min_time_between_inputs_; }; -/// Returns true if throttle time has expired or this is the first input. -/// Implementation in filter.cpp (accesses App without circular include). -bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_inputs); - -/// Update last_input timestamp to current loop time. Implementation in filter.cpp. -void throttle_update_timestamp(uint32_t &last_input); +/// 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); /// Same as 'throttle' but will immediately publish values contained in `value_to_prioritize`. template class ThrottleWithPriorityFilter : public ValueListFilter { @@ -393,9 +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_and_update(this->last_input_, this->min_time_between_inputs_) || - this->value_matches_any_(value)) { - throttle_update_timestamp(this->last_input_); + if (throttle_check_with_priority(this->last_input_, this->min_time_between_inputs_, + this->value_matches_any_(value))) { return value; } return {};