mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 04:33:47 +00:00
[wifi] Optimize WiFi network storage with FixedVector
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from esphome import automation
|
from esphome import automation
|
||||||
from esphome.automation import Condition
|
from esphome.automation import Condition
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
|
from esphome.codegen import MockObj
|
||||||
from esphome.components.const import CONF_USE_PSRAM
|
from esphome.components.const import CONF_USE_PSRAM
|
||||||
from esphome.components.esp32 import add_idf_sdkconfig_option, const, get_esp32_variant
|
from esphome.components.esp32 import add_idf_sdkconfig_option, const, get_esp32_variant
|
||||||
from esphome.components.network import IPAddress
|
from esphome.components.network import IPAddress
|
||||||
@@ -378,14 +379,22 @@ async def to_code(config):
|
|||||||
# Track if any network uses Enterprise authentication
|
# Track if any network uses Enterprise authentication
|
||||||
has_eap = False
|
has_eap = False
|
||||||
|
|
||||||
def add_sta(ap, network):
|
# Build all WiFiAP objects
|
||||||
ip_config = network.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
|
networks = config.get(CONF_NETWORKS, [])
|
||||||
cg.add(var.add_sta(wifi_network(network, ap, ip_config)))
|
if networks:
|
||||||
|
wifi_aps: list[MockObj] = []
|
||||||
|
for network in networks:
|
||||||
|
if CONF_EAP in network:
|
||||||
|
has_eap = True
|
||||||
|
ip_config = network.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
|
||||||
|
# Create a WiFiAP variable for each network
|
||||||
|
ap_var = cg.new_variable(network[CONF_ID], WiFiAP())
|
||||||
|
# Configure the WiFiAP
|
||||||
|
wifi_network(network, ap_var, ip_config)
|
||||||
|
wifi_aps.append(ap_var)
|
||||||
|
|
||||||
for network in config.get(CONF_NETWORKS, []):
|
# Set all WiFi networks at once
|
||||||
if CONF_EAP in network:
|
cg.add(var.set_stas(wifi_aps))
|
||||||
has_eap = True
|
|
||||||
cg.with_local_variable(network[CONF_ID], WiFiAP(), add_sta, network)
|
|
||||||
|
|
||||||
if CONF_AP in config:
|
if CONF_AP in config:
|
||||||
conf = config[CONF_AP]
|
conf = config[CONF_AP]
|
||||||
|
|||||||
@@ -330,11 +330,8 @@ float WiFiComponent::get_loop_priority() const {
|
|||||||
return 10.0f; // before other loop components
|
return 10.0f; // before other loop components
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiComponent::add_sta(const WiFiAP &ap) { this->sta_.push_back(ap); }
|
void WiFiComponent::set_stas(const std::initializer_list<WiFiAP> &aps) { this->sta_ = aps; }
|
||||||
void WiFiComponent::set_sta(const WiFiAP &ap) {
|
void WiFiComponent::set_sta(const WiFiAP &ap) { this->set_stas({ap}); }
|
||||||
this->clear_sta();
|
|
||||||
this->add_sta(ap);
|
|
||||||
}
|
|
||||||
void WiFiComponent::clear_sta() { this->sta_.clear(); }
|
void WiFiComponent::clear_sta() { this->sta_.clear(); }
|
||||||
void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
|
void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
|
||||||
SavedWifiSettings save{}; // zero-initialized - all bytes set to \0, guaranteeing null termination
|
SavedWifiSettings save{}; // zero-initialized - all bytes set to \0, guaranteeing null termination
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ class WiFiComponent : public Component {
|
|||||||
|
|
||||||
void set_sta(const WiFiAP &ap);
|
void set_sta(const WiFiAP &ap);
|
||||||
WiFiAP get_sta() { return this->selected_ap_; }
|
WiFiAP get_sta() { return this->selected_ap_; }
|
||||||
void add_sta(const WiFiAP &ap);
|
void set_stas(const std::initializer_list<WiFiAP> &aps);
|
||||||
void clear_sta();
|
void clear_sta();
|
||||||
|
|
||||||
#ifdef USE_WIFI_AP
|
#ifdef USE_WIFI_AP
|
||||||
@@ -393,7 +393,7 @@ class WiFiComponent : public Component {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string use_address_;
|
std::string use_address_;
|
||||||
std::vector<WiFiAP> sta_;
|
FixedVector<WiFiAP> sta_;
|
||||||
std::vector<WiFiSTAPriority> sta_priorities_;
|
std::vector<WiFiSTAPriority> sta_priorities_;
|
||||||
wifi_scan_vector_t<WiFiScanResult> scan_result_;
|
wifi_scan_vector_t<WiFiScanResult> scan_result_;
|
||||||
WiFiAP selected_ap_;
|
WiFiAP selected_ap_;
|
||||||
|
|||||||
Reference in New Issue
Block a user