ESPHome  2024.10.2
ble_presence_device.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace ble_presence {
11 
14  public Component {
15  public:
16  void set_address(uint64_t address) {
18  this->address_ = address;
19  }
20  void set_irk(uint8_t *irk) {
21  this->match_by_ = MATCH_BY_IRK;
22  this->irk_ = irk;
23  }
24  void set_service_uuid16(uint16_t uuid) {
27  }
28  void set_service_uuid32(uint32_t uuid) {
31  }
32  void set_service_uuid128(uint8_t *uuid) {
35  }
36  void set_ibeacon_uuid(uint8_t *uuid) {
39  }
40  void set_ibeacon_major(uint16_t major) {
41  this->check_ibeacon_major_ = true;
42  this->ibeacon_major_ = major;
43  }
44  void set_ibeacon_minor(uint16_t minor) {
45  this->check_ibeacon_minor_ = true;
46  this->ibeacon_minor_ = minor;
47  }
48  void set_minimum_rssi(int rssi) {
49  this->check_minimum_rssi_ = true;
50  this->minimum_rssi_ = rssi;
51  }
52  void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
53  bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
54  if (this->check_minimum_rssi_ && this->minimum_rssi_ > device.get_rssi()) {
55  return false;
56  }
57  switch (this->match_by_) {
59  if (device.address_uint64() == this->address_) {
60  this->set_found_(true);
61  return true;
62  }
63  break;
64  case MATCH_BY_IRK:
65  if (device.resolve_irk(this->irk_)) {
66  this->set_found_(true);
67  return true;
68  }
69  break;
71  for (auto uuid : device.get_service_uuids()) {
72  if (this->uuid_ == uuid) {
73  this->set_found_(true);
74  return true;
75  }
76  }
77  break;
79  if (!device.get_ibeacon().has_value()) {
80  return false;
81  }
82 
83  auto ibeacon = device.get_ibeacon().value();
84 
85  if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
86  return false;
87  }
88 
89  if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
90  return false;
91  }
92 
93  if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
94  return false;
95  }
96 
97  this->set_found_(true);
98  return true;
99  }
100  return false;
101  }
102 
103  void loop() override {
104  if (this->found_ && this->last_seen_ + this->timeout_ < millis())
105  this->set_found_(false);
106  }
107  void dump_config() override;
108  float get_setup_priority() const override { return setup_priority::DATA; }
109 
110  protected:
111  void set_found_(bool state) {
112  this->found_ = state;
113  if (state)
114  this->last_seen_ = millis();
115  this->publish_state(state);
116  }
119 
120  uint64_t address_;
121  uint8_t *irk_;
122 
124 
126  uint16_t ibeacon_major_{0};
127  uint16_t ibeacon_minor_{0};
128 
130 
131  bool check_ibeacon_major_{false};
132  bool check_ibeacon_minor_{false};
133  bool check_minimum_rssi_{false};
134 
135  bool found_{false};
136  uint32_t last_seen_{};
137  uint32_t timeout_{};
138 };
139 
140 } // namespace ble_presence
141 } // namespace esphome
142 
143 #endif
optional< ESPBLEiBeacon > get_ibeacon() const
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
const std::vector< ESPBTUUID > & get_service_uuids() const
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
void publish_state(bool state)
Publish a new state to the front-end.
bool resolve_irk(const uint8_t *irk) const
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_
uint8_t address
Definition: bl0906.h:211
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:171