ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ble_rssi_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_ESP32
8
9namespace esphome::ble_rssi {
10
12 public:
13 void set_address(uint64_t address) {
15 this->address_ = address;
16 }
17 void set_irk(uint8_t *irk) {
18 this->match_by_ = MATCH_BY_IRK;
19 this->irk_ = irk;
20 }
21 void set_service_uuid16(uint16_t uuid) {
23 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint16(uuid);
24 }
27 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint32(uuid);
28 }
29 void set_service_uuid128(uint8_t *uuid) {
31 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
32 }
33 void set_ibeacon_uuid(uint8_t *uuid) {
35 this->ibeacon_uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
36 }
37 void set_ibeacon_major(uint16_t major) {
38 this->check_ibeacon_major_ = true;
39 this->ibeacon_major_ = major;
40 }
41 void set_ibeacon_minor(uint16_t minor) {
42 this->check_ibeacon_minor_ = true;
43 this->ibeacon_minor_ = minor;
44 }
45 void on_scan_end() override {
46 if (!this->found_)
47 this->publish_state(NAN);
48 this->found_ = false;
49 }
50 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
51 switch (this->match_by_) {
53 if (device.address_uint64() == this->address_) {
54 this->publish_state(device.get_rssi());
55 this->found_ = true;
56 return true;
57 }
58 break;
59 case MATCH_BY_IRK:
60 if (device.resolve_irk(this->irk_)) {
61 this->publish_state(device.get_rssi());
62 this->found_ = true;
63 return true;
64 }
65 break;
67 for (auto uuid : device.get_service_uuids()) {
68 if (this->uuid_ == uuid) {
69 this->publish_state(device.get_rssi());
70 this->found_ = true;
71 return true;
72 }
73 }
74 break;
76 auto maybe_ibeacon = device.get_ibeacon();
77 if (!maybe_ibeacon.has_value()) {
78 return false;
79 }
80
81 auto ibeacon = *maybe_ibeacon;
82
83 if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
84 return false;
85 }
86
87 if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
88 return false;
89 }
90
91 if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
92 return false;
93 }
94
95 this->publish_state(device.get_rssi());
96 this->found_ = true;
97 return true;
98 }
99 return false;
100 }
101 void dump_config() override;
102
103 protected:
106
107 bool found_{false};
108
109 uint64_t address_;
110 uint8_t *irk_;
111
113
117
120};
121
122} // namespace esphome::ble_rssi
123
124#endif
uint8_t address
Definition bl0906.h:4
void set_service_uuid128(uint8_t *uuid)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void set_service_uuid32(uint32_t uuid)
esp32_ble_tracker::ESPBTUUID uuid_
void set_ibeacon_minor(uint16_t minor)
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_
void set_address(uint64_t address)
void set_service_uuid16(uint16_t uuid)
void set_ibeacon_uuid(uint8_t *uuid)
void set_ibeacon_major(uint16_t major)
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
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
static void uint32_t