11 namespace esp32_improv {
13 static const char *
const TAG =
"esp32_improv.component";
14 static const char *
const ESPHOME_MY_LINK =
"https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";
19 #ifdef USE_BINARY_SENSOR 43 this->
rpc_->
on_write([
this](
const std::vector<uint8_t> &data) {
60 uint8_t capabilities = 0x00;
63 capabilities |= improv::CAPABILITY_IDENTIFY;
73 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK 82 ESP_LOGD(TAG,
"Creating Improv service");
100 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
102 ESP_LOGD(TAG,
"Service started!");
108 case improv::STATE_AWAITING_AUTHORIZATION: {
109 #ifdef USE_BINARY_SENSOR 123 case improv::STATE_AUTHORIZED: {
124 #ifdef USE_BINARY_SENSOR 127 ESP_LOGD(TAG,
"Authorization timeout");
128 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
138 case improv::STATE_PROVISIONING: {
147 std::vector<std::string> urls = {ESPHOME_MY_LINK};
151 std::string webserver_url =
"http://" + ip.str() +
":" +
to_string(USE_WEBSERVER_PORT);
152 urls.push_back(webserver_url);
157 std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls);
163 case improv::STATE_PROVISIONED: {
192 uint32_t time = now % 1000;
199 ESP_LOGV(TAG,
"Setting state: %d", state);
202 uint8_t data[1]{state};
207 std::vector<uint8_t> service_data(8, 0);
208 service_data[0] = 0x77;
209 service_data[1] = 0x46;
210 service_data[2] =
static_cast<uint8_t
>(
state);
212 uint8_t capabilities = 0x00;
215 capabilities |= improv::CAPABILITY_IDENTIFY;
218 service_data[3] = capabilities;
219 service_data[4] = 0x00;
220 service_data[5] = 0x00;
221 service_data[6] = 0x00;
222 service_data[7] = 0x00;
225 #ifdef USE_ESP32_IMPROV_STATE_CALLBACK 231 if (error != improv::ERROR_NONE) {
232 ESP_LOGE(TAG,
"Error: %d", error);
235 uint8_t data[1]{error};
252 ESP_LOGD(TAG,
"Setting Improv to start");
269 ESP_LOGCONFIG(TAG,
"ESP32 Improv:");
270 #ifdef USE_BINARY_SENSOR 271 LOG_BINARY_SENSOR(
" ",
"Authorizer", this->
authorizer_);
274 ESP_LOGCONFIG(TAG,
" Status Indicator: '%s'", YESNO(this->
status_indicator_ !=
nullptr));
284 improv::ImprovCommand command = improv::parse_improv_data(this->
incoming_data_);
285 switch (command.command) {
286 case improv::BAD_CHECKSUM:
287 ESP_LOGW(TAG,
"Error decoding Improv payload");
291 case improv::WIFI_SETTINGS: {
292 if (this->
state_ != improv::STATE_AUTHORIZED) {
293 ESP_LOGW(TAG,
"Settings received, but not authorized");
294 this->
set_error_(improv::ERROR_NOT_AUTHORIZED);
300 sta.set_password(command.password);
306 ESP_LOGD(TAG,
"Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
307 command.password.c_str());
310 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
314 case improv::IDENTIFY:
319 ESP_LOGW(TAG,
"Unknown Improv payload");
324 ESP_LOGV(TAG,
"Too much data received or data malformed; resetting buffer...");
327 ESP_LOGV(TAG,
"Waiting for split data packets...");
332 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
334 #ifdef USE_BINARY_SENSOR 338 ESP_LOGW(TAG,
"Timed out while connecting to Wi-Fi network");
static const uint32_t PROPERTY_WRITE
binary_sensor::BinarySensor * authorizer_
virtual void turn_on()
Enable this binary output.
BLECharacteristic * capabilities_
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
void process_incoming_data_()
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
const std::string & get_password() const
BLECharacteristic * rpc_response_
void save_wifi_sta(const std::string &ssid, const std::string &password)
BLECharacteristic * error_
virtual void turn_off()
Disable this binary output.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void on_wifi_connect_timeout_()
void create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15, uint8_t inst_id=0)
void set_value(const uint8_t *data, size_t length)
const float AFTER_BLUETOOTH
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
uint32_t IRAM_ATTR HOT millis()
void setup_characteristics()
void start_connecting(const WiFiAP &ap, bool two)
void notify(bool notification=true)
improv::Error error_state_
BLEServer * global_ble_server
CallbackManager< void(improv::State, improv::Error)> state_callback_
void set_status_indicator_state_(bool state)
void set_state_(improv::State state)
wifi::WiFiAP connecting_sta_
void dump_config() override
void send_response_(std::vector< uint8_t > &response)
BLEService * get_service(ESPBTUUID uuid)
void advertising_set_service_data(const std::vector< uint8_t > &data)
void set_ssid(const std::string &ssid)
uint32_t identify_duration_
WiFiComponent * global_wifi_component
std::vector< uint8_t > & get_value()
float get_setup_priority() const override
uint32_t authorized_duration_
void on_write(const std::function< void(const std::vector< uint8_t > &)> &&func)
ESP32ImprovComponent * global_improv_component
uint32_t authorized_start_
std::vector< uint8_t > incoming_data_
std::string to_string(int value)
void add_descriptor(BLEDescriptor *descriptor)
void on_client_disconnect() override
bool status_indicator_state_
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
void set_error_(improv::Error error)
void set_sta(const WiFiAP &ap)
Implementation of SPI Controller mode.
static ESPBTUUID from_raw(const uint8_t *data)
const std::string & get_ssid() const
output::BinaryOutput * status_indicator_
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
BLECharacteristic * status_
static const uint32_t PROPERTY_READ
static const uint32_t PROPERTY_NOTIFY