ESPHome  2024.11.2
ble_rssi_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace 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) {
25  }
26  void set_service_uuid32(uint32_t uuid) {
29  }
30  void set_service_uuid128(uint8_t *uuid) {
33  }
34  void set_ibeacon_uuid(uint8_t *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 
116  uint16_t ibeacon_major_;
117  uint16_t ibeacon_minor_;
118 
121 };
122 
123 } // namespace ble_rssi
124 } // namespace esphome
125 
126 #endif
optional< ESPBLEiBeacon > get_ibeacon() const
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_ibeacon_uuid(uint8_t *uuid)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void set_ibeacon_minor(uint16_t minor)
float get_setup_priority() const override
void set_ibeacon_major(uint16_t major)
const std::vector< ESPBTUUID > & get_service_uuids() const
void set_service_uuid32(uint32_t uuid)
void set_address(uint64_t address)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
void set_service_uuid16(uint16_t uuid)
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
esp32_ble_tracker::ESPBTUUID uuid_
void set_service_uuid128(uint8_t *uuid)
bool resolve_irk(const uint8_t *irk) const
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
Base-class for all sensors.
Definition: sensor.h:57
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:171