ESPHome 2025.6.3
Loading...
Searching...
No Matches
key_collector.cpp
Go to the documentation of this file.
1#include "key_collector.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace key_collector {
7
8static const char *const TAG = "key_collector";
9
11 : progress_trigger_(new Trigger<std::string, uint8_t>()),
12 result_trigger_(new Trigger<std::string, uint8_t, uint8_t>()),
13 timeout_trigger_(new Trigger<std::string, uint8_t>()) {}
14
16 if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
17 return;
18 this->timeout_trigger_->trigger(this->result_, this->start_key_);
19 this->clear();
20}
21
23 ESP_LOGCONFIG(TAG, "Key Collector:");
24 if (this->min_length_ > 0)
25 ESP_LOGCONFIG(TAG, " min length: %d", this->min_length_);
26 if (this->max_length_ > 0)
27 ESP_LOGCONFIG(TAG, " max length: %d", this->max_length_);
28 if (!this->back_keys_.empty())
29 ESP_LOGCONFIG(TAG, " erase keys '%s'", this->back_keys_.c_str());
30 if (!this->clear_keys_.empty())
31 ESP_LOGCONFIG(TAG, " clear keys '%s'", this->clear_keys_.c_str());
32 if (!this->start_keys_.empty())
33 ESP_LOGCONFIG(TAG, " start keys '%s'", this->start_keys_.c_str());
34 if (!this->end_keys_.empty()) {
35 ESP_LOGCONFIG(TAG,
36 " end keys '%s'\n"
37 " end key is required: %s",
38 this->end_keys_.c_str(), ONOFF(this->end_key_required_));
39 }
40 if (!this->allowed_keys_.empty())
41 ESP_LOGCONFIG(TAG, " allowed keys '%s'", this->allowed_keys_.c_str());
42 if (this->timeout_ > 0)
43 ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
44}
45
47 provider->add_on_key_callback([this](uint8_t key) { this->key_pressed_(key); });
48}
49
50void KeyCollector::set_enabled(bool enabled) {
51 this->enabled_ = enabled;
52 if (!enabled)
53 this->clear(false);
54}
55
56void KeyCollector::clear(bool progress_update) {
57 this->result_.clear();
58 this->start_key_ = 0;
59 if (progress_update)
60 this->progress_trigger_->trigger(this->result_, 0);
61}
62
63void KeyCollector::send_key(uint8_t key) { this->key_pressed_(key); }
64
65void KeyCollector::key_pressed_(uint8_t key) {
66 if (!this->enabled_)
67 return;
68 this->last_key_time_ = millis();
69 if (!this->start_keys_.empty() && !this->start_key_) {
70 if (this->start_keys_.find(key) != std::string::npos) {
71 this->start_key_ = key;
72 this->progress_trigger_->trigger(this->result_, this->start_key_);
73 }
74 return;
75 }
76 if (this->back_keys_.find(key) != std::string::npos) {
77 if (!this->result_.empty()) {
78 this->result_.pop_back();
79 this->progress_trigger_->trigger(this->result_, this->start_key_);
80 }
81 return;
82 }
83 if (this->clear_keys_.find(key) != std::string::npos) {
84 if (!this->result_.empty())
85 this->clear();
86 return;
87 }
88 if (this->end_keys_.find(key) != std::string::npos) {
89 if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
90 this->result_trigger_->trigger(this->result_, this->start_key_, key);
91 this->clear();
92 }
93 return;
94 }
95 if (!this->allowed_keys_.empty() && (this->allowed_keys_.find(key) == std::string::npos))
96 return;
97 if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_))
98 this->result_.push_back(key);
99 if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
100 this->result_trigger_->trigger(this->result_, this->start_key_, 0);
101 this->clear(false);
102 }
103 this->progress_trigger_->trigger(this->result_, this->start_key_);
104}
105
106} // namespace key_collector
107} // namespace esphome
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
void set_provider(key_provider::KeyProvider *provider)
Trigger< std::string, uint8_t > * progress_trigger_
Trigger< std::string, uint8_t, uint8_t > * result_trigger_
Trigger< std::string, uint8_t > * timeout_trigger_
void clear(bool progress_update=true)
interface for components that provide keypresses
void add_on_key_callback(std::function< void(uint8_t)> &&callback)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28