Add on_tag_removed trigger to pn532 (#1436)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
romerod 2021-05-18 01:54:09 +02:00 committed by GitHub
parent d4686c0fb1
commit d3e291b442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 5 deletions

View file

@ -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
)

View file

@ -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) {

View file

@ -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<void()> callback) {
this->on_finished_write_callback_.add(std::move(callback));
@ -78,7 +79,8 @@ class PN532 : public PollingComponent {
bool requested_read_{false};
std::vector<PN532BinarySensor *> binary_sensors_;
std::vector<PN532OnTagTrigger *> triggers_;
std::vector<PN532OnTagTrigger *> triggers_ontag_;
std::vector<PN532OnTagTrigger *> triggers_ontagremoved_;
std::vector<uint8_t> current_uid_;
nfc::NdefMessage *next_task_message_to_write_;
enum NfcTask {

View file

@ -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"

View file

@ -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: