ESPHome  2024.10.2
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  void on_client_disconnect() override;
43 
44  float get_setup_priority() const override;
45  void start() override;
46  void stop() override;
47  bool is_active() const { return this->state_ != improv::STATE_STOPPED; }
48 
49 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK
50  void add_on_state_callback(std::function<void(improv::State, improv::Error)> &&callback) {
51  this->state_callback_.add(std::move(callback));
52  }
53 #endif
54 #ifdef USE_BINARY_SENSOR
55  void set_authorizer(binary_sensor::BinarySensor *authorizer) { this->authorizer_ = authorizer; }
56 #endif
57 #ifdef USE_OUTPUT
58  void set_status_indicator(output::BinaryOutput *status_indicator) { this->status_indicator_ = status_indicator; }
59 #endif
60  void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
61  void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
62 
63  void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
64  uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
65 
66  improv::State get_improv_state() const { return this->state_; }
67  improv::Error get_improv_error_state() const { return this->error_state_; }
68 
69  protected:
70  bool should_start_{false};
71  bool setup_complete_{false};
72 
73  uint32_t identify_start_{0};
75  uint32_t authorized_start_{0};
77 
78  uint32_t wifi_timeout_{};
79 
80  std::vector<uint8_t> incoming_data_;
82 
83  BLEService *service_ = nullptr;
89 
90 #ifdef USE_BINARY_SENSOR
91  binary_sensor::BinarySensor *authorizer_{nullptr};
92 #endif
93 #ifdef USE_OUTPUT
94  output::BinaryOutput *status_indicator_{nullptr};
95 #endif
96 
98  improv::Error error_state_{improv::ERROR_NONE};
99 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK
101 #endif
102 
103  bool status_indicator_state_{false};
104  void set_status_indicator_state_(bool state);
105 
106  void set_state_(improv::State state);
107  void set_error_(improv::Error error);
108  void send_response_(std::vector<uint8_t> &response);
109  void process_incoming_data_();
110  void on_wifi_connect_timeout_();
111  bool check_identify_();
112 };
113 
114 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
116 
117 } // namespace esp32_improv
118 } // namespace esphome
119 
120 #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