Fingerprint_grow: Trigger on finger scan start and on finger scan misplaced (#6003)

This commit is contained in:
alexborro 2024-01-16 08:38:19 +01:00 committed by GitHub
parent 72ab1700e7
commit 249cd67588
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 14 deletions

View file

@ -13,8 +13,10 @@ from esphome.const import (
CONF_ON_ENROLLMENT_DONE,
CONF_ON_ENROLLMENT_FAILED,
CONF_ON_ENROLLMENT_SCAN,
CONF_ON_FINGER_SCAN_START,
CONF_ON_FINGER_SCAN_MATCHED,
CONF_ON_FINGER_SCAN_UNMATCHED,
CONF_ON_FINGER_SCAN_MISPLACED,
CONF_ON_FINGER_SCAN_INVALID,
CONF_PASSWORD,
CONF_SENSING_PIN,
@ -35,6 +37,10 @@ FingerprintGrowComponent = fingerprint_grow_ns.class_(
"FingerprintGrowComponent", cg.PollingComponent, uart.UARTDevice
)
FingerScanStartTrigger = fingerprint_grow_ns.class_(
"FingerScanStartTrigger", automation.Trigger.template()
)
FingerScanMatchedTrigger = fingerprint_grow_ns.class_(
"FingerScanMatchedTrigger", automation.Trigger.template(cg.uint16, cg.uint16)
)
@ -43,6 +49,10 @@ FingerScanUnmatchedTrigger = fingerprint_grow_ns.class_(
"FingerScanUnmatchedTrigger", automation.Trigger.template()
)
FingerScanMisplacedTrigger = fingerprint_grow_ns.class_(
"FingerScanMisplacedTrigger", automation.Trigger.template()
)
FingerScanInvalidTrigger = fingerprint_grow_ns.class_(
"FingerScanInvalidTrigger", automation.Trigger.template()
)
@ -99,6 +109,13 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_SENSING_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_PASSWORD): cv.uint32_t,
cv.Optional(CONF_NEW_PASSWORD): cv.uint32_t,
cv.Optional(CONF_ON_FINGER_SCAN_START): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
FingerScanStartTrigger
),
}
),
cv.Optional(CONF_ON_FINGER_SCAN_MATCHED): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
@ -113,6 +130,13 @@ CONFIG_SCHEMA = (
),
}
),
cv.Optional(CONF_ON_FINGER_SCAN_MISPLACED): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
FingerScanMisplacedTrigger
),
}
),
cv.Optional(CONF_ON_FINGER_SCAN_INVALID): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
@ -164,6 +188,10 @@ async def to_code(config):
sensing_pin = await cg.gpio_pin_expression(config[CONF_SENSING_PIN])
cg.add(var.set_sensing_pin(sensing_pin))
for conf in config.get(CONF_ON_FINGER_SCAN_START, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_FINGER_SCAN_MATCHED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
@ -174,6 +202,10 @@ async def to_code(config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_FINGER_SCAN_MISPLACED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_FINGER_SCAN_INVALID, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)

View file

@ -15,16 +15,18 @@ void FingerprintGrowComponent::update() {
return;
}
if (this->sensing_pin_ != nullptr) {
if (this->has_sensing_pin_) {
if (this->sensing_pin_->digital_read()) {
ESP_LOGV(TAG, "No touch sensing");
this->waiting_removal_ = false;
return;
} else if (!this->waiting_removal_) {
this->finger_scan_start_callback_.call();
}
}
if (this->waiting_removal_) {
if (this->scan_image_(1) == NO_FINGER) {
if ((!this->has_sensing_pin_) && (this->scan_image_(1) == NO_FINGER)) {
ESP_LOGD(TAG, "Finger removed");
this->waiting_removal_ = false;
}
@ -51,6 +53,7 @@ void FingerprintGrowComponent::update() {
void FingerprintGrowComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Grow Fingerprint Reader...");
this->has_sensing_pin_ = (this->sensing_pin_ != nullptr);
if (this->check_password_()) {
if (this->new_password_ != -1) {
if (this->set_password_())
@ -91,7 +94,7 @@ void FingerprintGrowComponent::finish_enrollment(uint8_t result) {
}
void FingerprintGrowComponent::scan_and_match_() {
if (this->sensing_pin_ != nullptr) {
if (this->has_sensing_pin_) {
ESP_LOGD(TAG, "Scan and match");
} else {
ESP_LOGV(TAG, "Scan and match");
@ -122,33 +125,38 @@ void FingerprintGrowComponent::scan_and_match_() {
}
uint8_t FingerprintGrowComponent::scan_image_(uint8_t buffer) {
if (this->sensing_pin_ != nullptr) {
if (this->has_sensing_pin_) {
ESP_LOGD(TAG, "Getting image %d", buffer);
} else {
ESP_LOGV(TAG, "Getting image %d", buffer);
}
this->data_ = {GET_IMAGE};
switch (this->send_command_()) {
uint8_t send_result = this->send_command_();
switch (send_result) {
case OK:
break;
case NO_FINGER:
if (this->sensing_pin_ != nullptr) {
ESP_LOGD(TAG, "No finger");
this->finger_scan_invalid_callback_.call();
if (this->has_sensing_pin_) {
this->waiting_removal_ = true;
ESP_LOGD(TAG, "Finger Misplaced");
this->finger_scan_misplaced_callback_.call();
} else {
ESP_LOGV(TAG, "No finger");
}
return this->data_[0];
return send_result;
case IMAGE_FAIL:
ESP_LOGE(TAG, "Imaging error");
this->finger_scan_invalid_callback_.call();
return send_result;
default:
return this->data_[0];
ESP_LOGD(TAG, "Unknown Scan Error: %d", send_result);
return send_result;
}
ESP_LOGD(TAG, "Processing image %d", buffer);
this->data_ = {IMAGE_2_TZ, buffer};
switch (this->send_command_()) {
send_result = this->send_command_();
switch (send_result) {
case OK:
ESP_LOGI(TAG, "Processed image %d", buffer);
break;
@ -162,7 +170,7 @@ uint8_t FingerprintGrowComponent::scan_image_(uint8_t buffer) {
this->finger_scan_invalid_callback_.call();
break;
}
return this->data_[0];
return send_result;
}
uint8_t FingerprintGrowComponent::save_fingerprint_() {
@ -225,10 +233,11 @@ bool FingerprintGrowComponent::get_parameters_() {
ESP_LOGD(TAG, "Getting parameters");
this->data_ = {READ_SYS_PARAM};
if (this->send_command_() == OK) {
ESP_LOGD(TAG, "Got parameters");
if (this->status_sensor_ != nullptr) {
ESP_LOGD(TAG, "Got parameters"); // Bear in mind data_[0] is the transfer status,
if (this->status_sensor_ != nullptr) { // the parameters table start at data_[1]
this->status_sensor_->publish_state(((uint16_t) this->data_[1] << 8) | this->data_[2]);
}
this->system_identifier_code_ = ((uint16_t) this->data_[3] << 8) | this->data_[4];
this->capacity_ = ((uint16_t) this->data_[5] << 8) | this->data_[6];
if (this->capacity_sensor_ != nullptr) {
this->capacity_sensor_->publish_state(this->capacity_);
@ -430,13 +439,22 @@ uint8_t FingerprintGrowComponent::send_command_() {
void FingerprintGrowComponent::dump_config() {
ESP_LOGCONFIG(TAG, "GROW_FINGERPRINT_READER:");
ESP_LOGCONFIG(TAG, " System Identifier Code: 0x%.4X", this->system_identifier_code_);
ESP_LOGCONFIG(TAG, " Touch Sensing Pin: %s",
this->has_sensing_pin_ ? this->sensing_pin_->dump_summary().c_str() : "None");
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Fingerprint Count", this->fingerprint_count_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint16_t) this->fingerprint_count_sensor_->get_state());
LOG_SENSOR(" ", "Status", this->status_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint8_t) this->status_sensor_->get_state());
LOG_SENSOR(" ", "Capacity", this->capacity_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint16_t) this->capacity_sensor_->get_state());
LOG_SENSOR(" ", "Security Level", this->security_level_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint8_t) this->security_level_sensor_->get_state());
LOG_SENSOR(" ", "Last Finger ID", this->last_finger_id_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint32_t) this->last_finger_id_sensor_->get_state());
LOG_SENSOR(" ", "Last Confidence", this->last_confidence_sensor_);
ESP_LOGCONFIG(TAG, " Current Value: %d", (uint32_t) this->last_confidence_sensor_->get_state());
}
} // namespace fingerprint_grow

View file

@ -118,12 +118,18 @@ class FingerprintGrowComponent : public PollingComponent, public uart::UARTDevic
void set_enrolling_binary_sensor(binary_sensor::BinarySensor *enrolling_binary_sensor) {
this->enrolling_binary_sensor_ = enrolling_binary_sensor;
}
void add_on_finger_scan_start_callback(std::function<void()> callback) {
this->finger_scan_start_callback_.add(std::move(callback));
}
void add_on_finger_scan_matched_callback(std::function<void(uint16_t, uint16_t)> callback) {
this->finger_scan_matched_callback_.add(std::move(callback));
}
void add_on_finger_scan_unmatched_callback(std::function<void()> callback) {
this->finger_scan_unmatched_callback_.add(std::move(callback));
}
void add_on_finger_scan_misplaced_callback(std::function<void()> callback) {
this->finger_scan_misplaced_callback_.add(std::move(callback));
}
void add_on_finger_scan_invalid_callback(std::function<void()> callback) {
this->finger_scan_invalid_callback_.add(std::move(callback));
}
@ -166,8 +172,10 @@ class FingerprintGrowComponent : public PollingComponent, public uart::UARTDevic
uint16_t enrollment_slot_ = ENROLLMENT_SLOT_UNUSED;
uint8_t enrollment_buffers_ = 5;
bool waiting_removal_ = false;
bool has_sensing_pin_ = false;
uint32_t last_aura_led_control_ = 0;
uint16_t last_aura_led_duration_ = 0;
uint16_t system_identifier_code_ = 0;
sensor::Sensor *fingerprint_count_sensor_{nullptr};
sensor::Sensor *status_sensor_{nullptr};
sensor::Sensor *capacity_sensor_{nullptr};
@ -176,13 +184,22 @@ class FingerprintGrowComponent : public PollingComponent, public uart::UARTDevic
sensor::Sensor *last_confidence_sensor_{nullptr};
binary_sensor::BinarySensor *enrolling_binary_sensor_{nullptr};
CallbackManager<void()> finger_scan_invalid_callback_;
CallbackManager<void()> finger_scan_start_callback_;
CallbackManager<void(uint16_t, uint16_t)> finger_scan_matched_callback_;
CallbackManager<void()> finger_scan_unmatched_callback_;
CallbackManager<void()> finger_scan_misplaced_callback_;
CallbackManager<void(uint8_t, uint16_t)> enrollment_scan_callback_;
CallbackManager<void(uint16_t)> enrollment_done_callback_;
CallbackManager<void(uint16_t)> enrollment_failed_callback_;
};
class FingerScanStartTrigger : public Trigger<> {
public:
explicit FingerScanStartTrigger(FingerprintGrowComponent *parent) {
parent->add_on_finger_scan_start_callback([this]() { this->trigger(); });
}
};
class FingerScanMatchedTrigger : public Trigger<uint16_t, uint16_t> {
public:
explicit FingerScanMatchedTrigger(FingerprintGrowComponent *parent) {
@ -198,6 +215,13 @@ class FingerScanUnmatchedTrigger : public Trigger<> {
}
};
class FingerScanMisplacedTrigger : public Trigger<> {
public:
explicit FingerScanMisplacedTrigger(FingerprintGrowComponent *parent) {
parent->add_on_finger_scan_misplaced_callback([this]() { this->trigger(); });
}
};
class FingerScanInvalidTrigger : public Trigger<> {
public:
explicit FingerScanInvalidTrigger(FingerprintGrowComponent *parent) {

View file

@ -508,6 +508,8 @@ CONF_ON_ENROLLMENT_FAILED = "on_enrollment_failed"
CONF_ON_ENROLLMENT_SCAN = "on_enrollment_scan"
CONF_ON_FINGER_SCAN_INVALID = "on_finger_scan_invalid"
CONF_ON_FINGER_SCAN_MATCHED = "on_finger_scan_matched"
CONF_ON_FINGER_SCAN_MISPLACED = "on_finger_scan_misplaced"
CONF_ON_FINGER_SCAN_START = "on_finger_scan_start"
CONF_ON_FINGER_SCAN_UNMATCHED = "on_finger_scan_unmatched"
CONF_ON_JSON_MESSAGE = "on_json_message"
CONF_ON_LOCK = "on_lock"

View file

@ -1258,6 +1258,9 @@ fingerprint_grow:
number: 4
password: 0x12FE37DC
new_password: 0xA65B9840
on_finger_scan_start:
- homeassistant.event:
event: esphome.${device_name}_fingerprint_grow_finger_scan_start
on_finger_scan_invalid:
- homeassistant.event:
event: esphome.${device_name}_fingerprint_grow_finger_scan_invalid
@ -1270,6 +1273,9 @@ fingerprint_grow:
on_finger_scan_unmatched:
- homeassistant.event:
event: esphome.${device_name}_fingerprint_grow_finger_scan_unmatched
on_finger_scan_misplaced:
- homeassistant.event:
event: esphome.${device_name}_fingerprint_grow_finger_scan_misplaced
on_enrollment_scan:
- homeassistant.event:
event: esphome.${device_name}_fingerprint_grow_enrollment_scan