diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 64abf03c519..0f5aff3ba18 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -412,6 +412,14 @@ bool WiFiComponent::matches_configured_ssid_(const char *ssid) const { return false; } +void WiFiComponent::log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel) { +#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE + char bssid_s[18]; + format_mac_addr_upper(bssid, bssid_s); + ESP_LOGV(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s)") " %ddB Ch:%u", ssid, bssid_s, rssi, channel); +#endif +} + int8_t WiFiComponent::find_next_hidden_sta_(int8_t start_index) { // Find next SSID to try in RETRY_HIDDEN phase. // @@ -1234,18 +1242,6 @@ __attribute__((noinline)) static void log_scan_result(const WiFiScanResult &res) #endif } -#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE -// Helper function to log non-matching scan results at verbose level -__attribute__((noinline)) static void log_scan_result_non_matching(const WiFiScanResult &res) { - char bssid_s[18]; - auto bssid = res.get_bssid(); - format_mac_addr_upper(bssid.data(), bssid_s); - - ESP_LOGV(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s, - LOG_STR_ARG(get_signal_bars(res.get_rssi()))); -} -#endif - void WiFiComponent::check_scanning_finished() { if (!this->scan_done_) { if (millis() - this->action_started_ > WIFI_SCAN_TIMEOUT_MS) { @@ -1282,21 +1278,12 @@ void WiFiComponent::check_scanning_finished() { // Sort scan results using insertion sort for better memory efficiency insertion_sort_scan_results(this->scan_result_); - size_t non_matching_count = 0; + // Log matching networks (non-matching already logged at VERBOSE in scan callback) for (auto &res : this->scan_result_) { if (res.get_matches()) { log_scan_result(res); - } else { -#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE - log_scan_result_non_matching(res); -#else - non_matching_count++; -#endif } } - if (non_matching_count > 0) { - ESP_LOGD(TAG, "- %zu non-matching (VERBOSE to show)", non_matching_count); - } // SYNCHRONIZATION POINT: Establish link between scan_result_[0] and selected_sta_index_ // After sorting, scan_result_[0] contains the best network. Now find which sta_[i] config diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 3a6bf2994df..33263bbdfec 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -543,6 +543,8 @@ class WiFiComponent : public Component { bool needs_full_scan_results_() const; /// Check if SSID matches any configured network (for scan result filtering) bool matches_configured_ssid_(const char *ssid) const; + /// Log a discarded scan result at VERBOSE level + static void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel); /// Find next SSID that wasn't in scan results (might be hidden) /// Returns index of next potentially hidden SSID, or -1 if none found /// @param start_index Start searching from index after this (-1 to start from beginning) diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index dad899ff0ba..fbfa423ef7d 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -777,6 +777,8 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) { this->scan_result_.emplace_back( bssid_t{it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, std::string(ssid_cstr, it->ssid_len), it->channel, it->rssi, it->authmode != AUTH_OPEN, it->is_hidden != 0); + } else { + WiFiComponent::log_discarded_scan_result_(ssid_cstr, it->bssid, it->rssi, it->channel); } } this->scan_done_ = true; diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index a8eef686d0e..5a7fbd35f08 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -856,8 +856,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) { std::string ssid(ssid_cstr); this->scan_result_.emplace_back(bssid, std::move(ssid), record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty()); + } else { + WiFiComponent::log_discarded_scan_result_(ssid_cstr, record.bssid, record.rssi, record.primary); } } + ESP_LOGD(TAG, "Scan complete: %u found, %zu stored%s", number, this->scan_result_.size(), + needs_full ? "" : " (filtered)"); #ifdef USE_WIFI_SCAN_RESULTS_LISTENERS for (auto *listener : this->scan_results_listeners_) { listener->on_wifi_scan_results(this->scan_result_); diff --git a/esphome/components/wifi/wifi_component_libretiny.cpp b/esphome/components/wifi/wifi_component_libretiny.cpp index f0928603e1e..5f8ef3a3404 100644 --- a/esphome/components/wifi/wifi_component_libretiny.cpp +++ b/esphome/components/wifi/wifi_component_libretiny.cpp @@ -689,6 +689,9 @@ void WiFiComponent::wifi_scan_done_callback_() { this->scan_result_.emplace_back( bssid_t{ap.bssid[0], ap.bssid[1], ap.bssid[2], ap.bssid[3], ap.bssid[4], ap.bssid[5]}, std::string(ssid_cstr), ap.channel, ap.rssi, ap.auth != WIFI_AUTH_OPEN, ssid_cstr[0] == '\0'); + } else { + auto &ap = scan->ap[i]; + WiFiComponent::log_discarded_scan_result_(ssid_cstr, ap.bssid, ap.rssi, ap.channel); } } WiFi.scanDelete(); diff --git a/esphome/components/wifi/wifi_component_pico_w.cpp b/esphome/components/wifi/wifi_component_pico_w.cpp index 0db2300b8f7..57fc89d36be 100644 --- a/esphome/components/wifi/wifi_component_pico_w.cpp +++ b/esphome/components/wifi/wifi_component_pico_w.cpp @@ -141,6 +141,7 @@ void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *re // Skip networks that don't match any configured SSID (unless full results needed) if (!this->needs_full_scan_results_() && !this->matches_configured_ssid_(ssid_cstr)) { + WiFiComponent::log_discarded_scan_result_(ssid_cstr, result->bssid, result->rssi, result->channel); return; }