mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-27 08:18:28 +02:00
Add support for matrix keypads (#4241)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_ID, CONF_KEY
|
||||
from .. import MatrixKeypad, matrix_keypad_ns, CONF_KEYPAD_ID
|
||||
|
||||
CONF_ROW = "row"
|
||||
CONF_COL = "col"
|
||||
|
||||
DEPENDENCIES = ["matrix_keypad"]
|
||||
|
||||
MatrixKeypadBinarySensor = matrix_keypad_ns.class_(
|
||||
"MatrixKeypadBinarySensor", binary_sensor.BinarySensor
|
||||
)
|
||||
|
||||
|
||||
def check_button(obj):
|
||||
if CONF_ROW in obj or CONF_COL in obj:
|
||||
if CONF_KEY in obj:
|
||||
raise cv.Invalid("You can't provide both a key and a position")
|
||||
if CONF_ROW not in obj:
|
||||
raise cv.Invalid("Missing row")
|
||||
if CONF_COL not in obj:
|
||||
raise cv.Invalid("Missing col")
|
||||
elif CONF_KEY not in obj:
|
||||
raise cv.Invalid("Missing key or position")
|
||||
elif len(obj[CONF_KEY]) != 1:
|
||||
raise cv.Invalid("Key must be one character")
|
||||
return obj
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(MatrixKeypadBinarySensor),
|
||||
cv.GenerateID(CONF_KEYPAD_ID): cv.use_id(MatrixKeypad),
|
||||
cv.Optional(CONF_ROW): cv.int_,
|
||||
cv.Optional(CONF_COL): cv.int_,
|
||||
cv.Optional(CONF_KEY): cv.string,
|
||||
}
|
||||
),
|
||||
check_button,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
if CONF_KEY in config:
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_KEY][0])
|
||||
else:
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_ROW], config[CONF_COL])
|
||||
await binary_sensor.register_binary_sensor(var, config)
|
||||
matrix_keypad = await cg.get_variable(config[CONF_KEYPAD_ID])
|
||||
cg.add(matrix_keypad.register_listener(var))
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/matrix_keypad/matrix_keypad.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace matrix_keypad {
|
||||
|
||||
class MatrixKeypadBinarySensor : public MatrixKeypadListener, public binary_sensor::BinarySensor {
|
||||
public:
|
||||
MatrixKeypadBinarySensor(uint8_t key) : has_key_(true), key_(key){};
|
||||
MatrixKeypadBinarySensor(const char *key) : has_key_(true), key_((uint8_t) key[0]){};
|
||||
MatrixKeypadBinarySensor(int row, int col) : has_key_(false), row_(row), col_(col){};
|
||||
|
||||
void key_pressed(uint8_t key) override {
|
||||
if (!this->has_key_)
|
||||
return;
|
||||
if (key == this->key_)
|
||||
this->publish_state(true);
|
||||
}
|
||||
|
||||
void key_released(uint8_t key) override {
|
||||
if (!this->has_key_)
|
||||
return;
|
||||
if (key == this->key_)
|
||||
this->publish_state(false);
|
||||
}
|
||||
|
||||
void button_pressed(int row, int col) override {
|
||||
if (this->has_key_)
|
||||
return;
|
||||
if ((row == this->row_) && (col == this->col_))
|
||||
this->publish_state(true);
|
||||
}
|
||||
|
||||
void button_released(int row, int col) override {
|
||||
if (this->has_key_)
|
||||
return;
|
||||
if ((row == this->row_) && (col == this->col_))
|
||||
this->publish_state(false);
|
||||
}
|
||||
|
||||
protected:
|
||||
bool has_key_;
|
||||
uint8_t key_;
|
||||
int row_;
|
||||
int col_;
|
||||
};
|
||||
|
||||
} // namespace matrix_keypad
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user