ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
ble_rssi_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7// No platform #ifdef: ble_device_base provides the BLE types on every platform,
8// and this component is only compiled when configured — which requires a BLE
9// hub — so it builds on any platform with a BLEHub tracker without a per-chip
10// guard.
11namespace esphome::ble_rssi {
12
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 }
31 void set_service_uuid128(uint8_t *uuid) {
34 }
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 on_scan_end() override {
48 if (!this->found_)
49 this->publish_state(NAN);
50 this->found_ = false;
51 }
52 bool parse_device(const ble_device_base::ESPBTDevice &device) override {
53 switch (this->match_by_) {
55 if (device.address_uint64() == this->address_) {
56 this->publish_state(device.get_rssi());
57 this->found_ = true;
58 return true;
59 }
60 break;
61 case MATCH_BY_IRK:
62 if (device.resolve_irk(this->irk_)) {
63 this->publish_state(device.get_rssi());
64 this->found_ = true;
65 return true;
66 }
67 break;
69 for (auto uuid : device.get_service_uuids()) {
70 if (this->uuid_ == uuid) {
71 this->publish_state(device.get_rssi());
72 this->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->publish_state(device.get_rssi());
98 this->found_ = true;
99 return true;
100 }
101 return false;
102 }
103 void dump_config() override;
104
105 protected:
108
109 bool found_{false};
110
111 uint64_t address_;
112 uint8_t *irk_;
113
115
119
122};
123
124} // namespace esphome::ble_rssi
uint8_t address
Definition bl0906.h:4
const std::vector< ESPBTUUID > & get_service_uuids() const
Definition ble_device.h:223
optional< ESPBLEiBeacon > get_ibeacon() const
uint64_t address_uint64() const
Return MAC as packed uint64 (byte 0 in LSB — matches esp32's address_uint64).
bool resolve_irk(const uint8_t *irk) const
Resolve a Resolvable Private Address against a 16-byte IRK (Bluetooth "ah" function,...
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_raw(const uint8_t *data)
Construct from raw 16-byte little-endian UUID.
static ESPBTUUID from_uint32(uint32_t uuid)
void set_service_uuid128(uint8_t *uuid)
ble_device_base::ESPBTUUID uuid_
void set_service_uuid32(uint32_t uuid)
ble_device_base::ESPBTUUID ibeacon_uuid_
void set_ibeacon_minor(uint16_t minor)
bool parse_device(const ble_device_base::ESPBTDevice &device) override
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)
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