mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[fingerprint_grow] Use register_apply_action for the six fingerprint_grow actions (#19640)
This commit is contained in:
@@ -37,16 +37,6 @@ FingerprintGrowComponent = fingerprint_grow_ns.class_(
|
||||
"FingerprintGrowComponent", cg.PollingComponent, uart.UARTDevice
|
||||
)
|
||||
|
||||
EnrollmentAction = fingerprint_grow_ns.class_("EnrollmentAction", automation.Action)
|
||||
CancelEnrollmentAction = fingerprint_grow_ns.class_(
|
||||
"CancelEnrollmentAction", automation.Action
|
||||
)
|
||||
DeleteAction = fingerprint_grow_ns.class_("DeleteAction", automation.Action)
|
||||
DeleteAllAction = fingerprint_grow_ns.class_("DeleteAllAction", automation.Action)
|
||||
LEDControlAction = fingerprint_grow_ns.class_("LEDControlAction", automation.Action)
|
||||
AuraLEDControlAction = fingerprint_grow_ns.class_(
|
||||
"AuraLEDControlAction", automation.Action
|
||||
)
|
||||
|
||||
AuraLEDState = fingerprint_grow_ns.enum("GrowAuraLEDState", True)
|
||||
AURA_LED_STATES = {
|
||||
@@ -181,103 +171,68 @@ async def to_code(config):
|
||||
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"fingerprint_grow.enroll",
|
||||
EnrollmentAction,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
|
||||
cv.Optional(CONF_NUM_SCANS): cv.templatable(cv.uint8_t),
|
||||
},
|
||||
key=CONF_FINGER_ID,
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
async def fingerprint_grow_enroll_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
|
||||
template_ = await cg.templatable(config[CONF_FINGER_ID], args, cg.uint16)
|
||||
cg.add(var.set_finger_id(template_))
|
||||
if CONF_NUM_SCANS in config:
|
||||
template_ = await cg.templatable(config[CONF_NUM_SCANS], args, cg.uint8)
|
||||
cg.add(var.set_num_scans(template_))
|
||||
return var
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"fingerprint_grow.cancel_enroll",
|
||||
CancelEnrollmentAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"fingerprint_grow.delete",
|
||||
DeleteAction,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
|
||||
},
|
||||
key=CONF_FINGER_ID,
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
async def fingerprint_grow_delete_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
|
||||
template_ = await cg.templatable(config[CONF_FINGER_ID], args, cg.uint16)
|
||||
cg.add(var.set_finger_id(template_))
|
||||
return var
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"fingerprint_grow.delete_all",
|
||||
DeleteAllAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
FINGERPRINT_GROW_LED_CONTROL_ACTION_SCHEMA = cv.maybe_simple_value(
|
||||
FINGERPRINT_GROW_ID_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
|
||||
},
|
||||
key=CONF_STATE,
|
||||
}
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.enroll",
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
|
||||
cv.Optional(CONF_NUM_SCANS, default=2): cv.templatable(
|
||||
cv.int_range(min=1, max=255)
|
||||
),
|
||||
},
|
||||
key=CONF_FINGER_ID,
|
||||
),
|
||||
automation.ApplyCall(
|
||||
"enroll_fingerprint({}, {})",
|
||||
((CONF_FINGER_ID, cg.uint16), (CONF_NUM_SCANS, cg.uint8)),
|
||||
),
|
||||
)
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.cancel_enroll",
|
||||
FINGERPRINT_GROW_ID_SCHEMA,
|
||||
automation.ApplyCall("finish_enrollment(1)"),
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.delete",
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_FINGER_ID): cv.templatable(cv.uint16_t),
|
||||
},
|
||||
key=CONF_FINGER_ID,
|
||||
),
|
||||
automation.ApplyField(CONF_FINGER_ID, "delete_fingerprint", cg.uint16),
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.delete_all",
|
||||
FINGERPRINT_GROW_ID_SCHEMA,
|
||||
automation.ApplyCall("delete_all_fingerprints()"),
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.led_control",
|
||||
LEDControlAction,
|
||||
FINGERPRINT_GROW_LED_CONTROL_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
|
||||
},
|
||||
key=CONF_STATE,
|
||||
),
|
||||
automation.ApplyField(CONF_STATE, "led_control", cg.bool_),
|
||||
)
|
||||
async def fingerprint_grow_led_control_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
|
||||
template_ = await cg.templatable(config[CONF_STATE], args, cg.bool_)
|
||||
cg.add(var.set_state(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"fingerprint_grow.aura_led_control",
|
||||
AuraLEDControlAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(FingerprintGrowComponent),
|
||||
@@ -287,15 +242,13 @@ async def fingerprint_grow_led_control_to_code(config, action_id, template_arg,
|
||||
cv.Required(CONF_COUNT): cv.templatable(cv.uint8_t),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyCall(
|
||||
"aura_led_control({}, {}, {}, {})",
|
||||
(
|
||||
(CONF_STATE, cg.uint8),
|
||||
(CONF_SPEED, cg.uint8),
|
||||
(CONF_COLOR, cg.uint8),
|
||||
(CONF_COUNT, cg.uint8),
|
||||
),
|
||||
),
|
||||
)
|
||||
async def fingerprint_grow_aura_led_control_to_code(
|
||||
config, action_id, template_arg, args
|
||||
):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
|
||||
for key in [CONF_STATE, CONF_SPEED, CONF_COLOR, CONF_COUNT]:
|
||||
template_ = await cg.templatable(config[key], args, cg.uint8)
|
||||
cg.add(getattr(var, f"set_{key}")(template_))
|
||||
return var
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
@@ -209,71 +208,4 @@ class FingerprintGrowComponent final : public PollingComponent, public uart::UAR
|
||||
CallbackManager<void(uint16_t)> enrollment_failed_callback_;
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
class EnrollmentAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint16_t, finger_id)
|
||||
TEMPLATABLE_VALUE(uint8_t, num_scans)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto finger_id = this->finger_id_.value(x...);
|
||||
auto num_scans = this->num_scans_.value(x...);
|
||||
if (num_scans) {
|
||||
this->parent_->enroll_fingerprint(finger_id, num_scans);
|
||||
} else {
|
||||
this->parent_->enroll_fingerprint(finger_id, 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
class CancelEnrollmentAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->finish_enrollment(1); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class DeleteAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint16_t, finger_id)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto finger_id = this->finger_id_.value(x...);
|
||||
this->parent_->delete_fingerprint(finger_id);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class DeleteAllAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->delete_all_fingerprints(); }
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
class LEDControlAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(bool, state)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto state = this->state_.value(x...);
|
||||
this->parent_->led_control(state);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts>
|
||||
class AuraLEDControlAction final : public Action<Ts...>, public Parented<FingerprintGrowComponent> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint8_t, state)
|
||||
TEMPLATABLE_VALUE(uint8_t, speed)
|
||||
TEMPLATABLE_VALUE(uint8_t, color)
|
||||
TEMPLATABLE_VALUE(uint8_t, count)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto state = this->state_.value(x...);
|
||||
auto speed = this->speed_.value(x...);
|
||||
auto color = this->color_.value(x...);
|
||||
auto count = this->count_.value(x...);
|
||||
|
||||
this->parent_->aura_led_control(state, speed, color, count);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace esphome::fingerprint_grow
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: Fingerprint LED Actions
|
||||
on_press:
|
||||
- fingerprint_grow.enroll: 3
|
||||
- fingerprint_grow.enroll:
|
||||
finger_id: !lambda return 4;
|
||||
num_scans: !lambda return 3;
|
||||
- fingerprint_grow.led_control: true
|
||||
- fingerprint_grow.led_control:
|
||||
state: !lambda return false;
|
||||
- fingerprint_grow.aura_led_control:
|
||||
state: BREATHING
|
||||
speed: 200
|
||||
color: BLUE
|
||||
count: 2
|
||||
@@ -0,0 +1,9 @@
|
||||
substitutions:
|
||||
tx_pin: GPIO0
|
||||
rx_pin: GPIO2
|
||||
sensing_pin: GPIO15
|
||||
|
||||
packages:
|
||||
uart: !include ../../test_build_components/common/uart/esp8266-ard.yaml
|
||||
fingerprint_grow: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user