ESPHome 2025.5.0
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 {
10namespace ble_rssi {
11
13 public:
14 void set_address(uint64_t address) {
16 this->address_ = address;
17 }
18 void set_irk(uint8_t *irk) {
19 this->match_by_ = MATCH_BY_IRK;
20 this->irk_ = irk;
21 }
22 void set_service_uuid16(uint16_t uuid) {
24 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint16(uuid);
25 }
26 void set_service_uuid32(uint32_t uuid) {
28 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_uint32(uuid);
29 }
30 void set_service_uuid128(uint8_t *uuid) {
32 this->uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
33 }
34 void set_ibeacon_uuid(uint8_t *uuid) {
36 this->ibeacon_uuid_ = esp32_ble_tracker::ESPBTUUID::from_raw(uuid);
37 }
38 void set_ibeacon_major(uint16_t major) {
39 this->check_ibeacon_major_ = true;
40 this->ibeacon_major_ = major;
41 }
42 void set_ibeacon_minor(uint16_t minor) {
43 this->check_ibeacon_minor_ = true;
44 this->ibeacon_minor_ = minor;
45 }
46 void on_scan_end() override {
47 if (!this->found_)
48 this->publish_state(NAN);
49 this->found_ = false;
50 }
51 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
52 switch (this->match_by_) {
54 if (device.address_uint64() == this->address_) {
55 this->publish_state(device.get_rssi());
56 this->found_ = true;
57 return true;
58 }
59 break;
60 case MATCH_BY_IRK:
61 if (device.resolve_irk(this->irk_)) {
62 this->publish_state(device.get_rssi());
63 this->found_ = true;
64 return true;
65 }
66 break;
68 for (auto uuid : device.get_service_uuids()) {
69 if (this->uuid_ == uuid) {
70 this->publish_state(device.get_rssi());
71 this->found_ = true;
72 return true;
73 }
74 }
75 break;
77 if (!device.get_ibeacon().has_value()) {
78 return false;
79 }
80
81 auto ibeacon = device.get_ibeacon().value();
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 float get_setup_priority() const override { return setup_priority::DATA; }
103
104 protected:
107
108 bool found_{false};
109
110 uint64_t address_;
111 uint8_t *irk_;
112
114
118
121};
122
123} // namespace ble_rssi
124} // namespace esphome
125
126#endif
uint8_t address
Definition bl0906.h:4
void set_service_uuid128(uint8_t *uuid)
float get_setup_priority() const override
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:170
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:57
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7