2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-07-04 02:25:48 +02:00
|
|
|
#include "esphome/core/automation.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/helpers.h"
|
|
|
|
|
2022-11-24 01:12:55 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
namespace esphome {
|
|
|
|
|
|
|
|
namespace binary_sensor {
|
|
|
|
|
|
|
|
class BinarySensor;
|
|
|
|
|
|
|
|
class Filter {
|
|
|
|
public:
|
|
|
|
virtual optional<bool> 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<bool> dedup_;
|
|
|
|
};
|
|
|
|
|
2019-08-29 16:09:37 +02:00
|
|
|
class DelayedOnOffFilter : public Filter, public Component {
|
|
|
|
public:
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
2023-07-04 02:25:48 +02:00
|
|
|
template<typename T> void set_on_delay(T delay) { this->on_delay_ = delay; }
|
|
|
|
template<typename T> void set_off_delay(T delay) { this->off_delay_ = delay; }
|
|
|
|
|
2019-08-29 16:09:37 +02:00
|
|
|
protected:
|
2023-07-04 02:25:48 +02:00
|
|
|
TemplatableValue<uint32_t> on_delay_{};
|
|
|
|
TemplatableValue<uint32_t> off_delay_{};
|
2019-08-29 16:09:37 +02:00
|
|
|
};
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
class DelayedOnFilter : public Filter, public Component {
|
|
|
|
public:
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
2023-07-04 02:25:48 +02:00
|
|
|
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
protected:
|
2023-07-04 02:25:48 +02:00
|
|
|
TemplatableValue<uint32_t> delay_{};
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class DelayedOffFilter : public Filter, public Component {
|
|
|
|
public:
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
2023-07-04 02:25:48 +02:00
|
|
|
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
protected:
|
2023-07-04 02:25:48 +02:00
|
|
|
TemplatableValue<uint32_t> delay_{};
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class InvertFilter : public Filter {
|
|
|
|
public:
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
};
|
|
|
|
|
2021-05-13 22:36:53 +02:00
|
|
|
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:
|
2021-06-10 13:04:40 +02:00
|
|
|
explicit AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings);
|
2021-05-13 22:36:53 +02:00
|
|
|
|
|
|
|
optional<bool> 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<AutorepeatFilterTiming> timings_;
|
|
|
|
uint8_t active_timing_{0};
|
|
|
|
};
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
class LambdaFilter : public Filter {
|
|
|
|
public:
|
2021-06-10 13:04:40 +02:00
|
|
|
explicit LambdaFilter(std::function<optional<bool>(bool)> f);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::function<optional<bool>(bool)> f_;
|
|
|
|
};
|
|
|
|
|
2023-12-22 03:33:29 +01:00
|
|
|
class SettleFilter : public Filter, public Component {
|
|
|
|
public:
|
|
|
|
optional<bool> new_value(bool value, bool is_initial) override;
|
|
|
|
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
|
|
|
|
template<typename T> void set_delay(T delay) { this->delay_ = delay; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TemplatableValue<uint32_t> delay_{};
|
|
|
|
bool steady_{true};
|
|
|
|
};
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
} // namespace binary_sensor
|
|
|
|
|
|
|
|
} // namespace esphome
|