ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
key_collector.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
8
10
11class KeyCollector final : public Component {
12 public:
13 void loop() override;
14 void dump_config() override;
16 void set_min_length(uint16_t min_length) { this->min_length_ = min_length; };
17 void set_max_length(uint16_t max_length) { this->max_length_ = max_length; };
18 // The key setters keep the pointer; the strings must outlive the component (codegen passes literals)
19 void set_start_keys(const char *start_keys) { this->start_keys_ = StringRef(start_keys); };
20 void set_end_keys(const char *end_keys) { this->end_keys_ = StringRef(end_keys); };
21 void set_end_key_required(bool end_key_required) { this->end_key_required_ = end_key_required; };
22 void set_back_keys(const char *back_keys) { this->back_keys_ = StringRef(back_keys); };
23 void set_clear_keys(const char *clear_keys) { this->clear_keys_ = StringRef(clear_keys); };
24 void set_allowed_keys(const char *allowed_keys) { this->allowed_keys_ = StringRef(allowed_keys); };
25 template<typename F> void add_on_progress_callback(F &&callback) {
26 this->progress_callbacks_.add(std::forward<F>(callback));
27 }
28 template<typename F> void add_on_result_callback(F &&callback) {
29 this->result_callbacks_.add(std::forward<F>(callback));
30 }
31 template<typename F> void add_on_timeout_callback(F &&callback) {
32 this->timeout_callbacks_.add(std::forward<F>(callback));
33 }
34 void set_timeout(int timeout) { this->timeout_ = timeout; };
35 void set_enabled(bool enabled);
36
37 void clear(bool progress_update = true);
38 void send_key(uint8_t key);
39
40 protected:
41 uint16_t min_length_{0};
42 uint16_t max_length_{0};
45 bool end_key_required_{false};
49 std::string result_;
50 uint8_t start_key_{0};
51 LazyCallbackManager<void(const std::string &, uint8_t)> progress_callbacks_;
52 LazyCallbackManager<void(const std::string &, uint8_t, uint8_t)> result_callbacks_;
53 LazyCallbackManager<void(const std::string &, uint8_t)> timeout_callbacks_;
56 bool enabled_{};
57};
58
59} // namespace esphome::key_collector
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
void add_on_result_callback(F &&callback)
void add_on_timeout_callback(F &&callback)
LazyCallbackManager< void(const std::string &, uint8_t)> timeout_callbacks_
void set_start_keys(const char *start_keys)
void set_max_length(uint16_t max_length)
void set_clear_keys(const char *clear_keys)
void set_end_key_required(bool end_key_required)
void add_on_progress_callback(F &&callback)
void set_end_keys(const char *end_keys)
void set_min_length(uint16_t min_length)
void add_provider(key_provider::KeyProvider *provider)
void set_back_keys(const char *back_keys)
void set_allowed_keys(const char *allowed_keys)
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
const char * key
static void uint32_t