mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 11:26:39 +00:00
[hlk_fm22x] Use register_apply_action for scan, enroll, delete, delete_all and reset (#19537)
This commit is contained in:
@@ -27,11 +27,6 @@ HlkFm22xComponent = hlk_fm22x_ns.class_(
|
|||||||
"HlkFm22xComponent", cg.PollingComponent, uart.UARTDevice
|
"HlkFm22xComponent", cg.PollingComponent, uart.UARTDevice
|
||||||
)
|
)
|
||||||
|
|
||||||
EnrollmentAction = hlk_fm22x_ns.class_("EnrollmentAction", automation.Action)
|
|
||||||
DeleteAction = hlk_fm22x_ns.class_("DeleteAction", automation.Action)
|
|
||||||
DeleteAllAction = hlk_fm22x_ns.class_("DeleteAllAction", automation.Action)
|
|
||||||
ScanAction = hlk_fm22x_ns.class_("ScanAction", automation.Action)
|
|
||||||
ResetAction = hlk_fm22x_ns.class_("ResetAction", automation.Action)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
@@ -101,9 +96,8 @@ async def to_code(config):
|
|||||||
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
|
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
automation.register_apply_action(
|
||||||
"hlk_fm22x.enroll",
|
"hlk_fm22x.enroll",
|
||||||
EnrollmentAction,
|
|
||||||
cv.maybe_simple_value(
|
cv.maybe_simple_value(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
||||||
@@ -112,22 +106,14 @@ async def to_code(config):
|
|||||||
},
|
},
|
||||||
key=CONF_NAME,
|
key=CONF_NAME,
|
||||||
),
|
),
|
||||||
synchronous=True,
|
automation.ApplyCall(
|
||||||
|
"enroll_face({}, static_cast<hlk_fm22x::HlkFm22xFaceDirection>({}))",
|
||||||
|
((CONF_NAME, cg.std_string), (CONF_DIRECTION, cg.uint8)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
async def hlk_fm22x_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_NAME], args, cg.std_string)
|
automation.register_apply_action(
|
||||||
cg.add(var.set_name(template_))
|
|
||||||
template_ = await cg.templatable(config[CONF_DIRECTION], args, cg.uint8)
|
|
||||||
cg.add(var.set_direction(template_))
|
|
||||||
return var
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
|
||||||
"hlk_fm22x.delete",
|
"hlk_fm22x.delete",
|
||||||
DeleteAction,
|
|
||||||
cv.maybe_simple_value(
|
cv.maybe_simple_value(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
||||||
@@ -135,60 +121,37 @@ async def hlk_fm22x_enroll_to_code(config, action_id, template_arg, args):
|
|||||||
},
|
},
|
||||||
key=CONF_FACE_ID,
|
key=CONF_FACE_ID,
|
||||||
),
|
),
|
||||||
synchronous=True,
|
automation.ApplyField(CONF_FACE_ID, "delete_face", cg.int16),
|
||||||
)
|
)
|
||||||
async def hlk_fm22x_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_FACE_ID], args, cg.int16)
|
automation.register_apply_action(
|
||||||
cg.add(var.set_face_id(template_))
|
|
||||||
return var
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
|
||||||
"hlk_fm22x.delete_all",
|
"hlk_fm22x.delete_all",
|
||||||
DeleteAllAction,
|
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
synchronous=True,
|
automation.ApplyCall("delete_all_faces()"),
|
||||||
)
|
)
|
||||||
async def hlk_fm22x_delete_all_to_code(config, action_id, template_arg, args):
|
|
||||||
var = cg.new_Pvariable(action_id, template_arg)
|
|
||||||
await cg.register_parented(var, config[CONF_ID])
|
|
||||||
return var
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
automation.register_apply_action(
|
||||||
"hlk_fm22x.scan",
|
"hlk_fm22x.scan",
|
||||||
ScanAction,
|
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
synchronous=True,
|
automation.ApplyCall("scan_face()"),
|
||||||
)
|
)
|
||||||
async def hlk_fm22x_scan_to_code(config, action_id, template_arg, args):
|
|
||||||
var = cg.new_Pvariable(action_id, template_arg)
|
|
||||||
await cg.register_parented(var, config[CONF_ID])
|
|
||||||
return var
|
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(
|
automation.register_apply_action(
|
||||||
"hlk_fm22x.reset",
|
"hlk_fm22x.reset",
|
||||||
ResetAction,
|
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
cv.GenerateID(): cv.use_id(HlkFm22xComponent),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
synchronous=True,
|
automation.ApplyCall("reset()"),
|
||||||
)
|
)
|
||||||
async def hlk_fm22x_reset_to_code(config, action_id, template_arg, args):
|
|
||||||
var = cg.new_Pvariable(action_id, template_arg)
|
|
||||||
await cg.register_parented(var, config[CONF_ID])
|
|
||||||
return var
|
|
||||||
|
|||||||
@@ -141,41 +141,4 @@ class HlkFm22xComponent final : public PollingComponent, public uart::UARTDevice
|
|||||||
CallbackManager<void(uint8_t)> enrollment_failed_callback_;
|
CallbackManager<void(uint8_t)> enrollment_failed_callback_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class EnrollmentAction final : public Action<Ts...>, public Parented<HlkFm22xComponent> {
|
|
||||||
public:
|
|
||||||
TEMPLATABLE_VALUE(std::string, name)
|
|
||||||
TEMPLATABLE_VALUE(uint8_t, direction)
|
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
|
||||||
auto name = this->name_.value(x...);
|
|
||||||
auto direction = (HlkFm22xFaceDirection) this->direction_.value(x...);
|
|
||||||
this->parent_->enroll_face(name, direction);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename... Ts> class DeleteAction final : public Action<Ts...>, public Parented<HlkFm22xComponent> {
|
|
||||||
public:
|
|
||||||
TEMPLATABLE_VALUE(int16_t, face_id)
|
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
|
||||||
auto face_id = this->face_id_.value(x...);
|
|
||||||
this->parent_->delete_face(face_id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename... Ts> class DeleteAllAction final : public Action<Ts...>, public Parented<HlkFm22xComponent> {
|
|
||||||
public:
|
|
||||||
void play(const Ts &...x) override { this->parent_->delete_all_faces(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename... Ts> class ScanAction final : public Action<Ts...>, public Parented<HlkFm22xComponent> {
|
|
||||||
public:
|
|
||||||
void play(const Ts &...x) override { this->parent_->scan_face(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename... Ts> class ResetAction final : public Action<Ts...>, public Parented<HlkFm22xComponent> {
|
|
||||||
public:
|
|
||||||
void play(const Ts &...x) override { this->parent_->reset(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace esphome::hlk_fm22x
|
} // namespace esphome::hlk_fm22x
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ esphome:
|
|||||||
- hlk_fm22x.enroll:
|
- hlk_fm22x.enroll:
|
||||||
name: "Test"
|
name: "Test"
|
||||||
direction: 1
|
direction: 1
|
||||||
|
- hlk_fm22x.delete: 5
|
||||||
- hlk_fm22x.delete_all:
|
- hlk_fm22x.delete_all:
|
||||||
|
- hlk_fm22x.reset:
|
||||||
|
- hlk_fm22x.scan:
|
||||||
|
|
||||||
hlk_fm22x:
|
hlk_fm22x:
|
||||||
on_face_scan_matched:
|
on_face_scan_matched:
|
||||||
- logger.log: test_hlk_22x_face_scan_matched
|
- logger.log: test_hlk_22x_face_scan_matched
|
||||||
|
- hlk_fm22x.enroll:
|
||||||
|
name: !lambda 'return name + "-again";'
|
||||||
|
direction: !lambda 'return face_id % 3;'
|
||||||
|
- hlk_fm22x.delete: !lambda 'return face_id;'
|
||||||
on_face_scan_unmatched:
|
on_face_scan_unmatched:
|
||||||
- logger.log: test_hlk_22x_face_scan_unmatched
|
- logger.log: test_hlk_22x_face_scan_unmatched
|
||||||
on_face_scan_invalid:
|
on_face_scan_invalid:
|
||||||
|
|||||||
Reference in New Issue
Block a user