From 905267a9c30aae83323e8d9f7dcdfddbaac804a8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Oct 2026 06:38:25 -1000 Subject: [PATCH] [esp32_ble] Build ble.enabled, ble.enable and ble.disable with the apply helpers (#20839) --- esphome/components/esp32_ble/__init__.py | 29 +++++------------------- esphome/components/esp32_ble/ble.h | 21 ----------------- 2 files changed, 6 insertions(+), 44 deletions(-) diff --git a/esphome/components/esp32_ble/__init__.py b/esphome/components/esp32_ble/__init__.py index 95a83edf76f..6c83d5e44fe 100644 --- a/esphome/components/esp32_ble/__init__.py +++ b/esphome/components/esp32_ble/__init__.py @@ -296,10 +296,6 @@ NO_BLUETOOTH_VARIANTS = [const.VARIANT_ESP32S2] esp32_ble_ns = cg.esphome_ns.namespace("esp32_ble") ESP32BLE = esp32_ble_ns.class_("ESP32BLE", cg.Component) -BLEEnabledCondition = esp32_ble_ns.class_("BLEEnabledCondition", automation.Condition) -BLEEnableAction = esp32_ble_ns.class_("BLEEnableAction", automation.Action) -BLEDisableAction = esp32_ble_ns.class_("BLEDisableAction", automation.Action) - IoCapability = esp32_ble_ns.enum("IoCapability") IO_CAPABILITY = { "none": IoCapability.IO_CAP_NONE, @@ -613,27 +609,14 @@ async def to_code(config: ConfigType) -> None: cg.add_define("USE_ESP32_BLE_UUID") -automation.register_bare_condition( - "ble.enabled", - BLEEnabledCondition, - cv.Schema({}), -) +BLE_AUTOMATION_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(ESP32BLE)}) +automation.register_apply_condition("ble.enabled", BLE_AUTOMATION_SCHEMA, "is_active()") -automation.register_bare_action( - "ble.enable", - BLEEnableAction, - cv.Schema({}), - synchronous=True, -) - - -automation.register_bare_action( - "ble.disable", - BLEDisableAction, - cv.Schema({}), - synchronous=True, -) +for _name, _call in (("ble.enable", "enable()"), ("ble.disable", "disable()")): + automation.register_apply_action( + _name, BLE_AUTOMATION_SCHEMA, automation.ApplyCall(_call) + ) # ble_advertising.cpp is fully #ifdef'd on USE_ESP32_BLE_ADVERTISING, set diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index ef326e1532d..c21daabdbaa 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -259,27 +259,6 @@ class ESP32BLE final : public Component { // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) extern ESP32BLE *global_ble; -template class BLEEnabledCondition final : public Condition { - public: - bool check(const Ts &...x) override { return global_ble != nullptr && global_ble->is_active(); } -}; - -template class BLEEnableAction final : public Action { - public: - void play(const Ts &...x) override { - if (global_ble != nullptr) - global_ble->enable(); - } -}; - -template class BLEDisableAction final : public Action { - public: - void play(const Ts &...x) override { - if (global_ble != nullptr) - global_ble->disable(); - } -}; - } // namespace esphome::esp32_ble #endif