From b3cca5dcb63a2060921801d77854048f7ba1a844 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 14 Sep 2022 14:57:45 +1200 Subject: [PATCH] Add stop action for ble scanning (#3799) --- .../components/esp32_ble_tracker/__init__.py | 25 +++++++++++++++++++ .../components/esp32_ble_tracker/automation.h | 18 +++++++++++++ .../esp32_ble_tracker/esp32_ble_tracker.cpp | 7 ++++++ .../esp32_ble_tracker/esp32_ble_tracker.h | 14 +---------- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/esphome/components/esp32_ble_tracker/__init__.py b/esphome/components/esp32_ble_tracker/__init__.py index db7fcc68ae..f70fea0639 100644 --- a/esphome/components/esp32_ble_tracker/__init__.py +++ b/esphome/components/esp32_ble_tracker/__init__.py @@ -51,6 +51,9 @@ BLEEndOfScanTrigger = esp32_ble_tracker_ns.class_( ESP32BLEStartScanAction = esp32_ble_tracker_ns.class_( "ESP32BLEStartScanAction", automation.Action ) +ESP32BLEStopScanAction = esp32_ble_tracker_ns.class_( + "ESP32BLEStopScanAction", automation.Action +) def validate_scan_parameters(config): @@ -259,6 +262,28 @@ async def esp32_ble_tracker_start_scan_action_to_code( return var +ESP32_BLE_STOP_SCAN_ACTION_SCHEMA = automation.maybe_simple_id( + cv.Schema( + { + cv.GenerateID(): cv.use_id(ESP32BLETracker), + } + ) +) + + +@automation.register_action( + "esp32_ble_tracker.stop_scan", + ESP32BLEStopScanAction, + ESP32_BLE_STOP_SCAN_ACTION_SCHEMA, +) +async def esp32_ble_tracker_stop_scan_action_to_code( + config, action_id, template_arg, args +): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var + + async def register_ble_device(var, config): paren = await cg.get_variable(config[CONF_ESP32_BLE_ID]) cg.add(paren.register_listener(var)) diff --git a/esphome/components/esp32_ble_tracker/automation.h b/esphome/components/esp32_ble_tracker/automation.h index d9e3b820af..6131d6ddf7 100644 --- a/esphome/components/esp32_ble_tracker/automation.h +++ b/esphome/components/esp32_ble_tracker/automation.h @@ -84,6 +84,24 @@ class BLEEndOfScanTrigger : public Trigger<>, public ESPBTDeviceListener { void on_scan_end() override { this->trigger(); } }; +template class ESP32BLEStartScanAction : public Action { + public: + ESP32BLEStartScanAction(ESP32BLETracker *parent) : parent_(parent) {} + TEMPLATABLE_VALUE(bool, continuous) + void play(Ts... x) override { + this->parent_->set_scan_continuous(this->continuous_.value(x...)); + this->parent_->start_scan(); + } + + protected: + ESP32BLETracker *parent_; +}; + +template class ESP32BLEStopScanAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->stop_scan(); } +}; + } // namespace esp32_ble_tracker } // namespace esphome diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 743f246d3a..68d88eb1e8 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -156,6 +156,13 @@ void ESP32BLETracker::start_scan() { } } +void ESP32BLETracker::stop_scan() { + ESP_LOGD(TAG, "Stopping scan."); + this->scan_continuous_ = false; + esp_ble_gap_stop_scanning(); + this->cancel_timeout("scan"); +} + bool ESP32BLETracker::ble_setup() { // Initialize non-volatile storage for the bluetooth controller esp_err_t err = nvs_flash_init(); diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h index 1d2900af69..29d0c81542 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h @@ -191,6 +191,7 @@ class ESP32BLETracker : public Component { void print_bt_device_info(const ESPBTDevice &device); void start_scan(); + void stop_scan(); protected: /// The FreeRTOS task managing the bluetooth interface. @@ -243,19 +244,6 @@ class ESP32BLETracker : public Component { // NOLINTNEXTLINE extern ESP32BLETracker *global_esp32_ble_tracker; -template class ESP32BLEStartScanAction : public Action { - public: - ESP32BLEStartScanAction(ESP32BLETracker *parent) : parent_(parent) {} - TEMPLATABLE_VALUE(bool, continuous) - void play(Ts... x) override { - this->parent_->set_scan_continuous(this->continuous_.value(x...)); - this->parent_->start_scan(); - } - - protected: - ESP32BLETracker *parent_; -}; - } // namespace esp32_ble_tracker } // namespace esphome