Move PN532OnTagTrigger to nfc::NfcOnTagTrigger (#3379)

This commit is contained in:
Jesse Hills 2022-04-13 12:26:55 +12:00 committed by GitHub
parent a519e5c475
commit b622a8fa58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 20 deletions

View file

@ -1,3 +1,4 @@
from esphome import automation
import esphome.codegen as cg import esphome.codegen as cg
CODEOWNERS = ["@jesserockz"] CODEOWNERS = ["@jesserockz"]
@ -5,3 +6,7 @@ CODEOWNERS = ["@jesserockz"]
nfc_ns = cg.esphome_ns.namespace("nfc") nfc_ns = cg.esphome_ns.namespace("nfc")
NfcTag = nfc_ns.class_("NfcTag") NfcTag = nfc_ns.class_("NfcTag")
NfcOnTagTrigger = nfc_ns.class_(
"NfcOnTagTrigger", automation.Trigger.template(cg.std_string, NfcTag)
)

View file

@ -0,0 +1,9 @@
#include "automation.h"
namespace esphome {
namespace nfc {
void NfcOnTagTrigger::process(const std::unique_ptr<NfcTag> &tag) { this->trigger(format_uid(tag->get_uid()), *tag); }
} // namespace nfc
} // namespace esphome

View file

@ -0,0 +1,17 @@
#pragma once
#include <string>
#include "esphome/core/automation.h"
#include "nfc.h"
namespace esphome {
namespace nfc {
class NfcOnTagTrigger : public Trigger<std::string, NfcTag> {
public:
void process(const std::unique_ptr<NfcTag> &tag);
};
} // namespace nfc
} // namespace esphome

View file

@ -14,9 +14,6 @@ CONF_ON_FINISHED_WRITE = "on_finished_write"
pn532_ns = cg.esphome_ns.namespace("pn532") pn532_ns = cg.esphome_ns.namespace("pn532")
PN532 = pn532_ns.class_("PN532", cg.PollingComponent) PN532 = pn532_ns.class_("PN532", cg.PollingComponent)
PN532OnTagTrigger = pn532_ns.class_(
"PN532OnTagTrigger", automation.Trigger.template(cg.std_string, nfc.NfcTag)
)
PN532OnFinishedWriteTrigger = pn532_ns.class_( PN532OnFinishedWriteTrigger = pn532_ns.class_(
"PN532OnFinishedWriteTrigger", automation.Trigger.template() "PN532OnFinishedWriteTrigger", automation.Trigger.template()
) )
@ -30,7 +27,7 @@ PN532_SCHEMA = cv.Schema(
cv.GenerateID(): cv.declare_id(PN532), cv.GenerateID(): cv.declare_id(PN532),
cv.Optional(CONF_ON_TAG): automation.validate_automation( cv.Optional(CONF_ON_TAG): automation.validate_automation(
{ {
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PN532OnTagTrigger), cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
} }
), ),
cv.Optional(CONF_ON_FINISHED_WRITE): automation.validate_automation( cv.Optional(CONF_ON_FINISHED_WRITE): automation.validate_automation(
@ -42,7 +39,7 @@ PN532_SCHEMA = cv.Schema(
), ),
cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation( cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation(
{ {
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PN532OnTagTrigger), cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
} }
), ),
} }

View file

@ -144,9 +144,9 @@ void PN532::loop() {
} }
if (nfcid.size() == this->current_uid_.size()) { if (nfcid.size() == this->current_uid_.size()) {
bool same_uid = false; bool same_uid = true;
for (size_t i = 0; i < nfcid.size(); i++) for (size_t i = 0; i < nfcid.size(); i++)
same_uid |= nfcid[i] == this->current_uid_[i]; same_uid &= nfcid[i] == this->current_uid_[i];
if (same_uid) if (same_uid)
return; return;
} }
@ -376,9 +376,6 @@ bool PN532BinarySensor::process(std::vector<uint8_t> &data) {
this->found_ = true; this->found_ = true;
return true; return true;
} }
void PN532OnTagTrigger::process(const std::unique_ptr<nfc::NfcTag> &tag) {
this->trigger(nfc::format_uid(tag->get_uid()), *tag);
}
} // namespace pn532 } // namespace pn532
} // namespace esphome } // namespace esphome

View file

@ -5,6 +5,7 @@
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/nfc/nfc_tag.h" #include "esphome/components/nfc/nfc_tag.h"
#include "esphome/components/nfc/nfc.h" #include "esphome/components/nfc/nfc.h"
#include "esphome/components/nfc/automation.h"
namespace esphome { namespace esphome {
namespace pn532 { namespace pn532 {
@ -16,7 +17,6 @@ static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A; static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
class PN532BinarySensor; class PN532BinarySensor;
class PN532OnTagTrigger;
class PN532 : public PollingComponent { class PN532 : public PollingComponent {
public: public:
@ -30,8 +30,8 @@ class PN532 : public PollingComponent {
void loop() override; void loop() override;
void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); } void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
void register_ontag_trigger(PN532OnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); } void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
void register_ontagremoved_trigger(PN532OnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); } void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); }
void add_on_finished_write_callback(std::function<void()> callback) { void add_on_finished_write_callback(std::function<void()> callback) {
this->on_finished_write_callback_.add(std::move(callback)); this->on_finished_write_callback_.add(std::move(callback));
@ -79,8 +79,8 @@ class PN532 : public PollingComponent {
bool requested_read_{false}; bool requested_read_{false};
std::vector<PN532BinarySensor *> binary_sensors_; std::vector<PN532BinarySensor *> binary_sensors_;
std::vector<PN532OnTagTrigger *> triggers_ontag_; std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;
std::vector<PN532OnTagTrigger *> triggers_ontagremoved_; std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
std::vector<uint8_t> current_uid_; std::vector<uint8_t> current_uid_;
nfc::NdefMessage *next_task_message_to_write_; nfc::NdefMessage *next_task_message_to_write_;
enum NfcTask { enum NfcTask {
@ -115,11 +115,6 @@ class PN532BinarySensor : public binary_sensor::BinarySensor {
bool found_{false}; bool found_{false};
}; };
class PN532OnTagTrigger : public Trigger<std::string, nfc::NfcTag> {
public:
void process(const std::unique_ptr<nfc::NfcTag> &tag);
};
class PN532OnFinishedWriteTrigger : public Trigger<> { class PN532OnFinishedWriteTrigger : public Trigger<> {
public: public:
explicit PN532OnFinishedWriteTrigger(PN532 *parent) { explicit PN532OnFinishedWriteTrigger(PN532 *parent) {