Add minimum RSSI check to ble presence (#4646)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
NP v/d Spek 2023-05-11 18:28:24 +02:00 committed by GitHub
parent ed024a0aa5
commit cd7e8e4bdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 7 deletions

View file

@ -7,6 +7,7 @@ from esphome.const import (
CONF_IBEACON_MAJOR,
CONF_IBEACON_MINOR,
CONF_IBEACON_UUID,
CONF_MIN_RSSI,
)
DEPENDENCIES = ["esp32_ble_tracker"]
@ -37,6 +38,9 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_UUID): cv.uuid,
cv.Optional(CONF_MIN_RSSI): cv.All(
cv.decibel, cv.int_range(min=-90, max=-30)
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@ -51,6 +55,9 @@ async def to_code(config):
await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config)
if CONF_MIN_RSSI in config:
cg.add(var.set_minimum_rssi(config[CONF_MIN_RSSI]))
if CONF_MAC_ADDRESS in config:
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))

View file

@ -41,12 +41,19 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
this->check_ibeacon_minor_ = true;
this->ibeacon_minor_ = minor;
}
void set_minimum_rssi(int rssi) {
this->check_minimum_rssi_ = true;
this->minimum_rssi_ = rssi;
}
void on_scan_end() override {
if (!this->found_)
this->publish_state(false);
this->found_ = false;
}
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
if (this->check_minimum_rssi_ && this->minimum_rssi_ <= device.get_rssi()) {
return false;
}
switch (this->match_by_) {
case MATCH_BY_MAC_ADDRESS:
if (device.address_uint64() == this->address_) {
@ -96,17 +103,21 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
enum MatchType { MATCH_BY_MAC_ADDRESS, MATCH_BY_SERVICE_UUID, MATCH_BY_IBEACON_UUID };
MatchType match_by_;
bool found_{false};
uint64_t address_;
esp32_ble_tracker::ESPBTUUID uuid_;
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_;
uint16_t ibeacon_major_;
bool check_ibeacon_major_;
uint16_t ibeacon_minor_;
bool check_ibeacon_minor_;
uint16_t ibeacon_major_{0};
uint16_t ibeacon_minor_{0};
int minimum_rssi_{0};
bool check_ibeacon_major_{false};
bool check_ibeacon_minor_{false};
bool check_minimum_rssi_{false};
bool found_{false};
};
} // namespace ble_presence

View file

@ -102,8 +102,9 @@ class BLERSSISensor : public sensor::Sensor, public esp32_ble_tracker::ESPBTDevi
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_;
uint16_t ibeacon_major_;
bool check_ibeacon_major_;
uint16_t ibeacon_minor_;
bool check_ibeacon_major_;
bool check_ibeacon_minor_;
};

View file

@ -408,6 +408,7 @@ CONF_MIN_LENGTH = "min_length"
CONF_MIN_LEVEL = "min_level"
CONF_MIN_POWER = "min_power"
CONF_MIN_RANGE = "min_range"
CONF_MIN_RSSI = "min_rssi"
CONF_MIN_TEMPERATURE = "min_temperature"
CONF_MIN_VALUE = "min_value"
CONF_MIN_VERSION = "min_version"