[esp32_ble_server] Build the write and connect triggers as callback automations (#22456)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2026-11-09 09:22:39 -05:00
1 parent 3325b16c56
commit 935befcbe5
4 files changed
+23 -89

No files matched your search

+23 -27
View File
@@ -130,7 +130,6 @@ BLEServer = esp32_ble_server_ns.class_(
esp32_ble_server_automations_ns = esp32_ble_server_ns.namespace(
"esp32_ble_server_automations"
)
BLETriggers_ns = esp32_ble_server_automations_ns.namespace("BLETriggers")
BLEDescriptor = esp32_ble_server_ns.class_("BLEDescriptor")
BLECharacteristic = esp32_ble_server_ns.class_("BLECharacteristic")
BLEService = esp32_ble_server_ns.class_("BLEService")
@@ -682,6 +681,21 @@ def calculate_num_handles(service_config):
return total
async def _build_on_write_automation(var: MockObj, config: ConfigType) -> None:
"""Run an on_write automation with the written bytes as a vector, as its lambdas expect."""
await automation.build_callback_automation(
var,
"on_write",
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")],
config,
params=[
(cg.std_span.template(cg.uint8.operator("const")), "data"),
(cg.uint16, "id"),
],
forward=["std::vector<uint8_t>(data.begin(), data.end())", "id"],
)
async def to_code_descriptor(descriptor_conf, char_conf, char_var):
value = await parse_value(descriptor_conf[CONF_VALUE], {})
desc_var = cg.new_Pvariable(
@@ -692,14 +706,8 @@ async def to_code_descriptor(descriptor_conf, char_conf, char_var):
)
cg.add(char_var.add_descriptor(desc_var))
cg.add(desc_var.set_value(value))
if CONF_ON_WRITE in descriptor_conf:
on_write_conf = descriptor_conf[CONF_ON_WRITE]
cg.add_define("USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE")
await automation.build_automation(
BLETriggers_ns.create_descriptor_on_write_trigger(desc_var),
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")],
on_write_conf,
)
if (on_write_conf := descriptor_conf.get(CONF_ON_WRITE)) is not None:
await _build_on_write_automation(desc_var, on_write_conf)
async def to_code_characteristic(service_var, char_conf):
@@ -711,14 +719,8 @@ async def to_code_characteristic(service_var, char_conf):
characteristic_permissions(char_conf),
),
)
if CONF_ON_WRITE in char_conf:
on_write_conf = char_conf[CONF_ON_WRITE]
cg.add_define("USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE")
await automation.build_automation(
BLETriggers_ns.create_characteristic_on_write_trigger(char_var),
[(cg.std_vector.template(cg.uint8), "x"), (cg.uint16, "id")],
on_write_conf,
)
if (on_write_conf := char_conf.get(CONF_ON_WRITE)) is not None:
await _build_on_write_automation(char_var, on_write_conf)
if CONF_VALUE in char_conf:
# Check if the value is templated (Lambda)
value_data = char_conf[CONF_VALUE][CONF_DATA]
@@ -836,18 +838,12 @@ async def to_code(config):
else:
cg.add(var.enqueue_start_service(service_var))
if CONF_ON_CONNECT in config:
cg.add_define("USE_ESP32_BLE_SERVER_ON_CONNECT")
await automation.build_automation(
BLETriggers_ns.create_server_on_connect_trigger(var),
[(cg.uint16, "id")],
config[CONF_ON_CONNECT],
await automation.build_callback_automation(
var, "on_connect", [(cg.uint16, "id")], config[CONF_ON_CONNECT]
)
if CONF_ON_DISCONNECT in config:
cg.add_define("USE_ESP32_BLE_SERVER_ON_DISCONNECT")
await automation.build_automation(
BLETriggers_ns.create_server_on_disconnect_trigger(var),
[(cg.uint16, "id")],
config[CONF_ON_DISCONNECT],
await automation.build_callback_automation(
var, "on_disconnect", [(cg.uint16, "id")], config[CONF_ON_DISCONNECT]
)
for entry in _CALLBACK_AUTOMATIONS:
if config.get(entry.conf_key):
@@ -7,47 +7,6 @@ namespace esphome::esp32_ble_server::esp32_ble_server_automations {
using namespace esp32_ble;
#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_characteristic_on_write_trigger(
BLECharacteristic *characteristic) {
Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
new Trigger<std::vector<uint8_t>, uint16_t>();
characteristic->on_write([on_write_trigger](std::span<const uint8_t> data, uint16_t id) {
// Convert span to vector for trigger
on_write_trigger->trigger(std::vector<uint8_t>(data.begin(), data.end()), id);
});
return on_write_trigger;
}
#endif
#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
Trigger<std::vector<uint8_t>, uint16_t> *BLETriggers::create_descriptor_on_write_trigger(BLEDescriptor *descriptor) {
Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
new Trigger<std::vector<uint8_t>, uint16_t>();
descriptor->on_write([on_write_trigger](std::span<const uint8_t> data, uint16_t id) {
// Convert span to vector for trigger
on_write_trigger->trigger(std::vector<uint8_t>(data.begin(), data.end()), id);
});
return on_write_trigger;
}
#endif
#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
Trigger<uint16_t> *BLETriggers::create_server_on_connect_trigger(BLEServer *server) {
Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
server->on_connect([on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); });
return on_connect_trigger;
}
#endif
#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
Trigger<uint16_t> *BLETriggers::create_server_on_disconnect_trigger(BLEServer *server) {
Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
server->on_disconnect([on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); });
return on_disconnect_trigger;
}
#endif
#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic,
const std::function<void()> &pre_notify_listener) {
@@ -26,23 +26,6 @@ template<typename T, typename V, typename... Xs> void apply_value(T *target, con
}
}
class BLETriggers {
public:
#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger(
BLECharacteristic *characteristic);
#endif
#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
static Trigger<std::vector<uint8_t>, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor);
#endif
#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
static Trigger<uint16_t> *create_server_on_connect_trigger(BLEServer *server);
#endif
#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server);
#endif
};
#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
class BLECharacteristicSetValueActionManager {
-4
View File
@@ -541,10 +541,6 @@
#define USE_ESP32_BLE_SOFTWARE_COEXISTENCE
#define USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
#define USE_ESP32_BLE_SERVER_NOTIFY_ACTION
#define USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
#define USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
#define USE_ESP32_BLE_SERVER_ON_CONNECT
#define USE_ESP32_BLE_SERVER_ON_DISCONNECT
#define USE_ESP32_BLE_SERVER_ON_PASSKEY_REQUEST
#define USE_ESP32_BLE_SERVER_ON_PASSKEY_NOTIFICATION
#define USE_ESP32_BLE_SERVER_ON_NUMERIC_COMPARISON_REQUEST