Add timeout filter (#5104)

This commit is contained in:
Clyde Stubbs 2023-07-17 07:17:31 +10:00 committed by GitHub
parent 8c6cddf1bb
commit ac81fae855
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 0 deletions

View file

@ -217,6 +217,7 @@ OffsetFilter = sensor_ns.class_("OffsetFilter", Filter)
MultiplyFilter = sensor_ns.class_("MultiplyFilter", Filter) MultiplyFilter = sensor_ns.class_("MultiplyFilter", Filter)
FilterOutValueFilter = sensor_ns.class_("FilterOutValueFilter", Filter) FilterOutValueFilter = sensor_ns.class_("FilterOutValueFilter", Filter)
ThrottleFilter = sensor_ns.class_("ThrottleFilter", Filter) ThrottleFilter = sensor_ns.class_("ThrottleFilter", Filter)
TimeoutFilter = sensor_ns.class_("TimeoutFilter", Filter, cg.Component)
DebounceFilter = sensor_ns.class_("DebounceFilter", Filter, cg.Component) DebounceFilter = sensor_ns.class_("DebounceFilter", Filter, cg.Component)
HeartbeatFilter = sensor_ns.class_("HeartbeatFilter", Filter, cg.Component) HeartbeatFilter = sensor_ns.class_("HeartbeatFilter", Filter, cg.Component)
DeltaFilter = sensor_ns.class_("DeltaFilter", Filter) DeltaFilter = sensor_ns.class_("DeltaFilter", Filter)
@ -536,6 +537,15 @@ async def heartbeat_filter_to_code(config, filter_id):
return var return var
@FILTER_REGISTRY.register(
"timeout", TimeoutFilter, cv.positive_time_period_milliseconds
)
async def timeout_filter_to_code(config, filter_id):
var = cg.new_Pvariable(filter_id, config)
await cg.register_component(var, {})
return var
@FILTER_REGISTRY.register( @FILTER_REGISTRY.register(
"debounce", DebounceFilter, cv.positive_time_period_milliseconds "debounce", DebounceFilter, cv.positive_time_period_milliseconds
) )

View file

@ -373,6 +373,17 @@ void OrFilter::initialize(Sensor *parent, Filter *next) {
this->phi_.initialize(parent, nullptr); this->phi_.initialize(parent, nullptr);
} }
// TimeoutFilter
optional<float> TimeoutFilter::new_value(float value) {
this->set_timeout("timeout", this->time_period_, [this]() { this->output(NAN); });
this->output(value);
return {};
}
TimeoutFilter::TimeoutFilter(uint32_t time_period) : time_period_(time_period) {}
float TimeoutFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
// DebounceFilter // DebounceFilter
optional<float> DebounceFilter::new_value(float value) { optional<float> DebounceFilter::new_value(float value) {
this->set_timeout("debounce", this->time_period_, [this, value]() { this->output(value); }); this->set_timeout("debounce", this->time_period_, [this, value]() { this->output(value); });

View file

@ -313,6 +313,18 @@ class ThrottleFilter : public Filter {
uint32_t min_time_between_inputs_; uint32_t min_time_between_inputs_;
}; };
class TimeoutFilter : public Filter, public Component {
public:
explicit TimeoutFilter(uint32_t time_period);
optional<float> new_value(float value) override;
float get_setup_priority() const override;
protected:
uint32_t time_period_;
};
class DebounceFilter : public Filter, public Component { class DebounceFilter : public Filter, public Component {
public: public:
explicit DebounceFilter(uint32_t time_period); explicit DebounceFilter(uint32_t time_period);

View file

@ -86,6 +86,7 @@ sensor:
- delta: 100 - delta: 100
- throttle: 100ms - throttle: 100ms
- debounce: 500s - debounce: 500s
- timeout: 10min
- calibrate_linear: - calibrate_linear:
- 0 -> 0 - 0 -> 0
- 100 -> 100 - 100 -> 100