#pragma once #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include namespace esphome { namespace binary_sensor { class BinarySensor; class Filter { public: virtual optional new_value(bool value, bool is_initial) = 0; void input(bool value, bool is_initial); void output(bool value, bool is_initial); protected: friend BinarySensor; Filter *next_{nullptr}; BinarySensor *parent_{nullptr}; Deduplicator dedup_; }; class DelayedOnOffFilter : public Filter, public Component { public: optional new_value(bool value, bool is_initial) override; float get_setup_priority() const override; template void set_on_delay(T delay) { this->on_delay_ = delay; } template void set_off_delay(T delay) { this->off_delay_ = delay; } protected: TemplatableValue on_delay_{}; TemplatableValue off_delay_{}; }; class DelayedOnFilter : public Filter, public Component { public: optional new_value(bool value, bool is_initial) override; float get_setup_priority() const override; template void set_delay(T delay) { this->delay_ = delay; } protected: TemplatableValue delay_{}; }; class DelayedOffFilter : public Filter, public Component { public: optional new_value(bool value, bool is_initial) override; float get_setup_priority() const override; template void set_delay(T delay) { this->delay_ = delay; } protected: TemplatableValue delay_{}; }; class InvertFilter : public Filter { public: optional new_value(bool value, bool is_initial) override; }; struct AutorepeatFilterTiming { AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on) { this->delay = delay; this->time_off = off; this->time_on = on; } uint32_t delay; uint32_t time_off; uint32_t time_on; }; class AutorepeatFilter : public Filter, public Component { public: explicit AutorepeatFilter(std::vector timings); optional new_value(bool value, bool is_initial) override; float get_setup_priority() const override; protected: void next_timing_(); void next_value_(bool val); std::vector timings_; uint8_t active_timing_{0}; }; class LambdaFilter : public Filter { public: explicit LambdaFilter(std::function(bool)> f); optional new_value(bool value, bool is_initial) override; protected: std::function(bool)> f_; }; class SettleFilter : public Filter, public Component { public: optional new_value(bool value, bool is_initial) override; float get_setup_priority() const override; template void set_delay(T delay) { this->delay_ = delay; } protected: TemplatableValue delay_{}; bool steady_{true}; }; } // namespace binary_sensor } // namespace esphome