mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Add on_tag_removed trigger for RC522 (#4742)
This commit is contained in:
parent
986dd2ddd2
commit
f639f7c280
3 changed files with 27 additions and 5 deletions
|
@ -2,7 +2,12 @@ import esphome.codegen as cg
|
|||
import esphome.config_validation as cv
|
||||
from esphome import automation, pins
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ON_TAG, CONF_TRIGGER_ID, CONF_RESET_PIN
|
||||
from esphome.const import (
|
||||
CONF_ON_TAG,
|
||||
CONF_ON_TAG_REMOVED,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_RESET_PIN,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@glmnet"]
|
||||
AUTO_LOAD = ["binary_sensor"]
|
||||
|
@ -24,6 +29,11 @@ RC522_SCHEMA = cv.Schema(
|
|||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(RC522Trigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(RC522Trigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
).extend(cv.polling_component_schema("1s"))
|
||||
|
||||
|
@ -37,5 +47,10 @@ async def setup_rc522(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))
|
||||
await automation.build_automation(trigger, [(cg.std_string, "x")], 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))
|
||||
await automation.build_automation(trigger, [(cg.std_string, "x")], conf)
|
||||
|
|
|
@ -256,7 +256,7 @@ void RC522::loop() {
|
|||
|
||||
this->current_uid_ = rfid_uid;
|
||||
|
||||
for (auto *trigger : this->triggers_)
|
||||
for (auto *trigger : this->triggers_ontag_)
|
||||
trigger->process(rfid_uid);
|
||||
|
||||
if (report) {
|
||||
|
@ -265,6 +265,11 @@ void RC522::loop() {
|
|||
break;
|
||||
}
|
||||
case STATE_DONE: {
|
||||
if (!this->current_uid_.empty()) {
|
||||
ESP_LOGV(TAG, "Tag '%s' removed", format_uid(this->current_uid_).c_str());
|
||||
for (auto *trigger : this->triggers_ontagremoved_)
|
||||
trigger->process(this->current_uid_);
|
||||
}
|
||||
this->current_uid_ = {};
|
||||
state_ = STATE_INIT;
|
||||
break;
|
||||
|
|
|
@ -24,7 +24,8 @@ class RC522 : public PollingComponent {
|
|||
void loop() override;
|
||||
|
||||
void register_tag(RC522BinarySensor *tag) { this->binary_sensors_.push_back(tag); }
|
||||
void register_trigger(RC522Trigger *trig) { this->triggers_.push_back(trig); }
|
||||
void register_ontag_trigger(RC522Trigger *trig) { this->triggers_ontag_.push_back(trig); }
|
||||
void register_ontagremoved_trigger(RC522Trigger *trig) { this->triggers_ontagremoved_.push_back(trig); }
|
||||
|
||||
void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
|
||||
|
||||
|
@ -242,7 +243,8 @@ class RC522 : public PollingComponent {
|
|||
uint8_t reset_count_{0};
|
||||
uint32_t reset_timeout_{0};
|
||||
std::vector<RC522BinarySensor *> binary_sensors_;
|
||||
std::vector<RC522Trigger *> triggers_;
|
||||
std::vector<RC522Trigger *> triggers_ontag_;
|
||||
std::vector<RC522Trigger *> triggers_ontagremoved_;
|
||||
std::vector<uint8_t> current_uid_;
|
||||
|
||||
enum RC522Error {
|
||||
|
|
Loading…
Reference in a new issue