diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index d16c94fa13..e57bf25b8c 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -148,7 +148,7 @@ void WiFiComponent::loop() { switch (this->state_) { case WIFI_COMPONENT_STATE_COOLDOWN: { - this->status_set_warning("waiting to reconnect"); + this->status_set_warning(LOG_STR("waiting to reconnect")); if (millis() - this->action_started_ > 5000) { if (this->fast_connect_ || this->retry_hidden_) { if (!this->selected_ap_.get_bssid().has_value()) @@ -161,13 +161,13 @@ void WiFiComponent::loop() { break; } case WIFI_COMPONENT_STATE_STA_SCANNING: { - this->status_set_warning("scanning for networks"); + this->status_set_warning(LOG_STR("scanning for networks")); this->check_scanning_finished(); break; } case WIFI_COMPONENT_STATE_STA_CONNECTING: case WIFI_COMPONENT_STATE_STA_CONNECTING_2: { - this->status_set_warning("associating to network"); + this->status_set_warning(LOG_STR("associating to network")); this->check_connecting_finished(); break; } diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 40cda17ca3..5689a059a0 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -16,7 +16,7 @@ namespace esphome { static const char *const TAG = "component"; -static const char *const UNSPECIFIED_MESSAGE = "unspecified"; +static const auto *const UNSPECIFIED_MESSAGE = LOG_STR("unspecified"); // Global vectors for component data that doesn't belong in every instance. // Using vector instead of unordered_map for both because: @@ -143,7 +143,7 @@ void Component::call_dump_config() { } } ESP_LOGE(TAG, " %s is marked FAILED: %s", this->get_component_source(), - error_msg ? error_msg : UNSPECIFIED_MESSAGE); + error_msg ? error_msg : LOG_STR_ARG(UNSPECIFIED_MESSAGE)); } } @@ -280,20 +280,36 @@ bool Component::is_ready() const { bool Component::can_proceed() { return true; } bool Component::status_has_warning() const { return this->component_state_ & STATUS_LED_WARNING; } bool Component::status_has_error() const { return this->component_state_ & STATUS_LED_ERROR; } -void Component::status_set_warning(const char *message) { + +void Component::status_set_warning_flag_() { // Don't spam the log. This risks missing different warning messages though. if ((this->component_state_ & STATUS_LED_WARNING) != 0) return; this->component_state_ |= STATUS_LED_WARNING; App.app_state_ |= STATUS_LED_WARNING; - ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE); } + +void Component::status_set_warning(const char *message) { + this->status_set_warning_flag_(); + if ((this->component_state_ & STATUS_LED_WARNING) != 0) + ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), + message ? message : LOG_STR_ARG(UNSPECIFIED_MESSAGE)); +} +#ifdef USE_STORE_LOG_STR_IN_FLASH +void Component::status_set_warning(const LogString *message) { + this->status_set_warning_flag_(); + if ((this->component_state_ & STATUS_LED_WARNING) != 0) + ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), + message ? LOG_STR_ARG(message) : LOG_STR_ARG(UNSPECIFIED_MESSAGE)); +} +#endif void Component::status_set_error(const char *message) { if ((this->component_state_ & STATUS_LED_ERROR) != 0) return; this->component_state_ |= STATUS_LED_ERROR; App.app_state_ |= STATUS_LED_ERROR; - ESP_LOGE(TAG, "%s set Error flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE); + ESP_LOGE(TAG, "%s set Error flag: %s", this->get_component_source(), + message ? message : LOG_STR_ARG(UNSPECIFIED_MESSAGE)); if (message != nullptr) { // Lazy allocate the error messages vector if needed if (!component_error_messages) { diff --git a/esphome/core/component.h b/esphome/core/component.h index 096c6f9c69..98a183fc21 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -203,6 +203,9 @@ class Component { bool status_has_error() const; void status_set_warning(const char *message = nullptr); +#ifdef USE_STORE_LOG_STR_IN_FLASH + void status_set_warning(const LogString *message); +#endif void status_set_error(const char *message = nullptr); @@ -239,6 +242,9 @@ class Component { /// Helper to set component state (clears state bits and sets new state) void set_component_state_(uint8_t state); + /// Helper to set warning flag without duplicating logic + void status_set_warning_flag_(); + /** Set an interval function with a unique name. Empty name means no cancelling possible. * * This will call f every interval ms. Can be cancelled via CancelInterval().