1
0
mirror of https://github.com/esphome/esphome.git synced 2025-01-18 12:05:41 +00:00

add on_key trigger to matrix_keypad (#7830)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2024-11-25 14:21:47 -10:00 committed by GitHub
parent a70cee1dc1
commit d9d368d38e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 75 additions and 55 deletions

View File

@ -1,8 +1,8 @@
from esphome import automation, pins
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import key_provider from esphome.components import key_provider
from esphome.const import CONF_ID, CONF_PIN import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_ON_KEY, CONF_PIN, CONF_TRIGGER_ID
CODEOWNERS = ["@ssieb"] CODEOWNERS = ["@ssieb"]
@ -14,6 +14,9 @@ matrix_keypad_ns = cg.esphome_ns.namespace("matrix_keypad")
MatrixKeypad = matrix_keypad_ns.class_( MatrixKeypad = matrix_keypad_ns.class_(
"MatrixKeypad", key_provider.KeyProvider, cg.Component "MatrixKeypad", key_provider.KeyProvider, cg.Component
) )
MatrixKeyTrigger = matrix_keypad_ns.class_(
"MatrixKeyTrigger", automation.Trigger.template(cg.uint8)
)
CONF_KEYPAD_ID = "keypad_id" CONF_KEYPAD_ID = "keypad_id"
CONF_ROWS = "rows" CONF_ROWS = "rows"
@ -47,6 +50,11 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_DEBOUNCE_TIME, default=1): cv.int_range(min=1, max=100), cv.Optional(CONF_DEBOUNCE_TIME, default=1): cv.int_range(min=1, max=100),
cv.Optional(CONF_HAS_DIODES): cv.boolean, cv.Optional(CONF_HAS_DIODES): cv.boolean,
cv.Optional(CONF_HAS_PULLDOWNS): cv.boolean, cv.Optional(CONF_HAS_PULLDOWNS): cv.boolean,
cv.Optional(CONF_ON_KEY): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MatrixKeyTrigger),
}
),
} }
), ),
check_keys, check_keys,
@ -73,3 +81,7 @@ async def to_code(config):
cg.add(var.set_has_diodes(config[CONF_HAS_DIODES])) cg.add(var.set_has_diodes(config[CONF_HAS_DIODES]))
if CONF_HAS_PULLDOWNS in config: if CONF_HAS_PULLDOWNS in config:
cg.add(var.set_has_pulldowns(config[CONF_HAS_PULLDOWNS])) cg.add(var.set_has_pulldowns(config[CONF_HAS_PULLDOWNS]))
for conf in config.get(CONF_ON_KEY, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_key_trigger(trigger))
await automation.build_automation(trigger, [(cg.uint8, "x")], conf)

View File

@ -86,6 +86,8 @@ void MatrixKeypad::loop() {
if (!this->keys_.empty()) { if (!this->keys_.empty()) {
uint8_t keycode = this->keys_[key]; uint8_t keycode = this->keys_[key];
ESP_LOGD(TAG, "key '%c' pressed", keycode); ESP_LOGD(TAG, "key '%c' pressed", keycode);
for (auto &trigger : this->key_triggers_)
trigger->trigger(keycode);
for (auto &listener : this->listeners_) for (auto &listener : this->listeners_)
listener->key_pressed(keycode); listener->key_pressed(keycode);
this->send_key_(keycode); this->send_key_(keycode);
@ -107,5 +109,7 @@ void MatrixKeypad::dump_config() {
void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); } void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
void MatrixKeypad::register_key_trigger(MatrixKeyTrigger *trig) { this->key_triggers_.push_back(trig); }
} // namespace matrix_keypad } // namespace matrix_keypad
} // namespace esphome } // namespace esphome

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "esphome/components/key_provider/key_provider.h" #include "esphome/components/key_provider/key_provider.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
@ -18,6 +19,8 @@ class MatrixKeypadListener {
virtual void key_released(uint8_t key){}; virtual void key_released(uint8_t key){};
}; };
class MatrixKeyTrigger : public Trigger<uint8_t> {};
class MatrixKeypad : public key_provider::KeyProvider, public Component { class MatrixKeypad : public key_provider::KeyProvider, public Component {
public: public:
void setup() override; void setup() override;
@ -31,6 +34,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
void set_has_pulldowns(int has_pulldowns) { has_pulldowns_ = has_pulldowns; }; void set_has_pulldowns(int has_pulldowns) { has_pulldowns_ = has_pulldowns; };
void register_listener(MatrixKeypadListener *listener); void register_listener(MatrixKeypadListener *listener);
void register_key_trigger(MatrixKeyTrigger *trig);
protected: protected:
std::vector<GPIOPin *> rows_; std::vector<GPIOPin *> rows_;
@ -42,6 +46,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
int pressed_key_ = -1; int pressed_key_ = -1;
std::vector<MatrixKeypadListener *> listeners_{}; std::vector<MatrixKeypadListener *> listeners_{};
std::vector<MatrixKeyTrigger *> key_triggers_;
}; };
} // namespace matrix_keypad } // namespace matrix_keypad

View File

@ -1,8 +1,8 @@
from esphome import automation, pins
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins, automation
from esphome.components import key_provider from esphome.components import key_provider
from esphome.const import CONF_ID, CONF_ON_TAG, CONF_TRIGGER_ID import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_ON_KEY, CONF_ON_TAG, CONF_TRIGGER_ID
CODEOWNERS = ["@ssieb"] CODEOWNERS = ["@ssieb"]
@ -25,7 +25,6 @@ WiegandKeyTrigger = wiegand_ns.class_(
CONF_D0 = "d0" CONF_D0 = "d0"
CONF_D1 = "d1" CONF_D1 = "d1"
CONF_ON_KEY = "on_key"
CONF_ON_RAW = "on_raw" CONF_ON_RAW = "on_raw"
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(

View File

@ -575,6 +575,7 @@ CONF_ON_FINGER_SCAN_UNMATCHED = "on_finger_scan_unmatched"
CONF_ON_FINISHED_WRITE = "on_finished_write" CONF_ON_FINISHED_WRITE = "on_finished_write"
CONF_ON_IDLE = "on_idle" CONF_ON_IDLE = "on_idle"
CONF_ON_JSON_MESSAGE = "on_json_message" CONF_ON_JSON_MESSAGE = "on_json_message"
CONF_ON_KEY = "on_key"
CONF_ON_LOCK = "on_lock" CONF_ON_LOCK = "on_lock"
CONF_ON_LOOP = "on_loop" CONF_ON_LOOP = "on_loop"
CONF_ON_MESSAGE = "on_message" CONF_ON_MESSAGE = "on_message"

View File

@ -0,0 +1,8 @@
binary_sensor:
- platform: matrix_keypad
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 15 - pin: 15
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 4 - pin: 4
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 4 - pin: 4
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 15 - pin: 15
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -0,0 +1,15 @@
packages:
common: !include common.yaml
matrix_keypad:
id: keypad
rows:
- pin: 10
- pin: 11
columns:
- pin: 12
- pin: 13
keys: "1234"
has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 15 - pin: 15
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);

View File

@ -1,11 +1,5 @@
binary_sensor: packages:
- platform: matrix_keypad common: !include common.yaml
id: key4
row: 1
col: 1
- platform: matrix_keypad
id: key1
key: 1
matrix_keypad: matrix_keypad:
id: keypad id: keypad
@ -17,3 +11,5 @@ matrix_keypad:
- pin: 4 - pin: 4
keys: "1234" keys: "1234"
has_pulldowns: true has_pulldowns: true
on_key:
- lambda: ESP_LOGI("KEY", "key %d pressed", x);