mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Add ignore out of range option for clamp filter (#5455)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
841b24f744
commit
93056dead9
4 changed files with 23 additions and 6 deletions
|
@ -16,6 +16,7 @@ from esphome.const import (
|
|||
CONF_FROM,
|
||||
CONF_ICON,
|
||||
CONF_ID,
|
||||
CONF_IGNORE_OUT_OF_RANGE,
|
||||
CONF_ON_RAW_VALUE,
|
||||
CONF_ON_VALUE,
|
||||
CONF_ON_VALUE_RANGE,
|
||||
|
@ -688,6 +689,7 @@ CLAMP_SCHEMA = cv.All(
|
|||
{
|
||||
cv.Optional(CONF_MIN_VALUE, default="NaN"): cv.float_,
|
||||
cv.Optional(CONF_MAX_VALUE, default="NaN"): cv.float_,
|
||||
cv.Optional(CONF_IGNORE_OUT_OF_RANGE, default=False): cv.boolean,
|
||||
}
|
||||
),
|
||||
validate_clamp,
|
||||
|
@ -700,6 +702,7 @@ async def clamp_filter_to_code(config, filter_id):
|
|||
filter_id,
|
||||
config[CONF_MIN_VALUE],
|
||||
config[CONF_MAX_VALUE],
|
||||
config[CONF_IGNORE_OUT_OF_RANGE],
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -434,13 +434,25 @@ optional<float> CalibratePolynomialFilter::new_value(float value) {
|
|||
return res;
|
||||
}
|
||||
|
||||
ClampFilter::ClampFilter(float min, float max) : min_(min), max_(max) {}
|
||||
ClampFilter::ClampFilter(float min, float max, bool ignore_out_of_range)
|
||||
: min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {}
|
||||
optional<float> ClampFilter::new_value(float value) {
|
||||
if (std::isfinite(value)) {
|
||||
if (std::isfinite(this->min_) && value < this->min_)
|
||||
return this->min_;
|
||||
if (std::isfinite(this->max_) && value > this->max_)
|
||||
return this->max_;
|
||||
if (std::isfinite(this->min_) && value < this->min_) {
|
||||
if (this->ignore_out_of_range_) {
|
||||
return {};
|
||||
} else {
|
||||
return this->min_;
|
||||
}
|
||||
}
|
||||
|
||||
if (std::isfinite(this->max_) && value > this->max_) {
|
||||
if (this->ignore_out_of_range_) {
|
||||
return {};
|
||||
} else {
|
||||
return this->max_;
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
|
|
@ -411,12 +411,13 @@ class CalibratePolynomialFilter : public Filter {
|
|||
|
||||
class ClampFilter : public Filter {
|
||||
public:
|
||||
ClampFilter(float min, float max);
|
||||
ClampFilter(float min, float max, bool ignore_out_of_range);
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
protected:
|
||||
float min_{NAN};
|
||||
float max_{NAN};
|
||||
bool ignore_out_of_range_;
|
||||
};
|
||||
|
||||
class RoundFilter : public Filter {
|
||||
|
|
|
@ -335,6 +335,7 @@ CONF_IDLE_LEVEL = "idle_level"
|
|||
CONF_IDLE_TIME = "idle_time"
|
||||
CONF_IF = "if"
|
||||
CONF_IGNORE_EFUSE_MAC_CRC = "ignore_efuse_mac_crc"
|
||||
CONF_IGNORE_OUT_OF_RANGE = "ignore_out_of_range"
|
||||
CONF_IGNORE_STRAPPING_WARNING = "ignore_strapping_warning"
|
||||
CONF_IIR_FILTER = "iir_filter"
|
||||
CONF_ILLUMINANCE = "illuminance"
|
||||
|
|
Loading…
Reference in a new issue