ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
improv_ble_component.h
Go to the documentation of this file.
1#pragma once
2
7
10
11#ifdef USE_IMPROV_BLE_STATE_CALLBACK
13#endif
14
15#ifdef USE_BINARY_SENSOR
17#endif
18
19#ifdef USE_OUTPUT
21#endif
22
23#include <span>
24#include <vector>
25
26// ESP-IDF is currently the only target platform with a BLE GATT server, so it is
27// the only backend this component has. The Python side keeps the platform table
28// (BLE_SERVER_BACKENDS in __init__.py); a second backend adds another arm here.
29#ifdef USE_ESP32
30
33
34#include <improv.h>
35
36namespace esphome::improv_ble {
37
38using namespace esp32_ble_server;
39
41 public:
43 void dump_config() override;
44 void loop() override;
45 void setup() override;
47
48 float get_setup_priority() const override;
49 void start();
50 void stop();
51 bool is_active() const { return this->state_ != improv::STATE_STOPPED; }
52 bool should_start() const { return this->should_start_; }
53
54#ifdef USE_IMPROV_BLE_STATE_CALLBACK
55 template<typename F> void add_on_state_callback(F &&callback) {
56 this->state_callback_.add(std::forward<F>(callback));
57 }
58#endif
59#ifdef USE_BINARY_SENSOR
60 void set_authorizer(binary_sensor::BinarySensor *authorizer) { this->authorizer_ = authorizer; }
61#endif
62#ifdef USE_OUTPUT
63 void set_status_indicator(output::BinaryOutput *status_indicator) { this->status_indicator_ = status_indicator; }
64#endif
65 void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
66 void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
67
68 void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
69 uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
70
71 improv::State get_improv_state() const { return this->state_; }
72 improv::Error get_improv_error_state() const { return this->error_state_; }
73
74 protected:
79
81
82 std::vector<uint8_t> incoming_data_;
84 // Single byte fields here fill the alignment gap between connecting_sta_ and the next pointer
85 improv::State state_{improv::STATE_STOPPED};
86 improv::Error error_state_{improv::ERROR_NONE};
87 bool should_start_{false};
88 bool setup_complete_{false};
92
99
100#ifdef USE_BINARY_SENSOR
102#endif
103#ifdef USE_OUTPUT
105#endif
106
107#ifdef USE_IMPROV_BLE_STATE_CALLBACK
108 CallbackManager<void(improv::State, improv::Error)> state_callback_{};
109#endif
110
116
117 void set_state_(improv::State state, bool update_advertising = true);
118 void set_error_(improv::Error error);
119 improv::State get_initial_state_() const;
120 void send_response_(std::span<const uint8_t> response);
124 bool check_identify_();
126#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
127 const char *state_to_string_(improv::State state);
128#endif
129};
130
131// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
132extern ImprovBLEComponent *global_improv_component;
133
134} // namespace esphome::improv_ble
135
136#endif
Base class for all binary_sensor-type classes.
void set_state_(improv::State state, bool update_advertising=true)
const char * state_to_string_(improv::State state)
binary_sensor::BinarySensor * authorizer_
CallbackManager< void(improv::State, improv::Error)> state_callback_
void set_wifi_timeout(uint32_t wifi_timeout)
void set_status_indicator(output::BinaryOutput *status_indicator)
void set_authorized_duration(uint32_t authorized_duration)
void set_identify_duration(uint32_t identify_duration)
void set_authorizer(binary_sensor::BinarySensor *authorizer)
void send_response_(std::span< const uint8_t > response)
bool state
Definition fan.h:2
ImprovBLEComponent * global_improv_component
static void uint32_t