ESPHome 2026.8.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <vector>
7
8#ifdef USE_ESP32
9
11#ifdef USE_ESP32_BLE_DEVICE
12class ESPBTAdvertiseTrigger final : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
13 public:
14 explicit ESPBTAdvertiseTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
15 void set_addresses(std::initializer_list<uint64_t> addresses) { this->address_vec_ = addresses; }
16
17 bool parse_device(const ESPBTDevice &device) override {
18 uint64_t u64_addr = device.address_uint64();
19 if (!address_vec_.empty()) {
20 if (std::find(address_vec_.begin(), address_vec_.end(), u64_addr) == address_vec_.end()) {
21 return false;
22 }
23 }
24
25 this->trigger(device);
26 return true;
27 }
28
29 protected:
30 std::vector<uint64_t> address_vec_;
31};
32
33class BLEServiceDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
34 public:
36 void set_address(uint64_t address) { this->address_ = address; }
37 void set_service_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
39 void set_service_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
40
41 bool parse_device(const ESPBTDevice &device) override {
42 if (this->address_ && device.address_uint64() != this->address_) {
43 return false;
44 }
45 for (auto &service_data : device.get_service_datas()) {
46 if (service_data.uuid == this->uuid_) {
47 this->trigger(service_data.data);
48 return true;
49 }
50 }
51 return false;
52 }
53
54 protected:
55 uint64_t address_ = 0;
57};
58
59class BLEManufacturerDataAdvertiseTrigger final : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {
60 public:
62 void set_address(uint64_t address) { this->address_ = address; }
63 void set_manufacturer_uuid16(uint16_t uuid) { this->uuid_ = ESPBTUUID::from_uint16(uuid); }
65 void set_manufacturer_uuid128(uint8_t *uuid) { this->uuid_ = ESPBTUUID::from_raw(uuid); }
66
67 bool parse_device(const ESPBTDevice &device) override {
68 if (this->address_ && device.address_uint64() != this->address_) {
69 return false;
70 }
71 for (auto &manufacturer_data : device.get_manufacturer_datas()) {
72 if (manufacturer_data.uuid == this->uuid_) {
73 this->trigger(manufacturer_data.data);
74 return true;
75 }
76 }
77 return false;
78 }
79
80 protected:
81 uint64_t address_ = 0;
83};
84
85#endif // USE_ESP32_BLE_DEVICE
86
87class BLEEndOfScanTrigger final : public Trigger<>, public ESPBTDeviceListener {
88 public:
89 explicit BLEEndOfScanTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
90
91#ifdef USE_ESP32_BLE_DEVICE
92 bool parse_device(const ESPBTDevice &device) override { return false; }
93#endif
94 void on_scan_end() override { this->trigger(); }
95};
96
97template<typename... Ts> class ESP32BLEStartScanAction final : public Action<Ts...> {
98 public:
100 TEMPLATABLE_VALUE(bool, continuous)
101 void play(const Ts &...x) override {
102 this->parent_->set_scan_continuous(this->continuous_.value(x...));
103 // Only call start_scan() if scanner is IDLE
104 // For other states (STARTING, RUNNING, STOPPING, FAILED), the normal state
105 // machine flow will eventually transition back to IDLE, at which point
106 // loop() will see scan_continuous_ and restart scanning if it is true.
107 if (this->parent_->get_scanner_state() == ScannerState::IDLE) {
108 this->parent_->start_scan();
109 }
110 }
111
112 protected:
114};
115
116template<typename... Ts> class ESP32BLEStopScanAction final : public Action<Ts...>, public Parented<ESP32BLETracker> {
117 public:
118 void play(const Ts &...x) override { this->parent_->stop_scan(); }
119};
120
121} // namespace esphome::esp32_ble_tracker
122
123#endif
uint8_t address
Definition bl0906.h:4
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1907
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:461
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:92
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:67
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:41
TEMPLATABLE_VALUE(bool, continuous) void play(const Ts &...x) override
Definition automation.h:100
void register_listener(ESPBTDeviceListener *listener)
void set_addresses(std::initializer_list< uint64_t > addresses)
Definition automation.h:15
bool parse_device(const ESPBTDevice &device) override
Definition automation.h:17
static void uint32_t
uint16_t x
Definition tt21100.cpp:5