mirror of
https://github.com/esphome/esphome.git
synced 2024-11-13 02:37:47 +01:00
Add on_tag_removed trigger to pn532 (#1436)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
d4686c0fb1
commit
d3e291b442
5 changed files with 36 additions and 5 deletions
|
@ -2,7 +2,7 @@ import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome import automation
|
from esphome import automation
|
||||||
from esphome.components import nfc
|
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
|
from esphome.core import coroutine
|
||||||
|
|
||||||
CODEOWNERS = ["@OttoWinter", "@jesserockz"]
|
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"))
|
).extend(cv.polling_component_schema("1s"))
|
||||||
|
|
||||||
|
@ -59,7 +64,14 @@ def setup_pn532(var, config):
|
||||||
|
|
||||||
for conf in config.get(CONF_ON_TAG, []):
|
for conf in config.get(CONF_ON_TAG, []):
|
||||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
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(
|
yield automation.build_automation(
|
||||||
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
|
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
|
||||||
)
|
)
|
||||||
|
|
|
@ -103,6 +103,11 @@ void PN532::loop() {
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
// Something failed
|
// 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->current_uid_ = {};
|
||||||
this->turn_off_rf_();
|
this->turn_off_rf_();
|
||||||
return;
|
return;
|
||||||
|
@ -111,6 +116,11 @@ void PN532::loop() {
|
||||||
uint8_t num_targets = read[0];
|
uint8_t num_targets = read[0];
|
||||||
if (num_targets != 1) {
|
if (num_targets != 1) {
|
||||||
// no tags found or too many
|
// 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->current_uid_ = {};
|
||||||
this->turn_off_rf_();
|
this->turn_off_rf_();
|
||||||
return;
|
return;
|
||||||
|
@ -142,7 +152,7 @@ void PN532::loop() {
|
||||||
|
|
||||||
if (next_task_ == READ) {
|
if (next_task_ == READ) {
|
||||||
auto tag = this->read_tag_(nfcid);
|
auto tag = this->read_tag_(nfcid);
|
||||||
for (auto *trigger : this->triggers_)
|
for (auto *trigger : this->triggers_ontag_)
|
||||||
trigger->process(tag);
|
trigger->process(tag);
|
||||||
|
|
||||||
if (report) {
|
if (report) {
|
||||||
|
|
|
@ -30,7 +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_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) {
|
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));
|
||||||
|
@ -78,7 +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_;
|
std::vector<PN532OnTagTrigger *> triggers_ontag_;
|
||||||
|
std::vector<PN532OnTagTrigger *> 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 {
|
||||||
|
|
|
@ -380,6 +380,7 @@ CONF_ON_RELEASE = "on_release"
|
||||||
CONF_ON_SHUTDOWN = "on_shutdown"
|
CONF_ON_SHUTDOWN = "on_shutdown"
|
||||||
CONF_ON_STATE = "on_state"
|
CONF_ON_STATE = "on_state"
|
||||||
CONF_ON_TAG = "on_tag"
|
CONF_ON_TAG = "on_tag"
|
||||||
|
CONF_ON_TAG_REMOVED = "on_tag_removed"
|
||||||
CONF_ON_TIME = "on_time"
|
CONF_ON_TIME = "on_time"
|
||||||
CONF_ON_TIME_SYNC = "on_time_sync"
|
CONF_ON_TIME_SYNC = "on_time_sync"
|
||||||
CONF_ON_TURN_OFF = "on_turn_off"
|
CONF_ON_TURN_OFF = "on_turn_off"
|
||||||
|
|
|
@ -1968,6 +1968,12 @@ pn532_spi:
|
||||||
- mqtt.publish:
|
- mqtt.publish:
|
||||||
topic: the/topic
|
topic: the/topic
|
||||||
payload: !lambda 'return x;'
|
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:
|
pn532_i2c:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue