From d3e291b442f47a943a9cdf2e38f9d6c8fee14483 Mon Sep 17 00:00:00 2001 From: romerod Date: Tue, 18 May 2021 01:54:09 +0200 Subject: [PATCH] Add on_tag_removed trigger to pn532 (#1436) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/pn532/__init__.py | 16 ++++++++++++++-- esphome/components/pn532/pn532.cpp | 12 +++++++++++- esphome/components/pn532/pn532.h | 6 ++++-- esphome/const.py | 1 + tests/test1.yaml | 6 ++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/esphome/components/pn532/__init__.py b/esphome/components/pn532/__init__.py index 5403ebe5cd..83e6409f54 100644 --- a/esphome/components/pn532/__init__.py +++ b/esphome/components/pn532/__init__.py @@ -2,7 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import automation from esphome.components import nfc -from esphome.const import CONF_ID, CONF_ON_TAG, CONF_TRIGGER_ID +from esphome.const import CONF_ID, CONF_ON_TAG_REMOVED, CONF_ON_TAG, CONF_TRIGGER_ID from esphome.core import coroutine CODEOWNERS = ["@OttoWinter", "@jesserockz"] @@ -41,6 +41,11 @@ PN532_SCHEMA = cv.Schema( ), } ), + cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PN532OnTagTrigger), + } + ), } ).extend(cv.polling_component_schema("1s")) @@ -59,7 +64,14 @@ def setup_pn532(var, config): for conf in config.get(CONF_ON_TAG, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) - cg.add(var.register_trigger(trigger)) + cg.add(var.register_ontag_trigger(trigger)) + yield automation.build_automation( + trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf + ) + + for conf in config.get(CONF_ON_TAG_REMOVED, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) + cg.add(var.register_ontagremoved_trigger(trigger)) yield automation.build_automation( trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf ) diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index 1cc2b19c2e..5e0ec458ab 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -103,6 +103,11 @@ void PN532::loop() { if (!success) { // Something failed + if (!this->current_uid_.empty()) { + auto tag = new nfc::NfcTag(this->current_uid_); + for (auto *trigger : this->triggers_ontagremoved_) + trigger->process(tag); + } this->current_uid_ = {}; this->turn_off_rf_(); return; @@ -111,6 +116,11 @@ void PN532::loop() { uint8_t num_targets = read[0]; if (num_targets != 1) { // no tags found or too many + if (!this->current_uid_.empty()) { + auto tag = new nfc::NfcTag(this->current_uid_); + for (auto *trigger : this->triggers_ontagremoved_) + trigger->process(tag); + } this->current_uid_ = {}; this->turn_off_rf_(); return; @@ -142,7 +152,7 @@ void PN532::loop() { if (next_task_ == READ) { auto tag = this->read_tag_(nfcid); - for (auto *trigger : this->triggers_) + for (auto *trigger : this->triggers_ontag_) trigger->process(tag); if (report) { diff --git a/esphome/components/pn532/pn532.h b/esphome/components/pn532/pn532.h index 95a2e0dd2a..c4854cf7f2 100644 --- a/esphome/components/pn532/pn532.h +++ b/esphome/components/pn532/pn532.h @@ -30,7 +30,8 @@ class PN532 : public PollingComponent { void loop() override; void register_tag(PN532BinarySensor *tag) { this->binary_sensors_.push_back(tag); } - void register_trigger(PN532OnTagTrigger *trig) { this->triggers_.push_back(trig); } + 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 add_on_finished_write_callback(std::function callback) { this->on_finished_write_callback_.add(std::move(callback)); @@ -78,7 +79,8 @@ class PN532 : public PollingComponent { bool requested_read_{false}; std::vector binary_sensors_; - std::vector triggers_; + std::vector triggers_ontag_; + std::vector triggers_ontagremoved_; std::vector current_uid_; nfc::NdefMessage *next_task_message_to_write_; enum NfcTask { diff --git a/esphome/const.py b/esphome/const.py index a83e090285..a70fefa648 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -380,6 +380,7 @@ CONF_ON_RELEASE = "on_release" CONF_ON_SHUTDOWN = "on_shutdown" CONF_ON_STATE = "on_state" CONF_ON_TAG = "on_tag" +CONF_ON_TAG_REMOVED = "on_tag_removed" CONF_ON_TIME = "on_time" CONF_ON_TIME_SYNC = "on_time_sync" CONF_ON_TURN_OFF = "on_turn_off" diff --git a/tests/test1.yaml b/tests/test1.yaml index c2a79085b5..df1c340228 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1968,6 +1968,12 @@ pn532_spi: - mqtt.publish: topic: the/topic payload: !lambda 'return x;' + on_tag_removed: + - lambda: |- + ESP_LOGD("main", "Removed tag %s", x.c_str()); + - mqtt.publish: + topic: the/topic + payload: !lambda 'return x;' pn532_i2c: