ESPHome 2026.6.4
Loading...
Searching...
No Matches
ble_server_automations.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_server.h"
5#include "ble_descriptor.h"
6
8
9#include <vector>
10#include <functional>
11
12#ifdef USE_ESP32
13
14// Interface to interact with ESPHome actions and triggers
16
17using namespace esp32_ble;
18
20 public:
21#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
23 BLECharacteristic *characteristic);
24#endif
25#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
27#endif
28#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
30#endif
31#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
33#endif
34};
35
36#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
37// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
39 public:
40 // Singleton pattern
43 return &instance;
44 }
45 void set_listener(BLECharacteristic *characteristic, const std::function<void()> &pre_notify_listener);
46 bool has_listener(BLECharacteristic *characteristic) { return this->find_listener_(characteristic) != nullptr; }
47 void emit_pre_notify(BLECharacteristic *characteristic) {
48 for (const auto &entry : this->listeners_) {
49 if (entry.characteristic == characteristic) {
50 entry.pre_notify_listener();
51 break;
52 }
53 }
54 }
55
56 private:
57 struct ListenerEntry {
58 BLECharacteristic *characteristic;
59 std::function<void()> pre_notify_listener;
60 };
61 std::vector<ListenerEntry> listeners_;
62
63 ListenerEntry *find_listener_(BLECharacteristic *characteristic);
64 void remove_listener_(BLECharacteristic *characteristic);
65};
66
67template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
68 public:
69 BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
70 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
71 void set_buffer(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
72 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
73 void play(const Ts &...x) override {
74 // If the listener is already set, do nothing
76 return;
77 // Set initial value
78 this->parent_->set_value(this->buffer_.value(x...));
79 // Set the listener for read events
80 // ``mutable`` keeps by-copy captures non-const for triggers passing args by reference
81 // (e.g. climate on_control's ClimateCall&). See #17142.
82 this->parent_->on_read([this, x...](uint16_t id) mutable {
83 // Set the value of the characteristic every time it is read
84 this->parent_->set_value(this->buffer_.value(x...));
85 });
86 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
88 this->parent_, [this, x...]() mutable { this->parent_->set_value(this->buffer_.value(x...)); });
89 }
90
91 protected:
93};
94#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
95
96#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
97template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
98 public:
99 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
100 void play(const Ts &...x) override {
101#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
102 // Call the pre-notify event
104#endif
105 // Notify the characteristic
106 this->parent_->notify();
107 }
108
109 protected:
111};
112#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
113
114#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
115template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
116 public:
118 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
119 void set_buffer(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
120 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
121 void play(const Ts &...x) override { this->parent_->set_value(this->buffer_.value(x...)); }
122
123 protected:
125};
126#endif // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
127
128} // namespace esphome::esp32_ble_server::esp32_ble_server_automations
129
130#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:37
std::vector< uint8_t > get_data()
Definition bytebuffer.h:299
void on_read(std::function< void(uint16_t)> &&callback)
void set_value(std::vector< uint8_t > &&buffer)
void set_listener(BLECharacteristic *characteristic, const std::function< void()> &pre_notify_listener)
static Trigger< std::vector< uint8_t >, uint16_t > * create_characteristic_on_write_trigger(BLECharacteristic *characteristic)
static Trigger< uint16_t > * create_server_on_disconnect_trigger(BLEServer *server)
static Trigger< uint16_t > * create_server_on_connect_trigger(BLEServer *server)
static Trigger< std::vector< uint8_t >, uint16_t > * create_descriptor_on_write_trigger(BLEDescriptor *descriptor)
uint16_t x
Definition tt21100.cpp:5