mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-24 22:58:30 +02:00
Add NFC binary sensor platform (#6068)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_UID
|
||||
from esphome.core import HexInt
|
||||
from .. import nfc_ns, Nfcc, NfcTagListener
|
||||
|
||||
DEPENDENCIES = ["nfc"]
|
||||
|
||||
CONF_NDEF_CONTAINS = "ndef_contains"
|
||||
CONF_NFCC_ID = "nfcc_id"
|
||||
CONF_TAG_ID = "tag_id"
|
||||
|
||||
NfcTagBinarySensor = nfc_ns.class_(
|
||||
"NfcTagBinarySensor",
|
||||
binary_sensor.BinarySensor,
|
||||
cg.Component,
|
||||
NfcTagListener,
|
||||
cg.Parented.template(Nfcc),
|
||||
)
|
||||
|
||||
|
||||
def validate_uid(value):
|
||||
value = cv.string_strict(value)
|
||||
for x in value.split("-"):
|
||||
if len(x) != 2:
|
||||
raise cv.Invalid(
|
||||
"Each part (separated by '-') of the UID must be two characters "
|
||||
"long."
|
||||
)
|
||||
try:
|
||||
x = int(x, 16)
|
||||
except ValueError as err:
|
||||
raise cv.Invalid(
|
||||
"Valid characters for parts of a UID are 0123456789ABCDEF."
|
||||
) from err
|
||||
if x < 0 or x > 255:
|
||||
raise cv.Invalid(
|
||||
"Valid values for UID parts (separated by '-') are 00 to FF"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
binary_sensor.binary_sensor_schema(NfcTagBinarySensor)
|
||||
.extend(
|
||||
{
|
||||
cv.GenerateID(CONF_NFCC_ID): cv.use_id(Nfcc),
|
||||
cv.Optional(CONF_NDEF_CONTAINS): cv.string,
|
||||
cv.Optional(CONF_TAG_ID): cv.string,
|
||||
cv.Optional(CONF_UID): validate_uid,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA),
|
||||
cv.has_exactly_one_key(CONF_NDEF_CONTAINS, CONF_TAG_ID, CONF_UID),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
await cg.register_parented(var, config[CONF_NFCC_ID])
|
||||
|
||||
hub = await cg.get_variable(config[CONF_NFCC_ID])
|
||||
cg.add(hub.register_listener(var))
|
||||
if CONF_NDEF_CONTAINS in config:
|
||||
cg.add(var.set_ndef_match_string(config[CONF_NDEF_CONTAINS]))
|
||||
if CONF_TAG_ID in config:
|
||||
cg.add(var.set_tag_name(config[CONF_TAG_ID]))
|
||||
elif CONF_UID in config:
|
||||
addr = [HexInt(int(x, 16)) for x in config[CONF_UID].split("-")]
|
||||
cg.add(var.set_uid(addr))
|
||||
@@ -0,0 +1,114 @@
|
||||
#include "binary_sensor.h"
|
||||
#include "../nfc_helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace nfc {
|
||||
|
||||
static const char *const TAG = "nfc.binary_sensor";
|
||||
|
||||
void NfcTagBinarySensor::setup() {
|
||||
this->parent_->register_listener(this);
|
||||
this->publish_initial_state(false);
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::dump_config() {
|
||||
std::string match_str = "name";
|
||||
|
||||
LOG_BINARY_SENSOR("", "NFC Tag Binary Sensor", this);
|
||||
if (!this->match_string_.empty()) {
|
||||
if (!this->match_tag_name_) {
|
||||
match_str = "contains";
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Tag %s: %s", match_str.c_str(), this->match_string_.c_str());
|
||||
return;
|
||||
}
|
||||
if (!this->uid_.empty()) {
|
||||
ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes(this->uid_).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::set_ndef_match_string(const std::string &str) {
|
||||
this->match_string_ = str;
|
||||
this->match_tag_name_ = false;
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::set_tag_name(const std::string &str) {
|
||||
this->match_string_ = str;
|
||||
this->match_tag_name_ = true;
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::set_uid(const std::vector<uint8_t> &uid) { this->uid_ = uid; }
|
||||
|
||||
bool NfcTagBinarySensor::tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg) {
|
||||
for (const auto &record : msg->get_records()) {
|
||||
if (record->get_payload().find(this->match_string_) != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NfcTagBinarySensor::tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg) {
|
||||
for (const auto &record : msg->get_records()) {
|
||||
if (record->get_payload().find(HA_TAG_ID_PREFIX) != std::string::npos) {
|
||||
auto rec_substr = record->get_payload().substr(sizeof(HA_TAG_ID_PREFIX) - 1);
|
||||
if (rec_substr.find(this->match_string_) != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NfcTagBinarySensor::tag_match_uid(const std::vector<uint8_t> &data) {
|
||||
if (data.size() != this->uid_.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
if (data[i] != this->uid_[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::tag_off(NfcTag &tag) {
|
||||
if (!this->match_string_.empty() && tag.has_ndef_message()) {
|
||||
if (this->match_tag_name_) {
|
||||
if (this->tag_match_tag_name(tag.get_ndef_message())) {
|
||||
this->publish_state(false);
|
||||
}
|
||||
} else {
|
||||
if (this->tag_match_ndef_string(tag.get_ndef_message())) {
|
||||
this->publish_state(false);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
|
||||
this->publish_state(false);
|
||||
}
|
||||
}
|
||||
|
||||
void NfcTagBinarySensor::tag_on(NfcTag &tag) {
|
||||
if (!this->match_string_.empty() && tag.has_ndef_message()) {
|
||||
if (this->match_tag_name_) {
|
||||
if (this->tag_match_tag_name(tag.get_ndef_message())) {
|
||||
this->publish_state(true);
|
||||
}
|
||||
} else {
|
||||
if (this->tag_match_ndef_string(tag.get_ndef_message())) {
|
||||
this->publish_state(true);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
|
||||
this->publish_state(true);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nfc
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/nfc/nfc.h"
|
||||
#include "esphome/components/nfc/nfc_tag.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace nfc {
|
||||
|
||||
class NfcTagBinarySensor : public binary_sensor::BinarySensor,
|
||||
public Component,
|
||||
public NfcTagListener,
|
||||
public Parented<Nfcc> {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
void set_ndef_match_string(const std::string &str);
|
||||
void set_tag_name(const std::string &str);
|
||||
void set_uid(const std::vector<uint8_t> &uid);
|
||||
|
||||
bool tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg);
|
||||
bool tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg);
|
||||
bool tag_match_uid(const std::vector<uint8_t> &data);
|
||||
|
||||
void tag_off(NfcTag &tag) override;
|
||||
void tag_on(NfcTag &tag) override;
|
||||
|
||||
protected:
|
||||
bool match_tag_name_{false};
|
||||
std::string match_string_;
|
||||
std::vector<uint8_t> uid_;
|
||||
};
|
||||
|
||||
} // namespace nfc
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user