From b56c606523c1e34ade52ac0117aa42167ad02be4 Mon Sep 17 00:00:00 2001 From: Samuel Sieb Date: Wed, 9 Aug 2023 22:11:03 -0700 Subject: [PATCH] add value option to timeout filter (#5222) Co-authored-by: Samuel Sieb --- esphome/components/sensor/__init__.py | 14 +++++++++++--- esphome/components/sensor/filter.cpp | 4 ++-- esphome/components/sensor/filter.h | 4 +++- tests/test3.1.yaml | 3 +++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index bbcc730943..8f7d581b2d 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -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 diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index cd5ab5f9cd..6323023d50 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -375,13 +375,13 @@ void OrFilter::initialize(Sensor *parent, Filter *next) { // TimeoutFilter optional 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 diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 0141b73267..46aeefac56 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -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 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 { diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 42c1e1e1ab..46bc014204 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -87,6 +87,9 @@ sensor: - throttle: 100ms - debounce: 500s - timeout: 10min + - timeout: + timeout: 10min + value: 0 - calibrate_linear: method: exact datapoints: