ESPHome  2024.3.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_service_uuid16(uint16_t uuid) {
21  }
22  void set_service_uuid32(uint32_t uuid) {
25  }
26  void set_service_uuid128(uint8_t *uuid) {
29  }
30  void set_ibeacon_uuid(uint8_t *uuid) {
33  }
34  void set_ibeacon_major(uint16_t major) {
35  this->check_ibeacon_major_ = true;
36  this->ibeacon_major_ = major;
37  }
38  void set_ibeacon_minor(uint16_t minor) {
39  this->check_ibeacon_minor_ = true;
40  this->ibeacon_minor_ = minor;
41  }
42  void on_scan_end() override {
43  if (!this->found_)
44  this->publish_state(NAN);
45  this->found_ = false;
46  }
47  bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
48  switch (this->match_by_) {
50  if (device.address_uint64() == this->address_) {
51  this->publish_state(device.get_rssi());
52  this->found_ = true;
53  return true;
54  }
55  break;
57  for (auto uuid : device.get_service_uuids()) {
58  if (this->uuid_ == uuid) {
59  this->publish_state(device.get_rssi());
60  this->found_ = true;
61  return true;
62  }
63  }
64  break;
66  if (!device.get_ibeacon().has_value()) {
67  return false;
68  }
69 
70  auto ibeacon = device.get_ibeacon().value();
71 
72  if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
73  return false;
74  }
75 
76  if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
77  return false;
78  }
79 
80  if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
81  return false;
82  }
83 
84  this->publish_state(device.get_rssi());
85  this->found_ = true;
86  return true;
87  }
88  return false;
89  }
90  void dump_config() override;
91  float get_setup_priority() const override { return setup_priority::DATA; }
92 
93  protected:
96 
97  bool found_{false};
98 
99  uint64_t address_;
100 
102 
104  uint16_t ibeacon_major_;
105  uint16_t ibeacon_minor_;
106 
109 };
110 
111 } // namespace ble_rssi
112 } // namespace esphome
113 
114 #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)
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
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:164