esp32_improv add timeout (#5556)

This commit is contained in:
Jesse Hills 2023-10-19 11:42:52 +13:00
parent 02449f24c9
commit 9579423b24
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
3 changed files with 17 additions and 7 deletions

View file

@ -36,6 +36,9 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional( cv.Optional(
CONF_AUTHORIZED_DURATION, default="1min" CONF_AUTHORIZED_DURATION, default="1min"
): cv.positive_time_period_milliseconds, ): cv.positive_time_period_milliseconds,
cv.Optional(
CONF_WIFI_TIMEOUT, default="1min"
): cv.positive_time_period_milliseconds,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -53,6 +56,8 @@ async def to_code(config):
cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION])) cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION]))
cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION])) cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION]))
cg.add(var.set_wifi_timeout(config[CONF_WIFI_TIMEOUT]))
if CONF_AUTHORIZER in config and config[CONF_AUTHORIZER] is not None: if CONF_AUTHORIZER in config and config[CONF_AUTHORIZER] is not None:
activator = await cg.get_variable(config[CONF_AUTHORIZER]) activator = await cg.get_variable(config[CONF_AUTHORIZER])
cg.add(var.set_authorizer(activator)) cg.add(var.set_authorizer(activator))

View file

@ -51,6 +51,9 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; } void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; } void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
protected: protected:
bool should_start_{false}; bool should_start_{false};
bool setup_complete_{false}; bool setup_complete_{false};
@ -60,6 +63,8 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
uint32_t authorized_start_{0}; uint32_t authorized_start_{0};
uint32_t authorized_duration_; uint32_t authorized_duration_;
uint32_t wifi_timeout_{};
std::vector<uint8_t> incoming_data_; std::vector<uint8_t> incoming_data_;
wifi::WiFiAP connecting_sta_; wifi::WiFiAP connecting_sta_;

View file

@ -8,16 +8,16 @@
#include <user_interface.h> #include <user_interface.h>
#endif #endif
#include <utility>
#include <algorithm> #include <algorithm>
#include "lwip/err.h" #include <utility>
#include "lwip/dns.h" #include "lwip/dns.h"
#include "lwip/err.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/util.h" #include "esphome/core/util.h"
#include "esphome/core/application.h"
#ifdef USE_CAPTIVE_PORTAL #ifdef USE_CAPTIVE_PORTAL
#include "esphome/components/captive_portal/captive_portal.h" #include "esphome/components/captive_portal/captive_portal.h"
@ -96,7 +96,7 @@ void WiFiComponent::start() {
#endif #endif
} }
#ifdef USE_IMPROV #ifdef USE_IMPROV
if (esp32_improv::global_improv_component != nullptr) { if (!this->has_sta() && esp32_improv::global_improv_component != nullptr) {
if (this->wifi_mode_(true, {})) if (this->wifi_mode_(true, {}))
esp32_improv::global_improv_component->start(); esp32_improv::global_improv_component->start();
} }
@ -163,8 +163,8 @@ void WiFiComponent::loop() {
} }
#ifdef USE_IMPROV #ifdef USE_IMPROV
if (esp32_improv::global_improv_component != nullptr) { if (esp32_improv::global_improv_component != nullptr && !esp32_improv::global_improv_component->is_active()) {
if (!this->is_connected()) { if (now - this->last_connected_ > esp32_improv::global_improv_component->get_wifi_timeout()) {
if (this->wifi_mode_(true, {})) if (this->wifi_mode_(true, {}))
esp32_improv::global_improv_component->start(); esp32_improv::global_improv_component->start();
} }