ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ble_presence_device.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_ESP32
8
9namespace esphome::ble_presence {
10
13 public Component {
14 public:
15 void set_address(uint64_t address) {
17 this->address_ = address;
18 }
19 void set_irk(uint8_t *irk) {
20 this->match_by_ = MATCH_BY_IRK;
21 this->irk_ = irk;
22 }
23 void set_service_uuid16(uint16_t uuid) {
25 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint16(uuid);
26 }
29 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint32(uuid);
30 }
31 void set_service_uuid128(uint8_t *uuid) {
33 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
34 }
35 void set_ibeacon_uuid(uint8_t *uuid) {
37 this->ibeacon_uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
38 }
39 void set_ibeacon_major(uint16_t major) {
40 this->check_ibeacon_major_ = true;
41 this->ibeacon_major_ = major;
42 }
43 void set_ibeacon_minor(uint16_t minor) {
44 this->check_ibeacon_minor_ = true;
45 this->ibeacon_minor_ = minor;
46 }
47 void set_minimum_rssi(int rssi) {
48 this->check_minimum_rssi_ = true;
49 this->minimum_rssi_ = rssi;
50 }
51 void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
52 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
53 if (this->check_minimum_rssi_ && this->minimum_rssi_ > device.get_rssi()) {
54 return false;
55 }
56 switch (this->match_by_) {
58 if (device.address_uint64() == this->address_) {
59 this->set_found_(true);
60 return true;
61 }
62 break;
63 case MATCH_BY_IRK:
64 if (device.resolve_irk(this->irk_)) {
65 this->set_found_(true);
66 return true;
67 }
68 break;
70 for (auto uuid : device.get_service_uuids()) {
71 if (this->uuid_ == uuid) {
72 this->set_found_(true);
73 return true;
74 }
75 }
76 break;
78 auto maybe_ibeacon = device.get_ibeacon();
79 if (!maybe_ibeacon.has_value()) {
80 return false;
81 }
82
83 auto ibeacon = *maybe_ibeacon;
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_ && millis() - this->last_seen_ > this->timeout_)
105 this->set_found_(false);
106 }
107 void dump_config() override;
108
109 protected:
110 void set_found_(bool state) {
111 this->found_ = state;
112 if (state)
113 this->last_seen_ = millis();
114 this->publish_state(state);
115 }
118
119 uint64_t address_;
120 uint8_t *irk_;
121
123
125 uint16_t ibeacon_major_{0};
126 uint16_t ibeacon_minor_{0};
127
129
133
134 bool found_{false};
137};
138
139} // namespace esphome::ble_presence
140
141#endif
uint8_t address
Definition bl0906.h:4
void publish_state(bool new_state)
Publish a new state to the front-end.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:145
bool resolve_irk(const uint8_t *irk) const
const std::vector< ESPBTUUID > & get_service_uuids() const
optional< ESPBLEiBeacon > get_ibeacon() const
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t