mirror of
https://github.com/esphome/esphome.git
synced 2025-01-04 03:41:45 +01:00
add on_key trigger to matrix_keypad (#7830)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
a70cee1dc1
commit
d9d368d38e
13 changed files with 75 additions and 55 deletions
|
@ -1,8 +1,8 @@
|
|||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
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"]
|
||||
|
||||
|
@ -14,6 +14,9 @@ matrix_keypad_ns = cg.esphome_ns.namespace("matrix_keypad")
|
|||
MatrixKeypad = matrix_keypad_ns.class_(
|
||||
"MatrixKeypad", key_provider.KeyProvider, cg.Component
|
||||
)
|
||||
MatrixKeyTrigger = matrix_keypad_ns.class_(
|
||||
"MatrixKeyTrigger", automation.Trigger.template(cg.uint8)
|
||||
)
|
||||
|
||||
CONF_KEYPAD_ID = "keypad_id"
|
||||
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_HAS_DIODES): 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,
|
||||
|
@ -73,3 +81,7 @@ async def to_code(config):
|
|||
cg.add(var.set_has_diodes(config[CONF_HAS_DIODES]))
|
||||
if CONF_HAS_PULLDOWNS in config:
|
||||
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)
|
||||
|
|
|
@ -86,6 +86,8 @@ void MatrixKeypad::loop() {
|
|||
if (!this->keys_.empty()) {
|
||||
uint8_t keycode = this->keys_[key];
|
||||
ESP_LOGD(TAG, "key '%c' pressed", keycode);
|
||||
for (auto &trigger : this->key_triggers_)
|
||||
trigger->trigger(keycode);
|
||||
for (auto &listener : this->listeners_)
|
||||
listener->key_pressed(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_key_trigger(MatrixKeyTrigger *trig) { this->key_triggers_.push_back(trig); }
|
||||
|
||||
} // namespace matrix_keypad
|
||||
} // namespace esphome
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/components/key_provider/key_provider.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
@ -18,6 +19,8 @@ class MatrixKeypadListener {
|
|||
virtual void key_released(uint8_t key){};
|
||||
};
|
||||
|
||||
class MatrixKeyTrigger : public Trigger<uint8_t> {};
|
||||
|
||||
class MatrixKeypad : public key_provider::KeyProvider, public Component {
|
||||
public:
|
||||
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 register_listener(MatrixKeypadListener *listener);
|
||||
void register_key_trigger(MatrixKeyTrigger *trig);
|
||||
|
||||
protected:
|
||||
std::vector<GPIOPin *> rows_;
|
||||
|
@ -42,6 +46,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
|
|||
int pressed_key_ = -1;
|
||||
|
||||
std::vector<MatrixKeypadListener *> listeners_{};
|
||||
std::vector<MatrixKeyTrigger *> key_triggers_;
|
||||
};
|
||||
|
||||
} // namespace matrix_keypad
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins, automation
|
||||
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"]
|
||||
|
||||
|
@ -25,7 +25,6 @@ WiegandKeyTrigger = wiegand_ns.class_(
|
|||
|
||||
CONF_D0 = "d0"
|
||||
CONF_D1 = "d1"
|
||||
CONF_ON_KEY = "on_key"
|
||||
CONF_ON_RAW = "on_raw"
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
|
|
|
@ -575,6 +575,7 @@ CONF_ON_FINGER_SCAN_UNMATCHED = "on_finger_scan_unmatched"
|
|||
CONF_ON_FINISHED_WRITE = "on_finished_write"
|
||||
CONF_ON_IDLE = "on_idle"
|
||||
CONF_ON_JSON_MESSAGE = "on_json_message"
|
||||
CONF_ON_KEY = "on_key"
|
||||
CONF_ON_LOCK = "on_lock"
|
||||
CONF_ON_LOOP = "on_loop"
|
||||
CONF_ON_MESSAGE = "on_message"
|
||||
|
|
8
tests/components/matrix_keypad/common.yaml
Normal file
8
tests/components/matrix_keypad/common.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 15
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 4
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 4
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 15
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
15
tests/components/matrix_keypad/test.esp32-s3-idf.yaml
Normal file
15
tests/components/matrix_keypad/test.esp32-s3-idf.yaml
Normal 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);
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 15
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
binary_sensor:
|
||||
- platform: matrix_keypad
|
||||
id: key4
|
||||
row: 1
|
||||
col: 1
|
||||
- platform: matrix_keypad
|
||||
id: key1
|
||||
key: 1
|
||||
packages:
|
||||
common: !include common.yaml
|
||||
|
||||
matrix_keypad:
|
||||
id: keypad
|
||||
|
@ -17,3 +11,5 @@ matrix_keypad:
|
|||
- pin: 4
|
||||
keys: "1234"
|
||||
has_pulldowns: true
|
||||
on_key:
|
||||
- lambda: ESP_LOGI("KEY", "key %d pressed", x);
|
||||
|
|
Loading…
Reference in a new issue