[wifi] Hostname may not be set as expected on Arduino platform (#7050)

* bug #6014: workaround for not setting hostname on arduino plarform

* moving handle initailisation to ESPHOME_EVENT_ID_WIFI_STA_START callback
This commit is contained in:
NewoPL 2024-07-16 08:28:23 +02:00 committed by GitHub
parent 8980996b1a
commit 659fdefccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -82,8 +82,8 @@ bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
// WiFiClass::mode above calls esp_netif_create_default_wifi_sta() and
// esp_netif_create_default_wifi_ap(), which creates the interfaces.
if (set_sta)
s_sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
// s_sta_netif handle is set during ESPHOME_EVENT_ID_WIFI_STA_START event
#ifdef USE_WIFI_AP
if (set_ap)
s_ap_netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
@ -495,6 +495,7 @@ void WiFiComponent::wifi_event_callback_(esphome_wifi_event_id_t event, esphome_
case ESPHOME_EVENT_ID_WIFI_STA_START: {
ESP_LOGV(TAG, "Event: WiFi STA start");
// apply hostname
s_sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
esp_err_t err = esp_netif_set_hostname(s_sta_netif, App.get_name().c_str());
if (err != ERR_OK) {
ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));