diff --git a/esphome/components/nfc/__init__.py b/esphome/components/nfc/__init__.py index b795a5d5ca..c3bbc50bf9 100644 --- a/esphome/components/nfc/__init__.py +++ b/esphome/components/nfc/__init__.py @@ -1,3 +1,4 @@ +from esphome import automation import esphome.codegen as cg CODEOWNERS = ["@jesserockz"] @@ -5,3 +6,7 @@ CODEOWNERS = ["@jesserockz"] nfc_ns = cg.esphome_ns.namespace("nfc") NfcTag = nfc_ns.class_("NfcTag") + +NfcOnTagTrigger = nfc_ns.class_( + "NfcOnTagTrigger", automation.Trigger.template(cg.std_string, NfcTag) +) diff --git a/esphome/components/nfc/automation.cpp b/esphome/components/nfc/automation.cpp new file mode 100644 index 0000000000..ff00340df0 --- /dev/null +++ b/esphome/components/nfc/automation.cpp @@ -0,0 +1,9 @@ +#include "automation.h" + +namespace esphome { +namespace nfc { + +void NfcOnTagTrigger::process(const std::unique_ptr &tag) { this->trigger(format_uid(tag->get_uid()), *tag); } + +} // namespace nfc +} // namespace esphome diff --git a/esphome/components/nfc/automation.h b/esphome/components/nfc/automation.h new file mode 100644 index 0000000000..565b71bdd9 --- /dev/null +++ b/esphome/components/nfc/automation.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include "esphome/core/automation.h" + +#include "nfc.h" + +namespace esphome { +namespace nfc { + +class NfcOnTagTrigger : public Trigger { + public: + void process(const std::unique_ptr &tag); +}; + +} // namespace nfc +} // namespace esphome diff --git a/esphome/components/pn532/__init__.py b/esphome/components/pn532/__init__.py index b902e8e3d0..2f120bc983 100644 --- a/esphome/components/pn532/__init__.py +++ b/esphome/components/pn532/__init__.py @@ -14,9 +14,6 @@ CONF_ON_FINISHED_WRITE = "on_finished_write" pn532_ns = cg.esphome_ns.namespace("pn532") 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", automation.Trigger.template() ) @@ -30,7 +27,7 @@ PN532_SCHEMA = cv.Schema( cv.GenerateID(): cv.declare_id(PN532), 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( @@ -42,7 +39,7 @@ PN532_SCHEMA = cv.Schema( ), 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), } ), } diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index 0c46ff8a57..7ebf328cff 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -144,9 +144,9 @@ void PN532::loop() { } if (nfcid.size() == this->current_uid_.size()) { - bool same_uid = false; + bool same_uid = true; 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) return; } @@ -376,9 +376,6 @@ bool PN532BinarySensor::process(std::vector &data) { this->found_ = true; return true; } -void PN532OnTagTrigger::process(const std::unique_ptr &tag) { - this->trigger(nfc::format_uid(tag->get_uid()), *tag); -} } // namespace pn532 } // namespace esphome diff --git a/esphome/components/pn532/pn532.h b/esphome/components/pn532/pn532.h index 692a5011e6..4f688dacc2 100644 --- a/esphome/components/pn532/pn532.h +++ b/esphome/components/pn532/pn532.h @@ -5,6 +5,7 @@ #include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/nfc/nfc_tag.h" #include "esphome/components/nfc/nfc.h" +#include "esphome/components/nfc/automation.h" namespace esphome { namespace pn532 { @@ -16,7 +17,6 @@ static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40; static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A; class PN532BinarySensor; -class PN532OnTagTrigger; class PN532 : public PollingComponent { public: @@ -30,8 +30,8 @@ class PN532 : public PollingComponent { void loop() override; 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_ontagremoved_trigger(PN532OnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); } + void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); } + void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); } void add_on_finished_write_callback(std::function callback) { this->on_finished_write_callback_.add(std::move(callback)); @@ -79,8 +79,8 @@ class PN532 : public PollingComponent { bool requested_read_{false}; std::vector binary_sensors_; - std::vector triggers_ontag_; - std::vector triggers_ontagremoved_; + std::vector triggers_ontag_; + std::vector triggers_ontagremoved_; std::vector current_uid_; nfc::NdefMessage *next_task_message_to_write_; enum NfcTask { @@ -115,11 +115,6 @@ class PN532BinarySensor : public binary_sensor::BinarySensor { bool found_{false}; }; -class PN532OnTagTrigger : public Trigger { - public: - void process(const std::unique_ptr &tag); -}; - class PN532OnFinishedWriteTrigger : public Trigger<> { public: explicit PN532OnFinishedWriteTrigger(PN532 *parent) {