mirror of
https://github.com/esphome/esphome.git
synced 2024-11-13 10:47:46 +01:00
Add stop action for ble scanning (#3799)
This commit is contained in:
parent
49465223a4
commit
b3cca5dcb6
4 changed files with 51 additions and 13 deletions
|
@ -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))
|
||||
|
|
|
@ -84,6 +84,24 @@ class BLEEndOfScanTrigger : public Trigger<>, public ESPBTDeviceListener {
|
|||
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 esphome
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<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 esphome
|
||||
|
||||
|
|
Loading…
Reference in a new issue