ESPHome  2022.6.3
automation.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #ifdef USE_ESP32
7 
8 namespace esphome {
9 namespace esp32_ble_tracker {
10 class ESPBTAdvertiseTrigger : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
11  public:
12  explicit ESPBTAdvertiseTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
13  void set_address(uint64_t address) { this->address_ = address; }
14 
15  bool parse_device(const ESPBTDevice &device) override {
16  if (this->address_ && device.address_uint64() != this->address_) {
17  return false;
18  }
19  this->trigger(device);
20  return true;
21  }
22 
23  protected:
24  uint64_t address_ = 0;
25 };
26 
27 class BLEServiceDataAdvertiseTrigger : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
28  public:
30  void set_address(uint64_t address) { this->address_ = address; }
31  void set_service_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
32  void set_service_uuid32(uint32_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(uuid); }
33  void set_service_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
34 
35  bool parse_device(const ESPBTDevice &device) override {
36  if (this->address_ && device.address_uint64() != this->address_) {
37  return false;
38  }
39  for (auto &service_data : device.get_service_datas()) {
40  if (service_data.uuid == this->uuid_) {
41  this->trigger(service_data.data);
42  return true;
43  }
44  }
45  return false;
46  }
47 
48  protected:
49  uint64_t address_ = 0;
51 };
52 
53 class BLEManufacturerDataAdvertiseTrigger : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
54  public:
56  void set_address(uint64_t address) { this->address_ = address; }
57  void set_manufacturer_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
58  void set_manufacturer_uuid32(uint32_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(uuid); }
59  void set_manufacturer_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
60 
61  bool parse_device(const ESPBTDevice &device) override {
62  if (this->address_ && device.address_uint64() != this->address_) {
63  return false;
64  }
65  for (auto &manufacturer_data : device.get_manufacturer_datas()) {
66  if (manufacturer_data.uuid == this->uuid_) {
67  this->trigger(manufacturer_data.data);
68  return true;
69  }
70  }
71  return false;
72  }
73 
74  protected:
75  uint64_t address_ = 0;
77 };
78 
79 } // namespace esp32_ble_tracker
80 } // namespace esphome
81 
82 #endif
ESPBTAdvertiseTrigger(ESP32BLETracker *parent)
Definition: automation.h:12
void register_listener(ESPBTDeviceListener *listener)
const std::vector< ServiceData > & get_manufacturer_datas() const
static ESPBTUUID from_raw(const uint8_t *data)
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:61
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:15
const std::vector< ServiceData > & get_service_datas() const
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:35
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: a4988.cpp:4