ESPHome  2022.12.8
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 
80  public:
81  explicit BLEEndOfScanTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
82 
83  bool parse_device(const ESPBTDevice &device) override { return false; }
84  void on_scan_end() override { this->trigger(); }
85 };
86 
87 template<typename... Ts> class ESP32BLEStartScanAction : public Action<Ts...> {
88  public:
90  TEMPLATABLE_VALUE(bool, continuous)
91  void play(Ts... x) override {
92  this->parent_->set_scan_continuous(this->continuous_.value(x...));
93  this->parent_->start_scan();
94  }
95 
96  protected:
98 };
99 
100 template<typename... Ts> class ESP32BLEStopScanAction : public Action<Ts...>, public Parented<ESP32BLETracker> {
101  public:
102  void play(Ts... x) override { this->parent_->stop_scan(); }
103 };
104 
105 } // namespace esp32_ble_tracker
106 } // namespace esphome
107 
108 #endif
ESPBTAdvertiseTrigger(ESP32BLETracker *parent)
Definition: automation.h:12
void register_listener(ESPBTDeviceListener *listener)
BLEEndOfScanTrigger(ESP32BLETracker *parent)
Definition: automation.h:81
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:83
TEMPLATABLE_VALUE(bool, continuous) void play(Ts... x) override
Definition: automation.h:90
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
void set_scan_continuous(bool scan_continuous)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:500