ESPHome  2022.12.8
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 on_scan_end() override {
45  if (!this->found_)
46  this->publish_state(false);
47  this->found_ = false;
48  }
49  bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
50  switch (this->match_by_) {
52  if (device.address_uint64() == this->address_) {
53  this->publish_state(true);
54  this->found_ = true;
55  return true;
56  }
57  break;
59  for (auto uuid : device.get_service_uuids()) {
60  if (this->uuid_ == uuid) {
61  this->publish_state(true);
62  this->found_ = true;
63  return true;
64  }
65  }
66  break;
68  if (!device.get_ibeacon().has_value()) {
69  return false;
70  }
71 
72  auto ibeacon = device.get_ibeacon().value();
73 
74  if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
75  return false;
76  }
77 
78  if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
79  return false;
80  }
81 
82  if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
83  return false;
84  }
85 
86  this->publish_state(true);
87  this->found_ = true;
88  return true;
89  }
90  return false;
91  }
92  void dump_config() override;
93  float get_setup_priority() const override { return setup_priority::DATA; }
94 
95  protected:
98 
99  bool found_{false};
100 
101  uint64_t address_;
102 
104 
106  uint16_t ibeacon_major_;
108  uint16_t ibeacon_minor_;
110 };
111 
112 } // namespace ble_presence
113 } // namespace esphome
114 
115 #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_raw(const uint8_t *data)
void publish_state(bool state)
Publish a new state to the front-end.
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: a4988.cpp:4
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_