ESPHome  2023.9.3
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_service_uuid16(uint16_t uuid) {
23  }
24  void set_service_uuid32(uint32_t uuid) {
27  }
28  void set_service_uuid128(uint8_t *uuid) {
31  }
32  void set_ibeacon_uuid(uint8_t *uuid) {
35  }
36  void set_ibeacon_major(uint16_t major) {
37  this->check_ibeacon_major_ = true;
38  this->ibeacon_major_ = major;
39  }
40  void set_ibeacon_minor(uint16_t minor) {
41  this->check_ibeacon_minor_ = true;
42  this->ibeacon_minor_ = minor;
43  }
44  void set_minimum_rssi(int rssi) {
45  this->check_minimum_rssi_ = true;
46  this->minimum_rssi_ = rssi;
47  }
48  void on_scan_end() override {
49  if (!this->found_)
50  this->publish_state(false);
51  this->found_ = false;
52  }
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->publish_state(true);
61  this->found_ = true;
62  return true;
63  }
64  break;
66  for (auto uuid : device.get_service_uuids()) {
67  if (this->uuid_ == uuid) {
68  this->publish_state(true);
69  this->found_ = true;
70  return true;
71  }
72  }
73  break;
75  if (!device.get_ibeacon().has_value()) {
76  return false;
77  }
78 
79  auto ibeacon = device.get_ibeacon().value();
80 
81  if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
82  return false;
83  }
84 
85  if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
86  return false;
87  }
88 
89  if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
90  return false;
91  }
92 
93  this->publish_state(true);
94  this->found_ = true;
95  return true;
96  }
97  return false;
98  }
99  void dump_config() override;
100  float get_setup_priority() const override { return setup_priority::DATA; }
101 
102  protected:
105 
106  uint64_t address_;
107 
109 
111  uint16_t ibeacon_major_{0};
112  uint16_t ibeacon_minor_{0};
113 
115 
116  bool check_ibeacon_major_{false};
117  bool check_ibeacon_minor_{false};
118  bool check_minimum_rssi_{false};
119 
120  bool found_{false};
121 };
122 
123 } // namespace ble_presence
124 } // namespace esphome
125 
126 #endif
optional< ESPBLEiBeacon > get_ibeacon() const
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
const std::vector< ESPBTUUID > & get_service_uuids() const
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
void publish_state(bool state)
Publish a new state to the front-end.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:164