ESPHome 2026.5.1
Loading...
Searching...
No Matches
nfc_binary_sensor.cpp
Go to the documentation of this file.
1#include "nfc_binary_sensor.h"
2#include "../nfc.h"
3#include "../nfc_helpers.h"
4#include "esphome/core/log.h"
5
6namespace esphome::nfc {
7
8static const char *const TAG = "nfc.binary_sensor";
9
11 this->parent_->register_listener(this);
12 this->publish_initial_state(false);
13}
14
16 std::string match_str = "name";
17
18 LOG_BINARY_SENSOR("", "NFC Tag Binary Sensor", this);
19 if (!this->match_string_.empty()) {
20 if (!this->match_tag_name_) {
21 match_str = "contains";
22 }
23 ESP_LOGCONFIG(TAG, " Tag %s: %s", match_str.c_str(), this->match_string_.c_str());
24 return;
25 }
26 if (!this->uid_.empty()) {
27 char uid_buf[FORMAT_BYTES_BUFFER_SIZE];
28 ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes_to(uid_buf, this->uid_));
29 }
30}
31
32void NfcTagBinarySensor::set_ndef_match_string(const std::string &str) {
33 this->match_string_ = str;
34 this->match_tag_name_ = false;
35}
36
37void NfcTagBinarySensor::set_tag_name(const std::string &str) {
38 this->match_string_ = str;
39 this->match_tag_name_ = true;
40}
41
42void NfcTagBinarySensor::set_uid(const NfcTagUid &uid) { this->uid_ = uid; }
43
44bool NfcTagBinarySensor::tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg) {
45 for (const auto &record : msg->get_records()) {
46 if (record->get_payload().find(this->match_string_) != std::string::npos) {
47 return true;
48 }
49 }
50 return false;
51}
52
53bool NfcTagBinarySensor::tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg) {
54 for (const auto &record : msg->get_records()) {
55 if (record->get_payload().find(HA_TAG_ID_PREFIX) != std::string::npos) {
56 auto rec_substr = record->get_payload().substr(sizeof(HA_TAG_ID_PREFIX) - 1);
57 if (rec_substr.find(this->match_string_) != std::string::npos) {
58 return true;
59 }
60 }
61 }
62 return false;
63}
64
66 if (data.size() != this->uid_.size()) {
67 return false;
68 }
69
70 for (size_t i = 0; i < data.size(); i++) {
71 if (data[i] != this->uid_[i]) {
72 return false;
73 }
74 }
75 return true;
76}
77
79 if (!this->match_string_.empty() && tag.has_ndef_message()) {
80 if (this->match_tag_name_) {
81 if (this->tag_match_tag_name(tag.get_ndef_message())) {
82 this->publish_state(false);
83 }
84 } else {
85 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
86 this->publish_state(false);
87 }
88 }
89 return;
90 }
91 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
92 this->publish_state(false);
93 }
94}
95
97 if (!this->match_string_.empty() && tag.has_ndef_message()) {
98 if (this->match_tag_name_) {
99 if (this->tag_match_tag_name(tag.get_ndef_message())) {
100 this->publish_state(true);
101 }
102 } else {
103 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
104 this->publish_state(true);
105 }
106 }
107 return;
108 }
109 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
110 this->publish_state(true);
111 }
112}
113
114} // namespace esphome::nfc
bool empty() const
Definition helpers.h:278
void publish_state(bool new_state)
Publish a new state to the front-end.
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
bool tag_match_tag_name(const std::shared_ptr< NdefMessage > &msg)
void tag_on(NfcTag &tag) override
void set_tag_name(const std::string &str)
bool tag_match_uid(const NfcTagUid &data)
void set_ndef_match_string(const std::string &str)
void set_uid(const NfcTagUid &uid)
void tag_off(NfcTag &tag) override
bool tag_match_ndef_string(const std::shared_ptr< NdefMessage > &msg)
const std::shared_ptr< NdefMessage > & get_ndef_message()
Definition nfc_tag.h:47
bool has_ndef_message()
Definition nfc_tag.h:46
char * format_bytes_to(char *buffer, std::span< const uint8_t > bytes)
Format bytes to buffer with ' ' separator (e.g., "04 11 22 33"). Returns buffer for inline use.
Definition nfc.cpp:14
const char * tag
Definition log.h:74