ESPHome 2026.2.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 {
7namespace nfc {
8
9static const char *const TAG = "nfc.binary_sensor";
10
12 this->parent_->register_listener(this);
13 this->publish_initial_state(false);
14}
15
17 std::string match_str = "name";
18
19 LOG_BINARY_SENSOR("", "NFC Tag Binary Sensor", this);
20 if (!this->match_string_.empty()) {
21 if (!this->match_tag_name_) {
22 match_str = "contains";
23 }
24 ESP_LOGCONFIG(TAG, " Tag %s: %s", match_str.c_str(), this->match_string_.c_str());
25 return;
26 }
27 if (!this->uid_.empty()) {
28 char uid_buf[FORMAT_BYTES_BUFFER_SIZE];
29 ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes_to(uid_buf, this->uid_));
30 }
31}
32
33void NfcTagBinarySensor::set_ndef_match_string(const std::string &str) {
34 this->match_string_ = str;
35 this->match_tag_name_ = false;
36}
37
38void NfcTagBinarySensor::set_tag_name(const std::string &str) {
39 this->match_string_ = str;
40 this->match_tag_name_ = true;
41}
42
43void NfcTagBinarySensor::set_uid(const NfcTagUid &uid) { this->uid_ = uid; }
44
45bool NfcTagBinarySensor::tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg) {
46 for (const auto &record : msg->get_records()) {
47 if (record->get_payload().find(this->match_string_) != std::string::npos) {
48 return true;
49 }
50 }
51 return false;
52}
53
54bool NfcTagBinarySensor::tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg) {
55 for (const auto &record : msg->get_records()) {
56 if (record->get_payload().find(HA_TAG_ID_PREFIX) != std::string::npos) {
57 auto rec_substr = record->get_payload().substr(sizeof(HA_TAG_ID_PREFIX) - 1);
58 if (rec_substr.find(this->match_string_) != std::string::npos) {
59 return true;
60 }
61 }
62 }
63 return false;
64}
65
67 if (data.size() != this->uid_.size()) {
68 return false;
69 }
70
71 for (size_t i = 0; i < data.size(); i++) {
72 if (data[i] != this->uid_[i]) {
73 return false;
74 }
75 }
76 return true;
77}
78
80 if (!this->match_string_.empty() && tag.has_ndef_message()) {
81 if (this->match_tag_name_) {
82 if (this->tag_match_tag_name(tag.get_ndef_message())) {
83 this->publish_state(false);
84 }
85 } else {
86 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
87 this->publish_state(false);
88 }
89 }
90 return;
91 }
92 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
93 this->publish_state(false);
94 }
95}
96
98 if (!this->match_string_.empty() && tag.has_ndef_message()) {
99 if (this->match_tag_name_) {
100 if (this->tag_match_tag_name(tag.get_ndef_message())) {
101 this->publish_state(true);
102 }
103 } else {
104 if (this->tag_match_ndef_string(tag.get_ndef_message())) {
105 this->publish_state(true);
106 }
107 }
108 return;
109 }
110 if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
111 this->publish_state(true);
112 }
113}
114
115} // namespace nfc
116} // namespace esphome
size_t size() const
Definition helpers.h:197
bool empty() const
Definition helpers.h:198
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:48
bool has_ndef_message()
Definition nfc_tag.h:47
NfcTagUid & get_uid()
Definition nfc_tag.h:45
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:15
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7