mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 14:43:51 +00:00
[sensor] Add optimistic option to heartbeat filter (#10993)
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
@@ -28,6 +28,8 @@ from esphome.const import (
|
||||
CONF_ON_RAW_VALUE,
|
||||
CONF_ON_VALUE,
|
||||
CONF_ON_VALUE_RANGE,
|
||||
CONF_OPTIMISTIC,
|
||||
CONF_PERIOD,
|
||||
CONF_QUANTILE,
|
||||
CONF_SEND_EVERY,
|
||||
CONF_SEND_FIRST_AT,
|
||||
@@ -644,10 +646,29 @@ async def throttle_with_priority_filter_to_code(config, filter_id):
|
||||
return cg.new_Pvariable(filter_id, config[CONF_TIMEOUT], template_)
|
||||
|
||||
|
||||
HEARTBEAT_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_PERIOD): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@FILTER_REGISTRY.register(
|
||||
"heartbeat", HeartbeatFilter, cv.positive_time_period_milliseconds
|
||||
"heartbeat",
|
||||
HeartbeatFilter,
|
||||
cv.Any(
|
||||
cv.positive_time_period_milliseconds,
|
||||
HEARTBEAT_SCHEMA,
|
||||
),
|
||||
)
|
||||
async def heartbeat_filter_to_code(config, filter_id):
|
||||
if isinstance(config, dict):
|
||||
var = cg.new_Pvariable(filter_id, config[CONF_PERIOD])
|
||||
await cg.register_component(var, {})
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
return var
|
||||
|
||||
var = cg.new_Pvariable(filter_id, config)
|
||||
await cg.register_component(var, {})
|
||||
return var
|
||||
|
||||
@@ -372,8 +372,12 @@ optional<float> HeartbeatFilter::new_value(float value) {
|
||||
this->last_input_ = value;
|
||||
this->has_value_ = true;
|
||||
|
||||
if (this->optimistic_) {
|
||||
return value;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void HeartbeatFilter::setup() {
|
||||
this->set_interval("heartbeat", this->time_period_, [this]() {
|
||||
ESP_LOGVV(TAG, "HeartbeatFilter(%p)::interval(has_value=%s, last_input=%f)", this, YESNO(this->has_value_),
|
||||
@@ -384,6 +388,7 @@ void HeartbeatFilter::setup() {
|
||||
this->output(this->last_input_);
|
||||
});
|
||||
}
|
||||
|
||||
float HeartbeatFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
CalibrateLinearFilter::CalibrateLinearFilter(std::initializer_list<std::array<float, 3>> linear_functions)
|
||||
|
||||
@@ -396,15 +396,16 @@ class HeartbeatFilter : public Filter, public Component {
|
||||
explicit HeartbeatFilter(uint32_t time_period);
|
||||
|
||||
void setup() override;
|
||||
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
|
||||
protected:
|
||||
uint32_t time_period_;
|
||||
float last_input_;
|
||||
bool has_value_{false};
|
||||
bool optimistic_{false};
|
||||
};
|
||||
|
||||
class DeltaFilter : public Filter {
|
||||
|
||||
@@ -101,6 +101,9 @@ sensor:
|
||||
- filter_out: 10
|
||||
- filter_out: !lambda return NAN;
|
||||
- heartbeat: 5s
|
||||
- heartbeat:
|
||||
period: 5s
|
||||
optimistic: true
|
||||
- lambda: return x * (9.0/5.0) + 32.0;
|
||||
- max:
|
||||
window_size: 10
|
||||
|
||||
Reference in New Issue
Block a user