Allow multiple MAC addresses for 'on_ble_advertise' filter (#4773)

This commit is contained in:
RoboMagus 2023-06-09 02:53:30 +02:00 committed by GitHub
parent 302dea4169
commit 5f0892dec4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

View file

@ -167,7 +167,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_ON_BLE_ADVERTISE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPBTAdvertiseTrigger),
cv.Optional(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_MAC_ADDRESS): cv.ensure_list(cv.mac_address),
}
),
cv.Optional(CONF_ON_BLE_SERVICE_DATA_ADVERTISE): automation.validate_automation(
@ -223,7 +223,10 @@ async def to_code(config):
for conf in config.get(CONF_ON_BLE_ADVERTISE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
if CONF_MAC_ADDRESS in conf:
cg.add(trigger.set_address(conf[CONF_MAC_ADDRESS].as_hex))
addr_list = []
for it in conf[CONF_MAC_ADDRESS]:
addr_list.append(it.as_hex)
cg.add(trigger.set_addresses(addr_list))
await automation.build_automation(trigger, [(ESPBTDeviceConstRef, "x")], conf)
for conf in config.get(CONF_ON_BLE_SERVICE_DATA_ADVERTISE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)

View file

@ -10,18 +10,22 @@ namespace esp32_ble_tracker {
class ESPBTAdvertiseTrigger : public Trigger<const ESPBTDevice &>, public ESPBTDeviceListener {
public:
explicit ESPBTAdvertiseTrigger(ESP32BLETracker *parent) { parent->register_listener(this); }
void set_address(uint64_t address) { this->address_ = address; }
void set_addresses(const std::vector<uint64_t> &addresses) { this->address_vec_ = addresses; }
bool parse_device(const ESPBTDevice &device) override {
if (this->address_ && device.address_uint64() != this->address_) {
return false;
uint64_t u64_addr = device.address_uint64();
if (!address_vec_.empty()) {
if (std::find(address_vec_.begin(), address_vec_.end(), u64_addr) == address_vec_.end()) {
return false;
}
}
this->trigger(device);
return true;
}
protected:
uint64_t address_ = 0;
std::vector<uint64_t> address_vec_;
};
class BLEServiceDataAdvertiseTrigger : public Trigger<const adv_data_t &>, public ESPBTDeviceListener {

View file

@ -488,6 +488,14 @@ binary_sensor:
esp32_ble_tracker:
on_ble_advertise:
- mac_address:
- AA:BB:CC:DD:EE:FF
- FF:EE:DD:CC:BB:AA
then:
# yamllint disable rule:line-length
- lambda: !lambda |-
ESP_LOGD("main", "The device address (%s) exists in list", x.address_str().c_str());
# yamllint enable rule:line-length
- mac_address: AC:37:43:77:5F:4C
then:
# yamllint disable rule:line-length