mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 05:03:48 +00:00
[binary_sensor] Optimize MultiClickTrigger with FixedVector
This commit is contained in:
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -92,8 +92,8 @@ class DoubleClickTrigger : public Trigger<> {
|
|||||||
|
|
||||||
class MultiClickTrigger : public Trigger<>, public Component {
|
class MultiClickTrigger : public Trigger<>, public Component {
|
||||||
public:
|
public:
|
||||||
explicit MultiClickTrigger(BinarySensor *parent, std::vector<MultiClickTriggerEvent> timing)
|
explicit MultiClickTrigger(BinarySensor *parent, std::initializer_list<MultiClickTriggerEvent> timing)
|
||||||
: parent_(parent), timing_(std::move(timing)) {}
|
: parent_(parent), timing_(timing) {}
|
||||||
|
|
||||||
void setup() override {
|
void setup() override {
|
||||||
this->last_state_ = this->parent_->get_state_default(false);
|
this->last_state_ = this->parent_->get_state_default(false);
|
||||||
@@ -115,7 +115,7 @@ class MultiClickTrigger : public Trigger<>, public Component {
|
|||||||
void trigger_();
|
void trigger_();
|
||||||
|
|
||||||
BinarySensor *parent_;
|
BinarySensor *parent_;
|
||||||
std::vector<MultiClickTriggerEvent> timing_;
|
FixedVector<MultiClickTriggerEvent> timing_;
|
||||||
uint32_t invalid_cooldown_{1000};
|
uint32_t invalid_cooldown_{1000};
|
||||||
optional<size_t> at_index_{};
|
optional<size_t> at_index_{};
|
||||||
bool last_state_{false};
|
bool last_state_{false};
|
||||||
|
|||||||
@@ -70,3 +70,69 @@ binary_sensor:
|
|||||||
- delay: 10s
|
- delay: 10s
|
||||||
time_off: 200ms
|
time_off: 200ms
|
||||||
time_on: 800ms
|
time_on: 800ms
|
||||||
|
|
||||||
|
# Test on_multi_click with single click
|
||||||
|
- platform: template
|
||||||
|
id: multi_click_single
|
||||||
|
name: "Multi Click Single"
|
||||||
|
on_multi_click:
|
||||||
|
- timing:
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
then:
|
||||||
|
- logger.log: "Single click detected"
|
||||||
|
|
||||||
|
# Test on_multi_click with double click
|
||||||
|
- platform: template
|
||||||
|
id: multi_click_double
|
||||||
|
name: "Multi Click Double"
|
||||||
|
on_multi_click:
|
||||||
|
- timing:
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: false
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
then:
|
||||||
|
- logger.log: "Double click detected"
|
||||||
|
|
||||||
|
# Test on_multi_click with complex pattern (5 events)
|
||||||
|
- platform: template
|
||||||
|
id: multi_click_complex
|
||||||
|
name: "Multi Click Complex"
|
||||||
|
on_multi_click:
|
||||||
|
- timing:
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: false
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: false
|
||||||
|
min_length: 50ms
|
||||||
|
max_length: 350ms
|
||||||
|
- state: true
|
||||||
|
min_length: 50ms
|
||||||
|
then:
|
||||||
|
- logger.log: "Complex pattern detected"
|
||||||
|
|
||||||
|
# Test on_multi_click with custom invalid_cooldown
|
||||||
|
- platform: template
|
||||||
|
id: multi_click_cooldown
|
||||||
|
name: "Multi Click Cooldown"
|
||||||
|
on_multi_click:
|
||||||
|
- timing:
|
||||||
|
- state: true
|
||||||
|
min_length: 100ms
|
||||||
|
max_length: 500ms
|
||||||
|
invalid_cooldown: 2s
|
||||||
|
then:
|
||||||
|
- logger.log: "Click with custom cooldown"
|
||||||
|
|||||||
Reference in New Issue
Block a user