ESPHome  2024.4.0
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_addresses(const std::vector<uint64_t> &addresses) { this->address_vec_ = addresses; }
14 
15  bool parse_device(const ESPBTDevice &device) override {
16  uint64_t u64_addr = device.address_uint64();
17  if (!address_vec_.empty()) {
18  if (std::find(address_vec_.begin(), address_vec_.end(), u64_addr) == address_vec_.end()) {
19  return false;
20  }
21  }
22 
23  this->trigger(device);
24  return true;
25  }
26 
27  protected:
28  std::vector<uint64_t> address_vec_;
29 };
30 
31 class BLEServiceDataAdvertiseTrigger : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
32  public:
34  void set_address(uint64_t address) { this->address_ = address; }
35  void set_service_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
36  void set_service_uuid32(uint32_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(uuid); }
37  void set_service_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
38 
39  bool parse_device(const ESPBTDevice &device) override {
40  if (this->address_ && device.address_uint64() != this->address_) {
41  return false;
42  }
43  for (auto &service_data : device.get_service_datas()) {
44  if (service_data.uuid == this->uuid_) {
45  this->trigger(service_data.data);
46  return true;
47  }
48  }
49  return false;
50  }
51 
52  protected:
53  uint64_t address_ = 0;
55 };
56 
57 class BLEManufacturerDataAdvertiseTrigger : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
58  public:
60  void set_address(uint64_t address) { this->address_ = address; }
61  void set_manufacturer_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
62  void set_manufacturer_uuid32(uint32_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(uuid); }
63  void set_manufacturer_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
64 
65  bool parse_device(const ESPBTDevice &device) override {
66  if (this->address_ && device.address_uint64() != this->address_) {
67  return false;
68  }
69  for (auto &manufacturer_data : device.get_manufacturer_datas()) {
70  if (manufacturer_data.uuid == this->uuid_) {
71  this->trigger(manufacturer_data.data);
72  return true;
73  }
74  }
75  return false;
76  }
77 
78  protected:
79  uint64_t address_ = 0;
81 };
82 
84  public:
85  explicit BLEEndOfScanTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
86 
87  bool parse_device(const ESPBTDevice &device) override { return false; }
88  void on_scan_end() override { this->trigger(); }
89 };
90 
91 template<typename... Ts> class ESP32BLEStartScanAction : public Action<Ts...> {
92  public:
94  TEMPLATABLE_VALUE(bool, continuous)
95  void play(Ts... x) override {
96  this->parent_->set_scan_continuous(this->continuous_.value(x...));
97  this->parent_->start_scan();
98  }
99 
100  protected:
102 };
103 
104 template<typename... Ts> class ESP32BLEStopScanAction : public Action<Ts...>, public Parented<ESP32BLETracker> {
105  public:
106  void play(Ts... x) override { this->parent_->stop_scan(); }
107 };
108 
109 } // namespace esp32_ble_tracker
110 } // namespace esphome
111 
112 #endif
ESPBTAdvertiseTrigger(ESP32BLETracker *parent)
Definition: automation.h:12
void register_listener(ESPBTDeviceListener *listener)
uint16_t x
Definition: tt21100.cpp:17
BLEEndOfScanTrigger(ESP32BLETracker *parent)
Definition: automation.h:85
const std::vector< ServiceData > & get_manufacturer_datas() const
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:65
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:87
TEMPLATABLE_VALUE(bool, continuous) void play(Ts... x) override
Definition: automation.h:94
static ESPBTUUID from_uint32(uint32_t uuid)
Definition: ble_uuid.cpp:22
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:15
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
const std::vector< ServiceData > & get_service_datas() const
bool parse_device(const ESPBTDevice &device) override
Definition: automation.h:39
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
void set_addresses(const std::vector< uint64_t > &addresses)
Definition: automation.h:13
void set_scan_continuous(bool scan_continuous)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515