Skip to content

Commit 7036860

Browse files
committed
BLE Gateway: Added Discovery
1 parent 0603399 commit 7036860

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

components/ble_gateway/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from esphome import automation
55
from esphome.const import (
66
CONF_ID,
7+
CONF_DISCOVERY,
78
CONF_MAC_ADDRESS,
89
CONF_TRIGGER_ID,
910
CONF_ON_BLE_ADVERTISE,
@@ -27,6 +28,7 @@
2728
CONFIG_SCHEMA = cv.Schema(
2829
{
2930
cv.GenerateID(): cv.declare_id(BLEGateway),
31+
cv.Optional(CONF_DISCOVERY, default=False): cv.boolean,
3032
cv.Optional(CONF_DEVICES): cv.All(
3133
cv.ensure_list(
3234
{
@@ -49,6 +51,7 @@ async def to_code(config):
4951
await cg.register_component(var, config)
5052
await esp32_ble_tracker.register_ble_device(var, config)
5153

54+
cg.add(var.set_discovery(config[CONF_DISCOVERY]))
5255
if config.get(CONF_DEVICES):
5356
cg.add(var.set_devices("".join(f"{str(conf[CONF_MAC_ADDRESS]).replace(':', '')}" for conf in config[CONF_DEVICES])))
5457

components/ble_gateway/ble_gateway.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ std::string address_uint64_to_string(uint64_t address) {
5252
}
5353

5454
void BLEGateway::dump_config() {
55-
ESP_LOGCONFIG(TAG, "BLE Gateway (%d devices):", this->devices_.size());
55+
ESP_LOGCONFIG(TAG, "BLE Gateway: Discovery %s, %d device(s) configured:", YESNO(this->discovery_), this->devices_.size());
5656
for (auto device : this->devices_)
5757
ESP_LOGCONFIG(TAG, " MAC address: %s", address_uint64_to_string(device).c_str());
5858
}
5959

6060
bool BLEGateway::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
61-
if (std::find(this->devices_.begin(), this->devices_.end(), device.address_uint64()) != this->devices_.end()) {
61+
if (this->discovery_ || std::find(this->devices_.begin(), this->devices_.end(), device.address_uint64()) != this->devices_.end()) {
6262
auto packet = scan_result_to_hci_packet_hex(device.get_scan_result());
6363
ESP_LOGD(TAG, "[%s] Packet %s", address_uint64_to_string(device.address_uint64()).c_str(), packet.c_str());
6464
this->callback_.call(device, packet);

components/ble_gateway/ble_gateway.h

+3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ class BLEGateway : public Component, public esp32_ble_tracker::ESPBTDeviceListen
1717
static std::string scan_result_to_hci_packet_hex(const esp_ble_gap_cb_param_t::ble_scan_result_evt_param &scan_result);
1818
void dump_config() override;
1919
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
20+
bool get_discovery() const { return this->discovery_; }
21+
void set_discovery(bool discovery) { this->discovery_ = discovery; }
2022
void add_device(uint64_t device);
2123
void set_devices(std::string devices);
2224
protected:
25+
bool discovery_{false};
2326
std::vector<uint64_t> devices_{};
2427
CallbackManager<void(const esp32_ble_tracker::ESPBTDevice &, std::string)> callback_{};
2528
};

0 commit comments

Comments
 (0)