mirror of
https://github.com/esphome/esphome.git
synced 2025-09-27 15:42:22 +01:00
Move esp32_ble_server to its own component (#1898)
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor, output, esp32_ble
|
||||
from esphome.components import binary_sensor, output, esp32_ble_server
|
||||
from esphome.const import CONF_ID, ESP_PLATFORM_ESP32
|
||||
|
||||
|
||||
AUTO_LOAD = ["binary_sensor", "output", "improv"]
|
||||
AUTO_LOAD = ["binary_sensor", "output", "improv", "esp32_ble_server"]
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
DEPENDENCIES = ["esp32_ble", "wifi"]
|
||||
CONFLICTS_WITH = ["esp32_ble_tracker", "esp32_ble_beacon"]
|
||||
DEPENDENCIES = ["wifi"]
|
||||
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
|
||||
|
||||
CONF_AUTHORIZED_DURATION = "authorized_duration"
|
||||
@@ -18,7 +19,7 @@ CONF_WIFI_TIMEOUT = "wifi_timeout"
|
||||
|
||||
esp32_improv_ns = cg.esphome_ns.namespace("esp32_improv")
|
||||
ESP32ImprovComponent = esp32_improv_ns.class_(
|
||||
"ESP32ImprovComponent", cg.Component, esp32_ble.BLEServiceComponent
|
||||
"ESP32ImprovComponent", cg.Component, esp32_ble_server.BLEServiceComponent
|
||||
)
|
||||
|
||||
|
||||
@@ -33,7 +34,7 @@ def validate_none_(value):
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(ESP32ImprovComponent),
|
||||
cv.GenerateID(CONF_BLE_SERVER_ID): cv.use_id(esp32_ble.BLEServer),
|
||||
cv.GenerateID(CONF_BLE_SERVER_ID): cv.use_id(esp32_ble_server.BLEServer),
|
||||
cv.Required(CONF_AUTHORIZER): cv.Any(
|
||||
validate_none_, cv.use_id(binary_sensor.BinarySensor)
|
||||
),
|
||||
|
@@ -1,7 +1,9 @@
|
||||
#include "esp32_improv_component.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "esphome/components/esp32_ble/ble.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_2902.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/components/esp32_ble/ble_2902.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
@@ -12,40 +14,39 @@ static const char *const TAG = "esp32_improv.component";
|
||||
|
||||
ESP32ImprovComponent::ESP32ImprovComponent() { global_improv_component = this; }
|
||||
|
||||
void ESP32ImprovComponent::setup_service() {
|
||||
this->service_ = esp32_ble::global_ble_server->create_service(improv::SERVICE_UUID, true);
|
||||
void ESP32ImprovComponent::setup() {
|
||||
this->service_ = global_ble_server->create_service(improv::SERVICE_UUID, true);
|
||||
this->setup_characteristics();
|
||||
}
|
||||
|
||||
void ESP32ImprovComponent::setup_characteristics() {
|
||||
this->status_ = this->service_->create_characteristic(
|
||||
improv::STATUS_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ | esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *status_descriptor = new esp32_ble::BLE2902();
|
||||
improv::STATUS_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *status_descriptor = new BLE2902();
|
||||
this->status_->add_descriptor(status_descriptor);
|
||||
|
||||
this->error_ = this->service_->create_characteristic(
|
||||
improv::ERROR_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ | esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *error_descriptor = new esp32_ble::BLE2902();
|
||||
improv::ERROR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *error_descriptor = new BLE2902();
|
||||
this->error_->add_descriptor(error_descriptor);
|
||||
|
||||
this->rpc_ =
|
||||
this->service_->create_characteristic(improv::RPC_COMMAND_UUID, esp32_ble::BLECharacteristic::PROPERTY_WRITE);
|
||||
this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE);
|
||||
this->rpc_->on_write([this](const std::vector<uint8_t> &data) {
|
||||
if (data.size() > 0) {
|
||||
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
|
||||
}
|
||||
});
|
||||
esp32_ble::BLEDescriptor *rpc_descriptor = new esp32_ble::BLE2902();
|
||||
BLEDescriptor *rpc_descriptor = new BLE2902();
|
||||
this->rpc_->add_descriptor(rpc_descriptor);
|
||||
|
||||
this->rpc_response_ =
|
||||
this->service_->create_characteristic(improv::RPC_RESULT_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ |
|
||||
esp32_ble::BLECharacteristic::PROPERTY_NOTIFY);
|
||||
esp32_ble::BLEDescriptor *rpc_response_descriptor = new esp32_ble::BLE2902();
|
||||
this->rpc_response_ = this->service_->create_characteristic(
|
||||
improv::RPC_RESULT_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||
BLEDescriptor *rpc_response_descriptor = new BLE2902();
|
||||
this->rpc_response_->add_descriptor(rpc_response_descriptor);
|
||||
|
||||
this->capabilities_ =
|
||||
this->service_->create_characteristic(improv::CAPABILITIES_UUID, esp32_ble::BLECharacteristic::PROPERTY_READ);
|
||||
esp32_ble::BLEDescriptor *capabilities_descriptor = new esp32_ble::BLE2902();
|
||||
this->service_->create_characteristic(improv::CAPABILITIES_UUID, BLECharacteristic::PROPERTY_READ);
|
||||
BLEDescriptor *capabilities_descriptor = new BLE2902();
|
||||
this->capabilities_->add_descriptor(capabilities_descriptor);
|
||||
uint8_t capabilities = 0x00;
|
||||
if (this->status_indicator_ != nullptr)
|
||||
@@ -64,13 +65,9 @@ void ESP32ImprovComponent::loop() {
|
||||
if (this->status_indicator_ != nullptr)
|
||||
this->status_indicator_->turn_off();
|
||||
|
||||
if (this->service_->is_created() && !this->setup_complete_) {
|
||||
this->setup_characteristics();
|
||||
}
|
||||
|
||||
if (this->should_start_ && this->setup_complete_) {
|
||||
if (this->service_->is_created() && this->should_start_ && this->setup_complete_) {
|
||||
if (this->service_->is_running()) {
|
||||
this->service_->get_server()->get_advertising()->start();
|
||||
esp32_ble::global_ble->get_advertising()->start();
|
||||
|
||||
this->set_state_(improv::STATE_AWAITING_AUTHORIZATION);
|
||||
this->set_error_(improv::ERROR_NONE);
|
||||
@@ -205,10 +202,7 @@ void ESP32ImprovComponent::stop() {
|
||||
});
|
||||
}
|
||||
|
||||
float ESP32ImprovComponent::get_setup_priority() const {
|
||||
// Before WiFi
|
||||
return setup_priority::AFTER_BLUETOOTH;
|
||||
}
|
||||
float ESP32ImprovComponent::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
|
||||
|
||||
void ESP32ImprovComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "ESP32 Improv:");
|
||||
|
@@ -1,26 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_server.h"
|
||||
#include "esphome/components/esp32_ble_server/ble_characteristic.h"
|
||||
#include "esphome/components/improv/improv.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "esphome/components/esp32_ble/ble_server.h"
|
||||
#include "esphome/components/esp32_ble/ble_characteristic.h"
|
||||
#include "esphome/components/output/binary_output.h"
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#include "esphome/components/improv/improv.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_improv {
|
||||
|
||||
class ESP32ImprovComponent : public Component, public esp32_ble::BLEServiceComponent {
|
||||
using namespace esp32_ble_server;
|
||||
|
||||
class ESP32ImprovComponent : public Component, public BLEServiceComponent {
|
||||
public:
|
||||
ESP32ImprovComponent();
|
||||
void dump_config() override;
|
||||
void loop() override;
|
||||
void setup_service() override;
|
||||
void setup() override;
|
||||
void setup_characteristics();
|
||||
void on_client_disconnect() override;
|
||||
|
||||
@@ -46,12 +48,12 @@ class ESP32ImprovComponent : public Component, public esp32_ble::BLEServiceCompo
|
||||
std::vector<uint8_t> incoming_data_;
|
||||
wifi::WiFiAP connecting_sta_;
|
||||
|
||||
esp32_ble::BLEService *service_;
|
||||
esp32_ble::BLECharacteristic *status_;
|
||||
esp32_ble::BLECharacteristic *error_;
|
||||
esp32_ble::BLECharacteristic *rpc_;
|
||||
esp32_ble::BLECharacteristic *rpc_response_;
|
||||
esp32_ble::BLECharacteristic *capabilities_;
|
||||
BLEService *service_;
|
||||
BLECharacteristic *status_;
|
||||
BLECharacteristic *error_;
|
||||
BLECharacteristic *rpc_;
|
||||
BLECharacteristic *rpc_response_;
|
||||
BLECharacteristic *capabilities_;
|
||||
|
||||
binary_sensor::BinarySensor *authorizer_{nullptr};
|
||||
output::BinaryOutput *status_indicator_{nullptr};
|
||||
|
Reference in New Issue
Block a user