ESPHome  2025.2.0
esp32_improv_component.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/helpers.h"
7 
11 
12 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK
14 #endif
15 
16 #ifdef USE_BINARY_SENSOR
18 #endif
19 
20 #ifdef USE_OUTPUT
22 #endif
23 
24 #include <vector>
25 
26 #ifdef USE_ESP32
27 
28 #include <improv.h>
29 
30 namespace esphome {
31 namespace esp32_improv {
32 
33 using namespace esp32_ble_server;
34 
36  public:
38  void dump_config() override;
39  void loop() override;
40  void setup() override;
41  void setup_characteristics();
42 
43  float get_setup_priority() const override;
44  void start();
45  void stop();
46  bool is_active() const { return this->state_ != improv::STATE_STOPPED; }
47 
48 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK
49  void add_on_state_callback(std::function<void(improv::State, improv::Error)> &&callback) {
50  this->state_callback_.add(std::move(callback));
51  }
52 #endif
53 #ifdef USE_BINARY_SENSOR
54  void set_authorizer(binary_sensor::BinarySensor *authorizer) { this->authorizer_ = authorizer; }
55 #endif
56 #ifdef USE_OUTPUT
57  void set_status_indicator(output::BinaryOutput *status_indicator) { this->status_indicator_ = status_indicator; }
58 #endif
59  void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
60  void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
61 
62  void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
63  uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
64 
65  improv::State get_improv_state() const { return this->state_; }
66  improv::Error get_improv_error_state() const { return this->error_state_; }
67 
68  protected:
69  bool should_start_{false};
70  bool setup_complete_{false};
71 
72  uint32_t identify_start_{0};
74  uint32_t authorized_start_{0};
76 
77  uint32_t wifi_timeout_{};
78 
79  std::vector<uint8_t> incoming_data_;
81 
82  BLEService *service_ = nullptr;
88 
89 #ifdef USE_BINARY_SENSOR
90  binary_sensor::BinarySensor *authorizer_{nullptr};
91 #endif
92 #ifdef USE_OUTPUT
93  output::BinaryOutput *status_indicator_{nullptr};
94 #endif
95 
97  improv::Error error_state_{improv::ERROR_NONE};
98 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK
100 #endif
101 
102  bool status_indicator_state_{false};
103  void set_status_indicator_state_(bool state);
104 
105  void set_state_(improv::State state);
106  void set_error_(improv::Error error);
107  void send_response_(std::vector<uint8_t> &response);
108  void process_incoming_data_();
109  void on_wifi_connect_timeout_();
110  bool check_identify_();
111 };
112 
113 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
115 
116 } // namespace esp32_improv
117 } // namespace esphome
118 
119 #endif
void setup()
void loop()
void add_on_state_callback(std::function< void(improv::State, improv::Error)> &&callback)
void set_authorized_duration(uint32_t authorized_duration)
void set_identify_duration(uint32_t identify_duration)
void set_authorizer(binary_sensor::BinarySensor *authorizer)
ESP32ImprovComponent * global_improv_component
void set_status_indicator(output::BinaryOutput *status_indicator)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool state
Definition: fan.h:34