From 84bbf02bde3cd9ef7ec24a97e8d372feb60463ec Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:41:45 +1300 Subject: [PATCH] ble_client rssi sensor fix when not connected (#5632) --- .../ble_client/sensor/ble_rssi_sensor.cpp | 14 +++++++++++--- .../components/ble_client/sensor/ble_rssi_sensor.h | 6 +++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/esphome/components/ble_client/sensor/ble_rssi_sensor.cpp b/esphome/components/ble_client/sensor/ble_rssi_sensor.cpp index 13e51ed5b3..a36e191e32 100644 --- a/esphome/components/ble_client/sensor/ble_rssi_sensor.cpp +++ b/esphome/components/ble_client/sensor/ble_rssi_sensor.cpp @@ -1,8 +1,8 @@ #include "ble_rssi_sensor.h" -#include "esphome/core/log.h" +#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/core/application.h" #include "esphome/core/helpers.h" -#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" +#include "esphome/core/log.h" #ifdef USE_ESP32 @@ -37,6 +37,10 @@ void BLEClientRSSISensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga } case ESP_GATTC_SEARCH_CMPL_EVT: this->node_state = espbt::ClientState::ESTABLISHED; + if (this->should_update_) { + this->should_update_ = false; + this->get_rssi_(); + } break; default: break; @@ -50,6 +54,7 @@ void BLEClientRSSISensor::gap_event_handler(esp_gap_ble_cb_event_t event, esp_bl if (param->read_rssi_cmpl.status == ESP_BT_STATUS_SUCCESS) { int8_t rssi = param->read_rssi_cmpl.rssi; ESP_LOGI(TAG, "ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT RSSI: %d", rssi); + this->status_clear_warning(); this->publish_state(rssi); } break; @@ -61,9 +66,12 @@ void BLEClientRSSISensor::gap_event_handler(esp_gap_ble_cb_event_t event, esp_bl void BLEClientRSSISensor::update() { if (this->node_state != espbt::ClientState::ESTABLISHED) { ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str()); + this->should_update_ = true; return; } - + this->get_rssi_(); +} +void BLEClientRSSISensor::get_rssi_() { ESP_LOGV(TAG, "requesting rssi from %s", this->parent()->address_str().c_str()); auto status = esp_ble_gap_read_rssi(this->parent()->get_remote_bda()); if (status != ESP_OK) { diff --git a/esphome/components/ble_client/sensor/ble_rssi_sensor.h b/esphome/components/ble_client/sensor/ble_rssi_sensor.h index 028df83832..5dd3fc7af9 100644 --- a/esphome/components/ble_client/sensor/ble_rssi_sensor.h +++ b/esphome/components/ble_client/sensor/ble_rssi_sensor.h @@ -1,9 +1,9 @@ #pragma once -#include "esphome/core/component.h" #include "esphome/components/ble_client/ble_client.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/components/sensor/sensor.h" +#include "esphome/core/component.h" #ifdef USE_ESP32 #include @@ -24,6 +24,10 @@ class BLEClientRSSISensor : public sensor::Sensor, public PollingComponent, publ void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override; + + protected: + void get_rssi_(); + bool should_update_{false}; }; } // namespace ble_client