add value option to timeout filter (#5222)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2023-08-09 22:11:03 -07:00 committed by GitHub
parent f457269a68
commit b56c606523
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 6 deletions

View file

@ -23,6 +23,7 @@ from esphome.const import (
CONF_SEND_EVERY,
CONF_SEND_FIRST_AT,
CONF_STATE_CLASS,
CONF_TIMEOUT,
CONF_TO,
CONF_TRIGGER_ID,
CONF_TYPE,
@ -543,11 +544,18 @@ async def heartbeat_filter_to_code(config, filter_id):
return var
@FILTER_REGISTRY.register(
"timeout", TimeoutFilter, cv.positive_time_period_milliseconds
TIMEOUT_SCHEMA = cv.maybe_simple_value(
{
cv.Required(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
cv.Optional(CONF_VALUE, default="nan"): cv.float_,
},
key=CONF_TIMEOUT,
)
@FILTER_REGISTRY.register("timeout", TimeoutFilter, TIMEOUT_SCHEMA)
async def timeout_filter_to_code(config, filter_id):
var = cg.new_Pvariable(filter_id, config)
var = cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], config[CONF_VALUE])
await cg.register_component(var, {})
return var

View file

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

View file

@ -315,7 +315,8 @@ class ThrottleFilter : public Filter {
class TimeoutFilter : public Filter, public Component {
public:
explicit TimeoutFilter(uint32_t time_period);
explicit TimeoutFilter(uint32_t time_period, float new_value);
void set_value(float new_value) { this->value_ = new_value; }
optional<float> new_value(float value) override;
@ -323,6 +324,7 @@ class TimeoutFilter : public Filter, public Component {
protected:
uint32_t time_period_;
float value_;
};
class DebounceFilter : public Filter, public Component {

View file

@ -87,6 +87,9 @@ sensor:
- throttle: 100ms
- debounce: 500s
- timeout: 10min
- timeout:
timeout: 10min
value: 0
- calibrate_linear:
method: exact
datapoints: