ESPHome  2022.6.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) {
15  this->by_address_ = true;
16  this->address_ = address;
17  }
18  void set_service_uuid16(uint16_t uuid) {
19  this->by_address_ = false;
21  }
22  void set_service_uuid32(uint32_t uuid) {
23  this->by_address_ = false;
25  }
26  void set_service_uuid128(uint8_t *uuid) {
27  this->by_address_ = false;
29  }
30  void on_scan_end() override {
31  if (!this->found_)
32  this->publish_state(NAN);
33  this->found_ = false;
34  }
35  bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
36  if (this->by_address_) {
37  if (device.address_uint64() == this->address_) {
38  this->publish_state(device.get_rssi());
39  this->found_ = true;
40  return true;
41  }
42  } else {
43  for (auto uuid : device.get_service_uuids()) {
44  if (this->uuid_ == uuid) {
45  this->publish_state(device.get_rssi());
46  this->found_ = true;
47  return true;
48  }
49  }
50  }
51  return false;
52  }
53  void dump_config() override;
54  float get_setup_priority() const override { return setup_priority::DATA; }
55 
56  protected:
57  bool found_{false};
58  bool by_address_{false};
59  uint64_t address_;
61 };
62 
63 } // namespace ble_rssi
64 } // namespace esphome
65 
66 #endif
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
float get_setup_priority() const override
const std::vector< ESPBTUUID > & get_service_uuids() const
void set_service_uuid32(uint32_t uuid)
static ESPBTUUID from_raw(const uint8_t *data)
void set_address(uint64_t address)
void set_service_uuid16(uint16_t uuid)
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:70
esp32_ble_tracker::ESPBTUUID uuid_
void set_service_uuid128(uint8_t *uuid)
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:47