ESPHome 2026.11.0-dev
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
8ESPHOME_LOG_TAG(TAG, "nfc.binary_sensor");
9
11
13 LOG_BINARY_SENSOR("", "NFC Tag Binary Sensor", this);
14 if (!this->match_string_.empty()) {
15 ESP_LOGCONFIG(TAG, " Tag %s: %s", this->match_tag_name_ ? LOG_STR_LITERAL("name") : LOG_STR_LITERAL("contains"),
16 this->match_string_.c_str());
17 return;
18 }
19 if (!this->uid_.empty()) {
20 char uid_buf[FORMAT_BYTES_BUFFER_SIZE];
21 ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes_to(uid_buf, this->uid_));
22 }
23}
24
26 this->match_string_ = StringRef(str);
27 this->match_tag_name_ = false;
28}
29
30void NfcTagBinarySensor::set_tag_name(const char *str) {
31 this->match_string_ = StringRef(str);
32 this->match_tag_name_ = true;
33}
34
35void NfcTagBinarySensor::set_uid(const NfcTagUid &uid) { this->uid_ = uid; }
36
37bool NfcTagBinarySensor::tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg) {
38 for (const auto &record : msg->get_records()) {
39 if (record->get_payload().find(this->match_string_.c_str(), 0, this->match_string_.size()) != std::string::npos) {
40 return true;
41 }
42 }
43 return false;
44}
45
46bool NfcTagBinarySensor::tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg) {
47 for (const auto &record : msg->get_records()) {
48 if (record->get_payload().find(HA_TAG_ID_PREFIX) != std::string::npos) {
49 auto rec_substr = record->get_payload().substr(sizeof(HA_TAG_ID_PREFIX) - 1);
50 if (rec_substr.find(this->match_string_.c_str(), 0, this->match_string_.size()) != std::string::npos) {
51 return true;
52 }
53 }
54 }
55 return false;
56}
57
59 if (data.size() != this->uid_.size()) {
60 return false;
61 }
62
63 for (size_t i = 0; i < data.size(); i++) {
64 if (data[i] != this->uid_[i]) {
65 return false;
66 }
67 }
68 return true;
69}
70
72 if (!this->match_string_.empty() && tag.has_ndef_message()) {
73 if (this->match_tag_name_) {
74 if (this->tag_match_tag_name(tag.get_ndef_message())) {
75 this->publish_state(false);
76 }
77 } else {
78 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
79 this->publish_state(false);
80 }
81 }
82 return;
83 }
84 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
85 this->publish_state(false);
86 }
87}
88
90 if (!this->match_string_.empty() && tag.has_ndef_message()) {
91 if (this->match_tag_name_) {
92 if (this->tag_match_tag_name(tag.get_ndef_message())) {
93 this->publish_state(true);
94 }
95 } else {
96 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
97 this->publish_state(true);
98 }
99 }
100 return;
101 }
102 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
103 this->publish_state(true);
104 }
105}
106
107} // namespace esphome::nfc
bool empty() const
Definition helpers.h:357
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
constexpr bool empty() const
Definition string_ref.h:81
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_ndef_match_string(const char *str)
bool tag_match_uid(const NfcTagUid &data)
void set_uid(const NfcTagUid &uid)
void tag_off(NfcTag &tag) override
bool tag_match_ndef_string(const std::shared_ptr< NdefMessage > &msg)
bool has_ndef_message() const
Definition nfc_tag.h:47
const std::shared_ptr< NdefMessage > & get_ndef_message() const
Definition nfc_tag.h:48
NfcTagUid & get_uid()
Definition nfc_tag.h:44
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
ESPHOME_LOG_TAG(TAG, "nfc.binary_sensor")