ESPHome 2025.5.0
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, " end keys '%s'", this->end_keys_.c_str());
36 ESP_LOGCONFIG(TAG, " end key is required: %s", ONOFF(this->end_key_required_));
37 }
38 if (!this->allowed_keys_.empty())
39 ESP_LOGCONFIG(TAG, " allowed keys '%s'", this->allowed_keys_.c_str());
40 if (this->timeout_ > 0)
41 ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
42}
43
45 provider->add_on_key_callback([this](uint8_t key) { this->key_pressed_(key); });
46}
47
48void KeyCollector::set_enabled(bool enabled) {
49 this->enabled_ = enabled;
50 if (!enabled)
51 this->clear(false);
52}
53
54void KeyCollector::clear(bool progress_update) {
55 this->result_.clear();
56 this->start_key_ = 0;
57 if (progress_update)
58 this->progress_trigger_->trigger(this->result_, 0);
59}
60
61void KeyCollector::send_key(uint8_t key) { this->key_pressed_(key); }
62
63void KeyCollector::key_pressed_(uint8_t key) {
64 if (!this->enabled_)
65 return;
66 this->last_key_time_ = millis();
67 if (!this->start_keys_.empty() && !this->start_key_) {
68 if (this->start_keys_.find(key) != std::string::npos) {
69 this->start_key_ = key;
70 this->progress_trigger_->trigger(this->result_, this->start_key_);
71 }
72 return;
73 }
74 if (this->back_keys_.find(key) != std::string::npos) {
75 if (!this->result_.empty()) {
76 this->result_.pop_back();
77 this->progress_trigger_->trigger(this->result_, this->start_key_);
78 }
79 return;
80 }
81 if (this->clear_keys_.find(key) != std::string::npos) {
82 if (!this->result_.empty())
83 this->clear();
84 return;
85 }
86 if (this->end_keys_.find(key) != std::string::npos) {
87 if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
88 this->result_trigger_->trigger(this->result_, this->start_key_, key);
89 this->clear();
90 }
91 return;
92 }
93 if (!this->allowed_keys_.empty() && (this->allowed_keys_.find(key) == std::string::npos))
94 return;
95 if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_))
96 this->result_.push_back(key);
97 if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
98 this->result_trigger_->trigger(this->result_, this->start_key_, 0);
99 this->clear(false);
100 }
101 this->progress_trigger_->trigger(this->result_, this->start_key_);
102}
103
104} // namespace key_collector
105} // 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:27