This commit is contained in:
Maxim Slipenko 2023-12-02 21:31:39 +03:00
parent 506b6f905a
commit 121ca8f453
7 changed files with 17 additions and 119 deletions

4
.gitignore vendored
View File

@ -3,4 +3,6 @@
# You can modify this file to suit your needs. # You can modify this file to suit your needs.
/.esphome/ /.esphome/
/secrets.yaml /secrets.yaml
/config.yaml /config.yaml
/configa.yaml
__pycache__/**

View File

@ -16,7 +16,7 @@ namespace esphome
} }
int NimbleDistanceSensor::get_1m_rssi(nimble_tracker::NimbleTrackerEvent *tracker_event) int NimbleDistanceSensor::get_1m_rssi(nimble_tracker::NimbleTrackerEvent *tracker_event)
{ {
return this->ref_rssi_ + tracker_event->getTXPower(); return this->ref_rssi_; //+ tracker_event->getTXPower() + 99;
} }
Filter::Filter(float fcmin, float beta, float dcutoff) : one_euro_{OneEuroFilter<float, unsigned long>(1, fcmin, beta, dcutoff)} Filter::Filter(float fcmin, float beta, float dcutoff) : one_euro_{OneEuroFilter<float, unsigned long>(1, fcmin, beta, dcutoff)}

View File

@ -50,7 +50,7 @@ namespace esphome
Filter *filter_; Filter *filter_;
int rssi_ = NO_RSSI, newest_ = NO_RSSI, recent_ = NO_RSSI, oldest_ = NO_RSSI; int rssi_ = NO_RSSI, newest_ = NO_RSSI, recent_ = NO_RSSI, oldest_ = NO_RSSI;
int8_t ref_rssi_ = -65; int8_t ref_rssi_ = -75;
float absorption_ = 3.5f; float absorption_ = 3.5f;
float last_reported_position_ = 0; float last_reported_position_ = 0;
int64_t last_reported_micro_seconds_ = 0; int64_t last_reported_micro_seconds_ = 0;

View File

@ -111,4 +111,4 @@ async def device_listener_to_code(var, config):
if CONF_IRK in config: if CONF_IRK in config:
cg.add(var.set_irk(config[CONF_IRK])) cg.add(var.set_irk(config[CONF_IRK]))
if CONF_MAC in config: if CONF_MAC in config:
cg.add(var.set_irk(config[CONF_MAC])) cg.add(var.set_address(config[CONF_MAC]))

View File

@ -26,6 +26,8 @@ namespace esphome
bool NimbleDeviceListener::parse_event(NimbleTrackerEvent *tracker_event) bool NimbleDeviceListener::parse_event(NimbleTrackerEvent *tracker_event)
{ {
// ESP_LOGD(TAG, "Found device %s", tracker_event->toString().c_str());
ESP_LOGD(TAG, "%d", this->match_by_);
if (this->match_by_ == MATCH_BY_IRK) { if (this->match_by_ == MATCH_BY_IRK) {
if (tracker_event->getAddressType() != BLE_ADDR_RANDOM) if (tracker_event->getAddressType() != BLE_ADDR_RANDOM)
{ {
@ -44,15 +46,19 @@ namespace esphome
{ {
return false; return false;
} }
} else if (this->match_by_ == MATCH_BY_ADDRESS) { }
auto address = tracker_event->getAddress(); if (this->match_by_ == MATCH_BY_ADDRESS) {
ESP_LOGD(TAG, "Found device %s", tracker_event->toString().c_str());
auto address = tracker_event->getAddress().toString();
if (this.address_ == address) { if (this->address_ == address) {
return this->update_state(tracker_event); return this->update_state(tracker_event);
} else { } else {
return false; return false;
} }
} }
return false;
}; };
} // namespace nimble_tracker } // namespace nimble_tracker

View File

@ -14,7 +14,7 @@ namespace esphome
public: public:
bool parse_event(NimbleTrackerEvent *tracker_event); bool parse_event(NimbleTrackerEvent *tracker_event);
void set_irk(std::string irk_hex); void set_irk(std::string irk_hex);
void set_address(std::stding address); void set_address(std::string address);
protected: protected:
virtual bool update_state(NimbleTrackerEvent *tracker_event) = 0; virtual bool update_state(NimbleTrackerEvent *tracker_event) = 0;
@ -22,7 +22,7 @@ namespace esphome
enum MatchType enum MatchType
{ {
MATCH_BY_IRK, MATCH_BY_IRK,
MATCH_BY_ADDRESS; MATCH_BY_ADDRESS,
}; };
MatchType match_by_; MatchType match_by_;

View File

@ -1,110 +0,0 @@
substitutions:
device_name: "nimble-shelly"
entity_id: "nimble_shelly"
room_name: "office"
mqtt:
broker: !secret mqtt_broker
username: esphome
password: !secret mqtt_password
discovery: false
id: mqtt_client
external_components:
- source:
type: local
path: ./components
nimble_tracker:
scan_parameters:
# window: 500ms
# interval: 1.2s
window: 100ms
interval: 100ms
active: false
globals:
- id: room_topic
type: std::string
initial_value: '"room_presence/${room_name}"'
sensor:
- platform: nimble_distance
irk: !secret apple_watch_irk
name: "Apple Watch Distance"
id: apple_watch_distance
internal: true
# TODO: should we retain the mqtt message?
on_value:
then:
- lambda: |-
id(mqtt_client)->publish_json(id(room_topic), [=](ArduinoJson::JsonObject root) -> void {
root["id"] = "apple_watch";
root["name"] = "Apple Watch";
root["distance"] = id(apple_watch_distance).state;
});
esphome:
name: ${device_name}
platformio_options:
board_build.f_cpu: 160000000L
esp32:
board: esp32dev
framework:
type: esp-idf
sdkconfig_options:
CONFIG_FREERTOS_UNICORE: y
CONFIG_ESP32_DEFAULT_CPU_FREQ_160: y
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ: "160"
# From https://github.com/esphome/issues/issues/2941
# Increase watchdog timeout to flash firmware with bluetooth enabled, fixes error:
# ERROR Error receiving acknowledge binary size: timed out
CONFIG_ESP_TASK_WDT_TIMEOUT_S: "20"
CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
output:
- platform: gpio
id: "relay_output"
pin: GPIO26
status_led:
pin:
number: GPIO0
inverted: true
# Enable logging
logger:
level: VERBOSE
# Enable Home Assistant API
api:
encryption:
key: !secret api_encryption_key
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
password: !secret wifi_password
switch:
- platform: output
id: shelly_relay
name: "${entity_id}"
output: "relay_output"
# after reboot, keep the relay off. this prevents light turning on after a power outage
restore_mode: ALWAYS_OFF
# From https://community.home-assistant.io/t/shelly-plus-1-esphome-bletracker/363549/38
# Setup a button in home assistant to reboot the shelly into safe mode
button:
- platform: safe_mode
name: "${entity_id}_safe_mode_restart"