From 659fdefccb851fa9e76d560b7574e5589b890d24 Mon Sep 17 00:00:00 2001 From: NewoPL <27411874+NewoPL@users.noreply.github.com> Date: Tue, 16 Jul 2024 08:28:23 +0200 Subject: [PATCH] [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 --- esphome/components/wifi/wifi_component_esp32_arduino.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/esphome/components/wifi/wifi_component_esp32_arduino.cpp b/esphome/components/wifi/wifi_component_esp32_arduino.cpp index fc954a2333..71548b7a3e 100644 --- a/esphome/components/wifi/wifi_component_esp32_arduino.cpp +++ b/esphome/components/wifi/wifi_component_esp32_arduino.cpp @@ -82,8 +82,8 @@ bool WiFiComponent::wifi_mode_(optional sta, optional 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));