ESPHome 2026.12.0-dev
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). Also holds start_scan(), the body of each tracker's start_scan action.
6
7#pragma once
8
9#include "ble_device.h"
10
13
15
19template<typename Tracker> void start_scan(Tracker *tracker, bool continuous) {
20 if (tracker->scan_running()) {
21 if (continuous != tracker->scan_continuous()) {
22 tracker->set_scan_continuous(continuous);
23 tracker->restart_scan_duration();
24 }
25 return;
26 }
27 tracker->set_scan_continuous(continuous);
28 tracker->start_scan();
29}
30
32inline bool mac_in_table(const uint64_t *table, uint64_t address) {
33 for (; *table != 0; table++) {
34 if (*table == address)
35 return true;
36 }
37 return false;
38}
39
40// on_ble_advertise: fires on every BLE advertisement, optionally filtered to one or more MACs.
41class ESPBTAdvertiseTrigger final : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
42 public:
43 template<typename Hub> explicit ESPBTAdvertiseTrigger(Hub *parent) { parent->register_listener(this); }
44
46 void set_addresses(const uint64_t *addresses) { this->addresses_ = addresses; }
47
48 bool parse_device(const ESPBTDevice &device) override {
49 if (this->addresses_ != nullptr && !mac_in_table(this->addresses_, device.address_uint64()))
50 return false;
51 this->trigger(device);
52 return true;
53 }
54
55 protected:
56 const uint64_t *addresses_{nullptr};
57};
58
61 uint64_t address; // MAC, checked only when has_address is set
63 bool manufacturer; // match manufacturer data instead of service data
65};
66
67// on_ble_service_data_advertise / on_ble_manufacturer_data_advertise: fires with the data of the
68// first entry whose UUID matches, optionally only for one MAC.
69class BLEAdvertiseDataTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
70 public:
71 template<typename Hub> BLEAdvertiseDataTrigger(Hub *parent, const AdvDataMatch *match) : match_(match) {
72 parent->register_listener(this);
73 }
74
75 bool parse_device(const ESPBTDevice &device) override {
76 const AdvDataMatch &match = *this->match_;
77 if (match.has_address && device.address_uint64() != match.address)
78 return false;
79 const adv_data_t *data =
81 if (data == nullptr)
82 return false;
83 this->trigger(*data);
84 return true;
85 }
86
87 protected:
89};
90
91// on_scan_end: fires whenever a scan period ends (duration elapsed or stop
92// requested). A listener whose on_scan_end() hook fires the trigger — never
93// claims devices (parse_device always returns false).
94class BLEEndOfScanTrigger final : public Trigger<>, public ESPBTDeviceListener {
95 public:
96 template<typename Hub> explicit BLEEndOfScanTrigger(Hub *parent) { parent->register_listener(this); }
97
98 bool parse_device(const ESPBTDevice &device) override { return false; }
99 void on_scan_end() override { this->trigger(); }
100};
101
102} // namespace esphome::ble_device_base
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:588
BLEAdvertiseDataTrigger(Hub *parent, const AdvDataMatch *match)
Definition automation.h:71
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:75
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:98
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:48
void set_addresses(const uint64_t *addresses)
Table of MACs ended by 0; must outlive the trigger.
Definition automation.h:46
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:261
const std::vector< ServiceData > & get_manufacturer_datas() const
Definition ble_device.h:260
void start_scan(Tracker *tracker, bool continuous)
Codegen use only: start a scan on tracker, continuous or one-shot.
Definition automation.h:19
std::vector< uint8_t > adv_data_t
Definition ble_device.h:38
const adv_data_t * find_adv_data(const std::vector< ServiceData > &datas, const ESPBTUUID &uuid)
First entry of datas with this UUID, or nullptr.
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:32
Filter of one service or manufacturer data trigger; codegen emits one constexpr per distinct filter.
Definition automation.h:60