ESPHome 2026.10.0-dev
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
6
7static const char *const TAG = "key_collector";
8
10 if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
11 return;
12 this->timeout_callbacks_.call(this->result_, this->start_key_);
13 this->clear();
14}
15
17#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_CONFIG
18 ESP_LOGCONFIG(TAG, "Key Collector:");
19 if (this->min_length_ > 0) {
20 ESP_LOGCONFIG(TAG, " min length: %d", this->min_length_);
21 }
22 if (this->max_length_ > 0) {
23 ESP_LOGCONFIG(TAG, " max length: %d", this->max_length_);
24 }
25 if (!this->back_keys_.empty()) {
26 ESP_LOGCONFIG(TAG, " erase keys '%s'", this->back_keys_.c_str());
27 }
28 if (!this->clear_keys_.empty()) {
29 ESP_LOGCONFIG(TAG, " clear keys '%s'", this->clear_keys_.c_str());
30 }
31 if (!this->start_keys_.empty()) {
32 ESP_LOGCONFIG(TAG, " start keys '%s'", this->start_keys_.c_str());
33 }
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 }
43 if (this->timeout_ > 0) {
44 ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
45 }
46#endif
47}
48
50 provider->add_on_key_callback([this](uint8_t key) { this->send_key(key); });
51}
52
53void KeyCollector::set_enabled(bool enabled) {
54 this->enabled_ = enabled;
55 if (!enabled) {
56 this->clear(false);
57 }
58}
59
60void KeyCollector::clear(bool progress_update) {
61 auto had_state = !this->result_.empty() || this->start_key_ != 0;
62 this->result_.clear();
63 this->start_key_ = 0;
64 if (progress_update && had_state) {
65 this->progress_callbacks_.call(this->result_, 0);
66 }
67 this->disable_loop();
68}
69
70void KeyCollector::send_key(uint8_t key) {
71 if (!this->enabled_)
72 return;
73 this->last_key_time_ = millis();
74 if (!this->start_keys_.empty() && !this->start_key_) {
75 if (this->start_keys_.find(key) != std::string::npos) {
76 this->start_key_ = key;
77 this->progress_callbacks_.call(this->result_, this->start_key_);
78 }
79 return;
80 }
81 if (this->back_keys_.find(key) != std::string::npos) {
82 if (!this->result_.empty()) {
83 this->result_.pop_back();
84 this->progress_callbacks_.call(this->result_, this->start_key_);
85 }
86 return;
87 }
88 if (this->clear_keys_.find(key) != std::string::npos) {
89 this->clear();
90 return;
91 }
92 if (this->end_keys_.find(key) != std::string::npos) {
93 if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
94 this->result_callbacks_.call(this->result_, this->start_key_, key);
95 this->clear();
96 }
97 return;
98 }
99 if (!this->allowed_keys_.empty() && this->allowed_keys_.find(key) == std::string::npos)
100 return;
101 if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_)) {
102 if (this->result_.empty())
103 this->enable_loop();
104 this->result_.push_back(key);
105 }
106 if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
107 this->result_callbacks_.call(this->result_, this->start_key_, 0);
108 this->clear(false);
109 }
110 this->progress_callbacks_.call(this->result_, this->start_key_);
111}
112
113} // namespace esphome::key_collector
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
LazyCallbackManager< void(const std::string &, uint8_t)> timeout_callbacks_
void add_provider(key_provider::KeyProvider *provider)
LazyCallbackManager< void(const std::string &, uint8_t)> progress_callbacks_
void clear(bool progress_update=true)
LazyCallbackManager< void(const std::string &, uint8_t, uint8_t)> result_callbacks_
interface for components that provide keypresses
Definition key_provider.h:9
void add_on_key_callback(F &&callback)
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28