ESPHome  2024.5.0
binary_sensor.cpp
Go to the documentation of this file.
1 #include "binary_sensor.h"
2 #include "../nfc_helpers.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace nfc {
7 
8 static 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  ESP_LOGCONFIG(TAG, " Tag UID: %s", format_bytes(this->uid_).c_str());
28  }
29 }
30 
31 void NfcTagBinarySensor::set_ndef_match_string(const std::string &str) {
32  this->match_string_ = str;
33  this->match_tag_name_ = false;
34 }
35 
36 void NfcTagBinarySensor::set_tag_name(const std::string &str) {
37  this->match_string_ = str;
38  this->match_tag_name_ = true;
39 }
40 
41 void NfcTagBinarySensor::set_uid(const std::vector<uint8_t> &uid) { this->uid_ = uid; }
42 
43 bool NfcTagBinarySensor::tag_match_ndef_string(const std::shared_ptr<NdefMessage> &msg) {
44  for (const auto &record : msg->get_records()) {
45  if (record->get_payload().find(this->match_string_) != std::string::npos) {
46  return true;
47  }
48  }
49  return false;
50 }
51 
52 bool NfcTagBinarySensor::tag_match_tag_name(const std::shared_ptr<NdefMessage> &msg) {
53  for (const auto &record : msg->get_records()) {
54  if (record->get_payload().find(HA_TAG_ID_PREFIX) != std::string::npos) {
55  auto rec_substr = record->get_payload().substr(sizeof(HA_TAG_ID_PREFIX) - 1);
56  if (rec_substr.find(this->match_string_) != std::string::npos) {
57  return true;
58  }
59  }
60  }
61  return false;
62 }
63 
64 bool NfcTagBinarySensor::tag_match_uid(const std::vector<uint8_t> &data) {
65  if (data.size() != this->uid_.size()) {
66  return false;
67  }
68 
69  for (size_t i = 0; i < data.size(); i++) {
70  if (data[i] != this->uid_[i]) {
71  return false;
72  }
73  }
74  return true;
75 }
76 
78  if (!this->match_string_.empty() && tag.has_ndef_message()) {
79  if (this->match_tag_name_) {
80  if (this->tag_match_tag_name(tag.get_ndef_message())) {
81  this->publish_state(false);
82  }
83  } else {
84  if (this->tag_match_ndef_string(tag.get_ndef_message())) {
85  this->publish_state(false);
86  }
87  }
88  return;
89  }
90  if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
91  this->publish_state(false);
92  }
93 }
94 
96  if (!this->match_string_.empty() && tag.has_ndef_message()) {
97  if (this->match_tag_name_) {
98  if (this->tag_match_tag_name(tag.get_ndef_message())) {
99  this->publish_state(true);
100  }
101  } else {
102  if (this->tag_match_ndef_string(tag.get_ndef_message())) {
103  this->publish_state(true);
104  }
105  }
106  return;
107  }
108  if (!this->uid_.empty() && this->tag_match_uid(tag.get_uid())) {
109  this->publish_state(true);
110  }
111 }
112 
113 } // namespace nfc
114 } // namespace esphome
void publish_initial_state(bool state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
const std::shared_ptr< NdefMessage > & get_ndef_message()
Definition: nfc_tag.h:47
std::vector< uint8_t > uid_
Definition: binary_sensor.h:34
void set_ndef_match_string(const std::string &str)
bool has_ndef_message()
Definition: nfc_tag.h:46
void set_uid(const std::vector< uint8_t > &uid)
bool tag_match_ndef_string(const std::shared_ptr< NdefMessage > &msg)
void publish_state(bool state)
Publish a new state to the front-end.
std::vector< uint8_t > & get_uid()
Definition: nfc_tag.h:44
bool tag_match_tag_name(const std::shared_ptr< NdefMessage > &msg)
void set_tag_name(const std::string &str)
void tag_on(NfcTag &tag) override
void tag_off(NfcTag &tag) override
bool tag_match_uid(const std::vector< uint8_t > &data)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::string format_bytes(std::vector< uint8_t > &bytes)
Definition: nfc.cpp:22