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";
35 this->
rpc_->
on_write([
this](
const std::vector<uint8_t> &data) {
52 uint8_t capabilities = 0x00;
54 capabilities |= improv::CAPABILITY_IDENTIFY;
73 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
76 ESP_LOGD(TAG,
"Service started!");
82 case improv::STATE_AWAITING_AUTHORIZATION: {
94 case improv::STATE_AUTHORIZED: {
97 ESP_LOGD(TAG,
"Authorization timeout");
98 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
104 if ((now % 1000) < 500) {
113 case improv::STATE_PROVISIONING: {
115 if ((now % 200) < 100) {
128 std::vector<std::string> urls = {ESPHOME_MY_LINK};
131 std::string webserver_url =
"http://" + ip.
str() +
":" +
to_string(USE_WEBSERVER_PORT);
132 urls.push_back(webserver_url);
134 std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls);
143 case improv::STATE_PROVISIONED: {
158 uint32_t time = now % 1000;
159 if (time < 600 && time % 200 < 100) {
169 ESP_LOGV(TAG,
"Setting state: %d", state);
172 uint8_t data[1]{state};
180 if (error != improv::ERROR_NONE) {
181 ESP_LOGE(TAG,
"Error: %d", error);
184 uint8_t data[1]{error};
201 ESP_LOGD(TAG,
"Setting Improv to start");
215 ESP_LOGCONFIG(TAG,
"ESP32 Improv:");
216 LOG_BINARY_SENSOR(
" ",
"Authorizer", this->
authorizer_);
217 ESP_LOGCONFIG(TAG,
" Status Indicator: '%s'", YESNO(this->
status_indicator_ !=
nullptr));
226 improv::ImprovCommand command = improv::parse_improv_data(this->
incoming_data_);
227 switch (command.command) {
228 case improv::BAD_CHECKSUM:
229 ESP_LOGW(TAG,
"Error decoding Improv payload");
233 case improv::WIFI_SETTINGS: {
234 if (this->
state_ != improv::STATE_AUTHORIZED) {
235 ESP_LOGW(TAG,
"Settings received, but not authorized");
236 this->
set_error_(improv::ERROR_NOT_AUTHORIZED);
242 sta.set_password(command.password);
248 ESP_LOGD(TAG,
"Received Improv wifi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
249 command.password.c_str());
252 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
256 case improv::IDENTIFY:
261 ESP_LOGW(TAG,
"Unknown Improv payload");
266 ESP_LOGV(TAG,
"Too much data came in, or malformed resetting buffer...");
269 ESP_LOGV(TAG,
"Waiting for split data packets...");
274 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
278 ESP_LOGW(TAG,
"Timed out trying to connect to given WiFi 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 set_value(const uint8_t *data, size_t length)
std::shared_ptr< BLEService > create_service(const uint8_t *uuid, bool advertise=false)
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 notify(bool notification=true)
BLEServer * global_ble_server
void set_state_(improv::State state)
wifi::WiFiAP connecting_sta_
void dump_config() override
void send_response_(std::vector< uint8_t > &response)
void set_ssid(const std::string &ssid)
bool state
The current reported state of the binary sensor.
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)
BLEAdvertising * get_advertising()
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
void set_error_(improv::Error error)
void set_sta(const WiFiAP &ap)
Implementation of SPI Controller mode.
const std::string & get_ssid() const
output::BinaryOutput * status_indicator_
BLECharacteristic * status_
network::IPAddress wifi_sta_ip()
static const uint32_t PROPERTY_READ
std::shared_ptr< BLEService > service_
static const uint32_t PROPERTY_NOTIFY