mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 17:27:59 +01:00
Tuneable min_rssi for ble_presence component
This commit is contained in:
parent
5c31ab4060
commit
89fcaa3b40
4 changed files with 37 additions and 1 deletions
|
@ -1,12 +1,14 @@
|
|||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor, esp32_ble_tracker
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components.number import Number
|
||||
from esphome.const import (
|
||||
CONF_IBEACON_MAJOR,
|
||||
CONF_IBEACON_MINOR,
|
||||
CONF_IBEACON_UUID,
|
||||
CONF_MAC_ADDRESS,
|
||||
CONF_MIN_RSSI,
|
||||
CONF_MIN_RSSI_NUMBER_ID,
|
||||
CONF_SERVICE_UUID,
|
||||
CONF_TIMEOUT,
|
||||
)
|
||||
|
@ -46,6 +48,7 @@ CONFIG_SCHEMA = cv.All(
|
|||
cv.Optional(CONF_MIN_RSSI): cv.All(
|
||||
cv.decibel, cv.int_range(min=-100, max=-30)
|
||||
),
|
||||
cv.Optional(CONF_MIN_RSSI_NUMBER_ID): cv.use_id(Number)
|
||||
}
|
||||
)
|
||||
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||
|
@ -53,6 +56,9 @@ CONFIG_SCHEMA = cv.All(
|
|||
cv.has_exactly_one_key(
|
||||
CONF_MAC_ADDRESS, CONF_IRK, CONF_SERVICE_UUID, CONF_IBEACON_UUID
|
||||
),
|
||||
cv.has_at_most_one_key(
|
||||
CONF_MIN_RSSI, CONF_MIN_RSSI_NUMBER_ID
|
||||
),
|
||||
_validate,
|
||||
)
|
||||
|
||||
|
@ -63,8 +69,12 @@ async def to_code(config):
|
|||
await esp32_ble_tracker.register_ble_device(var, config)
|
||||
|
||||
cg.add(var.set_timeout(config[CONF_TIMEOUT].total_milliseconds))
|
||||
|
||||
if min_rssi := config.get(CONF_MIN_RSSI):
|
||||
cg.add(var.set_minimum_rssi(min_rssi))
|
||||
elif min_rssi_number_id := config.get(CONF_MIN_RSSI_NUMBER_ID):
|
||||
min_rssi_number = await cg.get_variable(min_rssi_number_id)
|
||||
cg.add(var.set_minimum_rssi_input(min_rssi_number))
|
||||
|
||||
if mac_address := config.get(CONF_MAC_ADDRESS):
|
||||
cg.add(var.set_address(mac_address.as_hex))
|
||||
|
|
|
@ -10,6 +10,21 @@ static const char *const TAG = "ble_presence";
|
|||
|
||||
void BLEPresenceDevice::dump_config() { LOG_BINARY_SENSOR("", "BLE Presence", this); }
|
||||
|
||||
void BLEPresenceDevice::set_minimum_rssi_input(number::Number *min_rssi_number) {
|
||||
min_rssi_number->add_on_state_callback([this](float state) {
|
||||
int rssi = int(state);
|
||||
if (rssi < -100 || rssi > -30) {
|
||||
ESP_LOGW(TAG, "Valid RSSI range is -30dB to -100dB");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Setting minimum rssi to %d", rssi);
|
||||
this->set_minimum_rssi_(int(state));
|
||||
});
|
||||
|
||||
this->check_minimum_rssi_ = true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace ble_presence
|
||||
} // namespace esphome
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/number/number.h"
|
||||
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
|
@ -46,9 +48,12 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
|
|||
this->ibeacon_minor_ = minor;
|
||||
}
|
||||
void set_minimum_rssi(int rssi) {
|
||||
set_minimum_rssi_(rssi);
|
||||
this->check_minimum_rssi_ = true;
|
||||
this->minimum_rssi_ = rssi;
|
||||
}
|
||||
|
||||
void set_minimum_rssi_input(number::Number *min_rssi_number);
|
||||
|
||||
void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
|
||||
if (this->check_minimum_rssi_ && this->minimum_rssi_ > device.get_rssi()) {
|
||||
|
@ -108,6 +113,10 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
|
|||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
protected:
|
||||
void set_minimum_rssi_(int rssi) {
|
||||
this->minimum_rssi_ = rssi;
|
||||
}
|
||||
|
||||
void set_found_(bool state) {
|
||||
this->found_ = state;
|
||||
if (state)
|
||||
|
@ -135,6 +144,7 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
|
|||
bool found_{false};
|
||||
uint32_t last_seen_{};
|
||||
uint32_t timeout_{};
|
||||
|
||||
};
|
||||
|
||||
} // namespace ble_presence
|
||||
|
|
|
@ -489,6 +489,7 @@ CONF_MIN_LEVEL = "min_level"
|
|||
CONF_MIN_POWER = "min_power"
|
||||
CONF_MIN_RANGE = "min_range"
|
||||
CONF_MIN_RSSI = "min_rssi"
|
||||
CONF_MIN_RSSI_NUMBER_ID = "min_rssi_number_id"
|
||||
CONF_MIN_TEMPERATURE = "min_temperature"
|
||||
CONF_MIN_VALUE = "min_value"
|
||||
CONF_MIN_VERSION = "min_version"
|
||||
|
|
Loading…
Reference in a new issue