[sensor] A little bit of filter clean-up (#9986)

This commit is contained in:
Keith Burzinski 2025-07-31 21:55:59 -05:00 committed by GitHub
parent 0954a6185c
commit 291215909a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 9 deletions

View File

@ -599,7 +599,9 @@ async def throttle_filter_to_code(config, filter_id):
TIMEOUT_WITH_PRIORITY_SCHEMA = cv.maybe_simple_value(
{
cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
cv.Optional(CONF_VALUE, default="nan"): cv.ensure_list(cv.float_),
cv.Optional(CONF_VALUE, default="nan"): cv.Any(
cv.templatable(cv.float_), [cv.templatable(cv.float_)]
),
},
key=CONF_TIMEOUT,
)
@ -611,6 +613,8 @@ TIMEOUT_WITH_PRIORITY_SCHEMA = cv.maybe_simple_value(
TIMEOUT_WITH_PRIORITY_SCHEMA,
)
async def throttle_with_priority_filter_to_code(config, filter_id):
if not isinstance(config[CONF_VALUE], list):
config[CONF_VALUE] = [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_)

View File

@ -225,7 +225,7 @@ optional<float> SlidingWindowMovingAverageFilter::new_value(float value) {
// ExponentialMovingAverageFilter
ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
: send_every_(send_every), send_at_(send_every - send_first_at), alpha_(alpha) {}
: alpha_(alpha), send_every_(send_every), send_at_(send_every - send_first_at) {}
optional<float> ExponentialMovingAverageFilter::new_value(float value) {
if (!std::isnan(value)) {
if (this->first_value_) {
@ -325,7 +325,7 @@ optional<float> FilterOutValueFilter::new_value(float value) {
// ThrottleFilter
ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {}
optional<float> ThrottleFilter::new_value(float value) {
const uint32_t now = millis();
const uint32_t now = App.get_loop_component_start_time();
if (this->last_input_ == 0 || now - this->last_input_ >= min_time_between_inputs_) {
this->last_input_ = now;
return value;
@ -369,7 +369,7 @@ optional<float> ThrottleWithPriorityFilter::new_value(float value) {
// DeltaFilter
DeltaFilter::DeltaFilter(float delta, bool percentage_mode)
: delta_(delta), current_delta_(delta), percentage_mode_(percentage_mode), last_value_(NAN) {}
: delta_(delta), current_delta_(delta), last_value_(NAN), percentage_mode_(percentage_mode) {}
optional<float> DeltaFilter::new_value(float value) {
if (std::isnan(value)) {
if (std::isnan(this->last_value_)) {

View File

@ -221,11 +221,11 @@ class ExponentialMovingAverageFilter : public Filter {
void set_alpha(float alpha);
protected:
bool first_value_{true};
float accumulator_{NAN};
float alpha_;
size_t send_every_;
size_t send_at_;
float alpha_;
bool first_value_{true};
};
/** Simple throttle average filter.
@ -243,9 +243,9 @@ class ThrottleAverageFilter : public Filter, public Component {
float get_setup_priority() const override;
protected:
uint32_t time_period_;
float sum_{0.0f};
unsigned int n_{0};
uint32_t time_period_;
bool have_nan_{false};
};
@ -378,8 +378,8 @@ class DeltaFilter : public Filter {
protected:
float delta_;
float current_delta_;
bool percentage_mode_;
float last_value_{NAN};
bool percentage_mode_;
};
class OrFilter : public Filter {
@ -401,8 +401,8 @@ class OrFilter : public Filter {
};
std::vector<Filter *> filters_;
bool has_value_{false};
PhiNode phi_;
bool has_value_{false};
};
class CalibrateLinearFilter : public Filter {

View File

@ -135,10 +135,17 @@ sensor:
- throttle: 1s
- throttle_average: 2s
- throttle_with_priority: 5s
- throttle_with_priority:
timeout: 3s
value: 42.0
- throttle_with_priority:
timeout: 3s
value: !lambda return 1.0f / 2.0f;
- throttle_with_priority:
timeout: 3s
value:
- 42.0
- !lambda return 2.0f / 2.0f;
- nan
- timeout:
timeout: 10s