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