Add stop action for ble scanning (#3799)

This commit is contained in:
Jesse Hills 2022-09-14 14:57:45 +12:00 committed by GitHub
parent 49465223a4
commit b3cca5dcb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 13 deletions

View file

@ -51,6 +51,9 @@ BLEEndOfScanTrigger = esp32_ble_tracker_ns.class_(
ESP32BLEStartScanAction = esp32_ble_tracker_ns.class_( ESP32BLEStartScanAction = esp32_ble_tracker_ns.class_(
"ESP32BLEStartScanAction", automation.Action "ESP32BLEStartScanAction", automation.Action
) )
ESP32BLEStopScanAction = esp32_ble_tracker_ns.class_(
"ESP32BLEStopScanAction", automation.Action
)
def validate_scan_parameters(config): def validate_scan_parameters(config):
@ -259,6 +262,28 @@ async def esp32_ble_tracker_start_scan_action_to_code(
return var 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): async def register_ble_device(var, config):
paren = await cg.get_variable(config[CONF_ESP32_BLE_ID]) paren = await cg.get_variable(config[CONF_ESP32_BLE_ID])
cg.add(paren.register_listener(var)) cg.add(paren.register_listener(var))

View file

@ -84,6 +84,24 @@ class BLEEndOfScanTrigger : public Trigger<>, public ESPBTDeviceListener {
void on_scan_end() override { this->trigger(); } void on_scan_end() override { this->trigger(); }
}; };
template<typename... Ts> class ESP32BLEStartScanAction : public Action<Ts...> {
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<typename... Ts> class ESP32BLEStopScanAction : public Action<Ts...>, public Parented<ESP32BLETracker> {
public:
void play(Ts... x) override { this->parent_->stop_scan(); }
};
} // namespace esp32_ble_tracker } // namespace esp32_ble_tracker
} // namespace esphome } // namespace esphome

View file

@ -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() { bool ESP32BLETracker::ble_setup() {
// Initialize non-volatile storage for the bluetooth controller // Initialize non-volatile storage for the bluetooth controller
esp_err_t err = nvs_flash_init(); esp_err_t err = nvs_flash_init();

View file

@ -191,6 +191,7 @@ class ESP32BLETracker : public Component {
void print_bt_device_info(const ESPBTDevice &device); void print_bt_device_info(const ESPBTDevice &device);
void start_scan(); void start_scan();
void stop_scan();
protected: protected:
/// The FreeRTOS task managing the bluetooth interface. /// The FreeRTOS task managing the bluetooth interface.
@ -243,19 +244,6 @@ class ESP32BLETracker : public Component {
// NOLINTNEXTLINE // NOLINTNEXTLINE
extern ESP32BLETracker *global_esp32_ble_tracker; extern ESP32BLETracker *global_esp32_ble_tracker;
template<typename... Ts> class ESP32BLEStartScanAction : public Action<Ts...> {
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 esp32_ble_tracker
} // namespace esphome } // namespace esphome