ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1// Platform-neutral BLE advertisement triggers: ESPBTDeviceListener subclasses
2// registered on a BLEHub, exposed by each tracker under its own automation
3// names. parse_device()'s return feeds the "Found device" suppression.
4// Constructors are templated on the hub type so this header also builds with
5// no tracker present (host unit tests).
6
7#pragma once
8
9#include "ble_device.h"
10
13
14#include <algorithm>
15#include <initializer_list>
16
18
19// on_ble_advertise: fires on every BLE advertisement, optionally filtered to one or more MACs.
20class ESPBTAdvertiseTrigger final : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
21 public:
22 template<typename Hub> explicit ESPBTAdvertiseTrigger(Hub *parent) { parent->register_listener(this); }
23
24 void set_addresses(std::initializer_list<uint64_t> addresses) { this->addresses_ = addresses; }
25
26 bool parse_device(const ESPBTDevice &device) override {
27 if (!this->addresses_.empty() && std::find(this->addresses_.begin(), this->addresses_.end(),
28 device.address_uint64()) == this->addresses_.end()) {
29 return false;
30 }
31 this->trigger(device);
32 return true;
33 }
34
35 protected:
37};
38
39// on_ble_service_data_advertise: fires when an advertisement contains service
40// data for the given UUID. Optional single-MAC filter.
41class BLEServiceDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
42 public:
43 template<typename Hub> explicit BLEServiceDataAdvertiseTrigger(Hub *parent) { parent->register_listener(this); }
44
45 void set_service_uuid16(uint64_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(static_cast<uint16_t>(uuid)); }
46 void set_service_uuid32(uint64_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(static_cast<uint32_t>(uuid)); }
47 void set_service_uuid128(const uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
48
49 void set_address(uint64_t address) {
50 this->address_ = address;
51 this->has_address_ = true;
52 }
53
54 bool parse_device(const ESPBTDevice &device) override {
55 if (this->has_address_ && device.address_uint64() != this->address_) {
56 return false;
57 }
58 for (const auto &sd : device.get_service_datas()) {
59 if (sd.uuid == this->uuid_) {
60 this->trigger(sd.data);
61 return true;
62 }
63 }
64 return false;
65 }
66
67 protected:
69 uint64_t address_{0};
70 bool has_address_{false};
71};
72
73// on_ble_manufacturer_data_advertise: fires when an advertisement contains
74// manufacturer data for the given ID. Optional single-MAC filter.
75class BLEManufacturerDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
76 public:
77 template<typename Hub> explicit BLEManufacturerDataAdvertiseTrigger(Hub *parent) { parent->register_listener(this); }
78
79 void set_manufacturer_uuid16(uint64_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(static_cast<uint16_t>(uuid)); }
80 void set_manufacturer_uuid32(uint64_t uuid) { this->uuid_ = ESPBTUUID::from_uint32(static_cast<uint32_t>(uuid)); }
81 void set_manufacturer_uuid128(const uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
82
83 void set_address(uint64_t address) {
84 this->address_ = address;
85 this->has_address_ = true;
86 }
87
88 bool parse_device(const ESPBTDevice &device) override {
89 if (this->has_address_ && device.address_uint64() != this->address_) {
90 return false;
91 }
92 for (const auto &md : device.get_manufacturer_datas()) {
93 if (md.uuid == this->uuid_) {
94 this->trigger(md.data);
95 return true;
96 }
97 }
98 return false;
99 }
100
101 protected:
103 uint64_t address_{0};
104 bool has_address_{false};
105};
106
107// on_scan_end: fires whenever a scan period ends (duration elapsed or stop
108// requested). A listener whose on_scan_end() hook fires the trigger — never
109// claims devices (parse_device always returns false).
110class BLEEndOfScanTrigger final : public Trigger<>, public ESPBTDeviceListener {
111 public:
112 template<typename Hub> explicit BLEEndOfScanTrigger(Hub *parent) { parent->register_listener(this); }
113
114 bool parse_device(const ESPBTDevice &device) override { return false; }
115 void on_scan_end() override { this->trigger(); }
116};
117
118} // namespace esphome::ble_device_base
uint8_t address
Definition bl0906.h:4
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:544
bool empty() const
Definition helpers.h:711
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:461
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:114
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:88
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:54
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:26
void set_addresses(std::initializer_list< uint64_t > addresses)
Definition automation.h:24
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)
static void uint32_t