ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_ESP32
8
10#ifdef USE_ESP32_BLE_DEVICE
11class ESPBTAdvertiseTrigger final : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
12 public:
13 explicit ESPBTAdvertiseTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
15 void set_addresses(const uint64_t *addresses) { this->addresses_ = addresses; }
16
17 bool parse_device(const ESPBTDevice &device) override {
18 if (this->addresses_ != nullptr && !ble_device_base::mac_in_table(this->addresses_, device.address_uint64()))
19 return false;
20 this->trigger(device);
21 return true;
22 }
23
24 protected:
25 const uint64_t *addresses_{nullptr};
26};
27
28class BLEServiceDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
29 public:
31 void set_address(uint64_t address) { this->address_ = address; }
32 void set_service_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
34 void set_service_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
35
36 bool parse_device(const ESPBTDevice &device) override {
37 if (this->address_ && device.address_uint64() != this->address_) {
38 return false;
39 }
40 for (auto &service_data : device.get_service_datas()) {
41 if (service_data.uuid == this->uuid_) {
42 this->trigger(service_data.data);
43 return true;
44 }
45 }
46 return false;
47 }
48
49 protected:
50 uint64_t address_ = 0;
52};
53
54class BLEManufacturerDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
55 public:
57 void set_address(uint64_t address) { this->address_ = address; }
58 void set_manufacturer_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
60 void set_manufacturer_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
61
62 bool parse_device(const ESPBTDevice &device) override {
63 if (this->address_ && device.address_uint64() != this->address_) {
64 return false;
65 }
66 for (auto &manufacturer_data : device.get_manufacturer_datas()) {
67 if (manufacturer_data.uuid == this->uuid_) {
68 this->trigger(manufacturer_data.data);
69 return true;
70 }
71 }
72 return false;
73 }
74
75 protected:
76 uint64_t address_ = 0;
78};
79
80#endif // USE_ESP32_BLE_DEVICE
81
82class BLEEndOfScanTrigger final : public Trigger<>, public ESPBTDeviceListener {
83 public:
84 explicit BLEEndOfScanTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
85
86#ifdef USE_ESP32_BLE_DEVICE
87 bool parse_device(const ESPBTDevice &device) override { return false; }
88#endif
89 void on_scan_end() override { this->trigger(); }
90};
91
92} // namespace esphome::esp32_ble_tracker
93
94#endif
uint8_t address
Definition bl0906.h:4
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:556
uint64_t address_uint64() const
Return MAC as packed uint64 (byte 0 in LSB — matches esp32's address_uint64).
const std::vector< ServiceData > & get_service_datas() const
Definition ble_device.h:225
const std::vector< ServiceData > & get_manufacturer_datas() const
Definition ble_device.h:224
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_raw(const uint8_t *data)
Construct from raw 16-byte little-endian UUID.
static ESPBTUUID from_uint32(uint32_t uuid)
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:87
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:62
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:36
void register_listener(ESPBTDeviceListener *listener)
void set_addresses(const uint64_t *addresses)
Table of MACs ended by 0; must outlive the trigger.
Definition automation.h:15
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:17
bool mac_in_table(const uint64_t *table, uint64_t address)
True if address is in table, a list of MACs ended by 0.
Definition automation.h:17
static void uint32_t