ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
key_collector.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
7
8namespace esphome {
9namespace key_collector {
10
11class KeyCollector : 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 void set_start_keys(std::string start_keys) { this->start_keys_ = std::move(start_keys); };
19 void set_end_keys(std::string end_keys) { this->end_keys_ = std::move(end_keys); };
20 void set_end_key_required(bool end_key_required) { this->end_key_required_ = end_key_required; };
21 void set_back_keys(std::string back_keys) { this->back_keys_ = std::move(back_keys); };
22 void set_clear_keys(std::string clear_keys) { this->clear_keys_ = std::move(clear_keys); };
23 void set_allowed_keys(std::string allowed_keys) { this->allowed_keys_ = std::move(allowed_keys); };
24 void add_on_progress_callback(std::function<void(const std::string &, uint8_t)> &&callback) {
25 this->progress_callbacks_.add(std::move(callback));
26 }
27 void add_on_result_callback(std::function<void(const std::string &, uint8_t, uint8_t)> &&callback) {
28 this->result_callbacks_.add(std::move(callback));
29 }
30 void add_on_timeout_callback(std::function<void(const std::string &, uint8_t)> &&callback) {
31 this->timeout_callbacks_.add(std::move(callback));
32 }
33 void set_timeout(int timeout) { this->timeout_ = timeout; };
34 void set_enabled(bool enabled);
35
36 void clear(bool progress_update = true);
37 void send_key(uint8_t key);
38
39 protected:
40 uint16_t min_length_{0};
41 uint16_t max_length_{0};
42 std::string start_keys_;
43 std::string end_keys_;
44 bool end_key_required_{false};
45 std::string back_keys_;
46 std::string clear_keys_;
47 std::string allowed_keys_;
48 std::string result_;
49 uint8_t start_key_{0};
50 LazyCallbackManager<void(const std::string &, uint8_t)> progress_callbacks_;
51 LazyCallbackManager<void(const std::string &, uint8_t, uint8_t)> result_callbacks_;
52 LazyCallbackManager<void(const std::string &, uint8_t)> timeout_callbacks_;
53 uint32_t last_key_time_{};
54 uint32_t timeout_{0};
55 bool enabled_{};
56};
57
58template<typename... Ts> class EnableAction : public Action<Ts...>, public Parented<KeyCollector> {
59 void play(const Ts &...x) override { this->parent_->set_enabled(true); }
60};
61
62template<typename... Ts> class DisableAction : public Action<Ts...>, public Parented<KeyCollector> {
63 void play(const Ts &...x) override { this->parent_->set_enabled(false); }
64};
65
66} // namespace key_collector
67} // namespace esphome
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
LazyCallbackManager< void(const std::string &, uint8_t)> timeout_callbacks_
void set_start_keys(std::string start_keys)
void set_max_length(uint16_t max_length)
void set_end_key_required(bool end_key_required)
void set_back_keys(std::string back_keys)
void add_on_timeout_callback(std::function< void(const std::string &, uint8_t)> &&callback)
void add_on_result_callback(std::function< void(const std::string &, uint8_t, uint8_t)> &&callback)
void set_min_length(uint16_t min_length)
void set_clear_keys(std::string clear_keys)
void add_provider(key_provider::KeyProvider *provider)
void set_end_keys(std::string end_keys)
void add_on_progress_callback(std::function< void(const std::string &, uint8_t)> &&callback)
void set_allowed_keys(std::string 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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5