mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 20:53:50 +00:00
[binary_sensor] Optimize AutorepeatFilter with FixedVector (#11444)
This commit is contained in:
@@ -264,20 +264,31 @@ async def delayed_off_filter_to_code(config, filter_id):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
async def autorepeat_filter_to_code(config, filter_id):
|
async def autorepeat_filter_to_code(config, filter_id):
|
||||||
timings = []
|
|
||||||
if len(config) > 0:
|
if len(config) > 0:
|
||||||
timings.extend(
|
timings = [
|
||||||
(conf[CONF_DELAY], conf[CONF_TIME_OFF], conf[CONF_TIME_ON])
|
cg.StructInitializer(
|
||||||
for conf in config
|
cg.MockObj("AutorepeatFilterTiming", "esphome::binary_sensor::"),
|
||||||
)
|
("delay", conf[CONF_DELAY]),
|
||||||
else:
|
("time_off", conf[CONF_TIME_OFF]),
|
||||||
timings.append(
|
("time_on", conf[CONF_TIME_ON]),
|
||||||
(
|
|
||||||
cv.time_period_str_unit(DEFAULT_DELAY).total_milliseconds,
|
|
||||||
cv.time_period_str_unit(DEFAULT_TIME_OFF).total_milliseconds,
|
|
||||||
cv.time_period_str_unit(DEFAULT_TIME_ON).total_milliseconds,
|
|
||||||
)
|
)
|
||||||
)
|
for conf in config
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
timings = [
|
||||||
|
cg.StructInitializer(
|
||||||
|
cg.MockObj("AutorepeatFilterTiming", "esphome::binary_sensor::"),
|
||||||
|
("delay", cv.time_period_str_unit(DEFAULT_DELAY).total_milliseconds),
|
||||||
|
(
|
||||||
|
"time_off",
|
||||||
|
cv.time_period_str_unit(DEFAULT_TIME_OFF).total_milliseconds,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"time_on",
|
||||||
|
cv.time_period_str_unit(DEFAULT_TIME_ON).total_milliseconds,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]
|
||||||
var = cg.new_Pvariable(filter_id, timings)
|
var = cg.new_Pvariable(filter_id, timings)
|
||||||
await cg.register_component(var, {})
|
await cg.register_component(var, {})
|
||||||
return var
|
return var
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "filter.h"
|
#include "filter.h"
|
||||||
|
|
||||||
#include "binary_sensor.h"
|
#include "binary_sensor.h"
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
@@ -68,7 +67,7 @@ float DelayedOffFilter::get_setup_priority() const { return setup_priority::HARD
|
|||||||
|
|
||||||
optional<bool> InvertFilter::new_value(bool value) { return !value; }
|
optional<bool> InvertFilter::new_value(bool value) { return !value; }
|
||||||
|
|
||||||
AutorepeatFilter::AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings) : timings_(std::move(timings)) {}
|
AutorepeatFilter::AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings) : timings_(timings) {}
|
||||||
|
|
||||||
optional<bool> AutorepeatFilter::new_value(bool value) {
|
optional<bool> AutorepeatFilter::new_value(bool value) {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
namespace binary_sensor {
|
namespace binary_sensor {
|
||||||
@@ -82,11 +80,6 @@ class InvertFilter : public Filter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct AutorepeatFilterTiming {
|
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 delay;
|
||||||
uint32_t time_off;
|
uint32_t time_off;
|
||||||
uint32_t time_on;
|
uint32_t time_on;
|
||||||
@@ -94,7 +87,7 @@ struct AutorepeatFilterTiming {
|
|||||||
|
|
||||||
class AutorepeatFilter : public Filter, public Component {
|
class AutorepeatFilter : public Filter, public Component {
|
||||||
public:
|
public:
|
||||||
explicit AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings);
|
explicit AutorepeatFilter(std::initializer_list<AutorepeatFilterTiming> timings);
|
||||||
|
|
||||||
optional<bool> new_value(bool value) override;
|
optional<bool> new_value(bool value) override;
|
||||||
|
|
||||||
@@ -104,7 +97,7 @@ class AutorepeatFilter : public Filter, public Component {
|
|||||||
void next_timing_();
|
void next_timing_();
|
||||||
void next_value_(bool val);
|
void next_value_(bool val);
|
||||||
|
|
||||||
std::vector<AutorepeatFilterTiming> timings_;
|
FixedVector<AutorepeatFilterTiming> timings_;
|
||||||
uint8_t active_timing_{0};
|
uint8_t active_timing_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user