mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 14:04:53 +01:00
Add timeout filter (#5104)
This commit is contained in:
parent
d57a5d1793
commit
c4b9065749
4 changed files with 34 additions and 0 deletions
|
@ -217,6 +217,7 @@ OffsetFilter = sensor_ns.class_("OffsetFilter", Filter)
|
|||
MultiplyFilter = sensor_ns.class_("MultiplyFilter", Filter)
|
||||
FilterOutValueFilter = sensor_ns.class_("FilterOutValueFilter", Filter)
|
||||
ThrottleFilter = sensor_ns.class_("ThrottleFilter", Filter)
|
||||
TimeoutFilter = sensor_ns.class_("TimeoutFilter", Filter, cg.Component)
|
||||
DebounceFilter = sensor_ns.class_("DebounceFilter", Filter, cg.Component)
|
||||
HeartbeatFilter = sensor_ns.class_("HeartbeatFilter", Filter, cg.Component)
|
||||
DeltaFilter = sensor_ns.class_("DeltaFilter", Filter)
|
||||
|
@ -536,6 +537,15 @@ async def heartbeat_filter_to_code(config, filter_id):
|
|||
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(
|
||||
"debounce", DebounceFilter, cv.positive_time_period_milliseconds
|
||||
)
|
||||
|
|
|
@ -373,6 +373,17 @@ void OrFilter::initialize(Sensor *parent, Filter *next) {
|
|||
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
|
||||
optional<float> DebounceFilter::new_value(float value) {
|
||||
this->set_timeout("debounce", this->time_period_, [this, value]() { this->output(value); });
|
||||
|
|
|
@ -313,6 +313,18 @@ class ThrottleFilter : public Filter {
|
|||
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 {
|
||||
public:
|
||||
explicit DebounceFilter(uint32_t time_period);
|
||||
|
|
|
@ -86,6 +86,7 @@ sensor:
|
|||
- delta: 100
|
||||
- throttle: 100ms
|
||||
- debounce: 500s
|
||||
- timeout: 10min
|
||||
- calibrate_linear:
|
||||
- 0 -> 0
|
||||
- 100 -> 100
|
||||
|
|
Loading…
Reference in a new issue