ESPHome 2026.2.4
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 if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
12 return;
13 this->timeout_callbacks_.call(this->result_, this->start_key_);
14 this->clear();
15}
16
18 ESP_LOGCONFIG(TAG, "Key Collector:");
19 if (this->min_length_ > 0)
20 ESP_LOGCONFIG(TAG, " min length: %d", this->min_length_);
21 if (this->max_length_ > 0)
22 ESP_LOGCONFIG(TAG, " max length: %d", this->max_length_);
23 if (!this->back_keys_.empty())
24 ESP_LOGCONFIG(TAG, " erase keys '%s'", this->back_keys_.c_str());
25 if (!this->clear_keys_.empty())
26 ESP_LOGCONFIG(TAG, " clear keys '%s'", this->clear_keys_.c_str());
27 if (!this->start_keys_.empty())
28 ESP_LOGCONFIG(TAG, " start keys '%s'", this->start_keys_.c_str());
29 if (!this->end_keys_.empty()) {
30 ESP_LOGCONFIG(TAG,
31 " end keys '%s'\n"
32 " end key is required: %s",
33 this->end_keys_.c_str(), ONOFF(this->end_key_required_));
34 }
35 if (!this->allowed_keys_.empty())
36 ESP_LOGCONFIG(TAG, " allowed keys '%s'", this->allowed_keys_.c_str());
37 if (this->timeout_ > 0)
38 ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
39}
40
42 provider->add_on_key_callback([this](uint8_t key) { this->send_key(key); });
43}
44
45void KeyCollector::set_enabled(bool enabled) {
46 this->enabled_ = enabled;
47 if (!enabled) {
48 this->clear(false);
49 }
50}
51
52void KeyCollector::clear(bool progress_update) {
53 auto had_state = !this->result_.empty() || this->start_key_ != 0;
54 this->result_.clear();
55 this->start_key_ = 0;
56 if (progress_update && had_state) {
57 this->progress_callbacks_.call(this->result_, 0);
58 }
59 this->disable_loop();
60}
61
62void KeyCollector::send_key(uint8_t key) {
63 if (!this->enabled_)
64 return;
65 this->last_key_time_ = millis();
66 if (!this->start_keys_.empty() && !this->start_key_) {
67 if (this->start_keys_.find(key) != std::string::npos) {
68 this->start_key_ = key;
69 this->progress_callbacks_.call(this->result_, this->start_key_);
70 }
71 return;
72 }
73 if (this->back_keys_.find(key) != std::string::npos) {
74 if (!this->result_.empty()) {
75 this->result_.pop_back();
76 this->progress_callbacks_.call(this->result_, this->start_key_);
77 }
78 return;
79 }
80 if (this->clear_keys_.find(key) != std::string::npos) {
81 this->clear();
82 return;
83 }
84 if (this->end_keys_.find(key) != std::string::npos) {
85 if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
86 this->result_callbacks_.call(this->result_, this->start_key_, key);
87 this->clear();
88 }
89 return;
90 }
91 if (!this->allowed_keys_.empty() && this->allowed_keys_.find(key) == std::string::npos)
92 return;
93 if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_)) {
94 if (this->result_.empty())
95 this->enable_loop();
96 this->result_.push_back(key);
97 }
98 if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
99 this->result_callbacks_.call(this->result_, this->start_key_, 0);
100 this->clear(false);
101 }
102 this->progress_callbacks_.call(this->result_, this->start_key_);
103}
104
105} // namespace key_collector
106} // namespace esphome
void enable_loop()
Enable this component's loop.
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
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:25