Store strings only used for logging in flash (#2274)

Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Oxan van Leeuwen
2021-09-13 09:48:52 +02:00
committed by GitHub
parent e18dfdd656
commit d594a6fcbc
25 changed files with 241 additions and 251 deletions
+23 -21
View File
@@ -9,8 +9,8 @@ void ClimateCall::perform() {
ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
this->validate_();
if (this->mode_.has_value()) {
const char *mode_s = climate_mode_to_string(*this->mode_);
ESP_LOGD(TAG, " Mode: %s", mode_s);
const LogString *mode_s = climate_mode_to_string(*this->mode_);
ESP_LOGD(TAG, " Mode: %s", LOG_STR_ARG(mode_s));
}
if (this->custom_fan_mode_.has_value()) {
this->fan_mode_.reset();
@@ -18,8 +18,8 @@ void ClimateCall::perform() {
}
if (this->fan_mode_.has_value()) {
this->custom_fan_mode_.reset();
const char *fan_mode_s = climate_fan_mode_to_string(*this->fan_mode_);
ESP_LOGD(TAG, " Fan: %s", fan_mode_s);
const LogString *fan_mode_s = climate_fan_mode_to_string(*this->fan_mode_);
ESP_LOGD(TAG, " Fan: %s", LOG_STR_ARG(fan_mode_s));
}
if (this->custom_preset_.has_value()) {
this->preset_.reset();
@@ -27,12 +27,12 @@ void ClimateCall::perform() {
}
if (this->preset_.has_value()) {
this->custom_preset_.reset();
const char *preset_s = climate_preset_to_string(*this->preset_);
ESP_LOGD(TAG, " Preset: %s", preset_s);
const LogString *preset_s = climate_preset_to_string(*this->preset_);
ESP_LOGD(TAG, " Preset: %s", LOG_STR_ARG(preset_s));
}
if (this->swing_mode_.has_value()) {
const char *swing_mode_s = climate_swing_mode_to_string(*this->swing_mode_);
ESP_LOGD(TAG, " Swing: %s", swing_mode_s);
const LogString *swing_mode_s = climate_swing_mode_to_string(*this->swing_mode_);
ESP_LOGD(TAG, " Swing: %s", LOG_STR_ARG(swing_mode_s));
}
if (this->target_temperature_.has_value()) {
ESP_LOGD(TAG, " Target Temperature: %.2f", *this->target_temperature_);
@@ -50,7 +50,7 @@ void ClimateCall::validate_() {
if (this->mode_.has_value()) {
auto mode = *this->mode_;
if (!traits.supports_mode(mode)) {
ESP_LOGW(TAG, " Mode %s is not supported by this device!", climate_mode_to_string(mode));
ESP_LOGW(TAG, " Mode %s is not supported by this device!", LOG_STR_ARG(climate_mode_to_string(mode)));
this->mode_.reset();
}
}
@@ -63,7 +63,8 @@ void ClimateCall::validate_() {
} else if (this->fan_mode_.has_value()) {
auto fan_mode = *this->fan_mode_;
if (!traits.supports_fan_mode(fan_mode)) {
ESP_LOGW(TAG, " Fan Mode %s is not supported by this device!", climate_fan_mode_to_string(fan_mode));
ESP_LOGW(TAG, " Fan Mode %s is not supported by this device!",
LOG_STR_ARG(climate_fan_mode_to_string(fan_mode)));
this->fan_mode_.reset();
}
}
@@ -76,14 +77,15 @@ void ClimateCall::validate_() {
} else if (this->preset_.has_value()) {
auto preset = *this->preset_;
if (!traits.supports_preset(preset)) {
ESP_LOGW(TAG, " Preset %s is not supported by this device!", climate_preset_to_string(preset));
ESP_LOGW(TAG, " Preset %s is not supported by this device!", LOG_STR_ARG(climate_preset_to_string(preset)));
this->preset_.reset();
}
}
if (this->swing_mode_.has_value()) {
auto swing_mode = *this->swing_mode_;
if (!traits.supports_swing_mode(swing_mode)) {
ESP_LOGW(TAG, " Swing Mode %s is not supported by this device!", climate_swing_mode_to_string(swing_mode));
ESP_LOGW(TAG, " Swing Mode %s is not supported by this device!",
LOG_STR_ARG(climate_swing_mode_to_string(swing_mode)));
this->swing_mode_.reset();
}
}
@@ -373,24 +375,24 @@ void Climate::publish_state() {
ESP_LOGD(TAG, "'%s' - Sending state:", this->name_.c_str());
auto traits = this->get_traits();
ESP_LOGD(TAG, " Mode: %s", climate_mode_to_string(this->mode));
ESP_LOGD(TAG, " Mode: %s", LOG_STR_ARG(climate_mode_to_string(this->mode)));
if (traits.get_supports_action()) {
ESP_LOGD(TAG, " Action: %s", climate_action_to_string(this->action));
ESP_LOGD(TAG, " Action: %s", LOG_STR_ARG(climate_action_to_string(this->action)));
}
if (traits.get_supports_fan_modes() && this->fan_mode.has_value()) {
ESP_LOGD(TAG, " Fan Mode: %s", climate_fan_mode_to_string(this->fan_mode.value()));
ESP_LOGD(TAG, " Fan Mode: %s", LOG_STR_ARG(climate_fan_mode_to_string(this->fan_mode.value())));
}
if (!traits.get_supported_custom_fan_modes().empty() && this->custom_fan_mode.has_value()) {
ESP_LOGD(TAG, " Custom Fan Mode: %s", this->custom_fan_mode.value().c_str());
}
if (traits.get_supports_presets() && this->preset.has_value()) {
ESP_LOGD(TAG, " Preset: %s", climate_preset_to_string(this->preset.value()));
ESP_LOGD(TAG, " Preset: %s", LOG_STR_ARG(climate_preset_to_string(this->preset.value())));
}
if (!traits.get_supported_custom_presets().empty() && this->custom_preset.has_value()) {
ESP_LOGD(TAG, " Custom Preset: %s", this->custom_preset.value().c_str());
}
if (traits.get_supports_swing_modes()) {
ESP_LOGD(TAG, " Swing Mode: %s", climate_swing_mode_to_string(this->swing_mode));
ESP_LOGD(TAG, " Swing Mode: %s", LOG_STR_ARG(climate_swing_mode_to_string(this->swing_mode)));
}
if (traits.get_supports_current_temperature()) {
ESP_LOGD(TAG, " Current Temperature: %.2f°C", this->current_temperature);
@@ -534,12 +536,12 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported modes:");
for (ClimateMode m : traits.get_supported_modes())
ESP_LOGCONFIG(tag, " - %s", climate_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_mode_to_string(m)));
}
if (!traits.get_supported_fan_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported fan modes:");
for (ClimateFanMode m : traits.get_supported_fan_modes())
ESP_LOGCONFIG(tag, " - %s", climate_fan_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_fan_mode_to_string(m)));
}
if (!traits.get_supported_custom_fan_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported custom fan modes:");
@@ -549,7 +551,7 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_presets().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported presets:");
for (ClimatePreset p : traits.get_supported_presets())
ESP_LOGCONFIG(tag, " - %s", climate_preset_to_string(p));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_preset_to_string(p)));
}
if (!traits.get_supported_custom_presets().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported custom presets:");
@@ -559,7 +561,7 @@ void Climate::dump_traits_(const char *tag) {
if (!traits.get_supported_swing_modes().empty()) {
ESP_LOGCONFIG(tag, " [x] Supported swing modes:");
for (ClimateSwingMode m : traits.get_supported_swing_modes())
ESP_LOGCONFIG(tag, " - %s", climate_swing_mode_to_string(m));
ESP_LOGCONFIG(tag, " - %s", LOG_STR_ARG(climate_swing_mode_to_string(m)));
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ namespace climate {
#define LOG_CLIMATE(prefix, type, obj) \
if ((obj) != nullptr) { \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
}
class Climate;
+49 -49
View File
@@ -3,105 +3,105 @@
namespace esphome {
namespace climate {
const char *climate_mode_to_string(ClimateMode mode) {
const LogString *climate_mode_to_string(ClimateMode mode) {
switch (mode) {
case CLIMATE_MODE_OFF:
return "OFF";
case CLIMATE_MODE_AUTO:
return "AUTO";
case CLIMATE_MODE_COOL:
return "COOL";
case CLIMATE_MODE_HEAT:
return "HEAT";
case CLIMATE_MODE_FAN_ONLY:
return "FAN_ONLY";
case CLIMATE_MODE_DRY:
return "DRY";
return LOG_STR("OFF");
case CLIMATE_MODE_HEAT_COOL:
return "HEAT_COOL";
return LOG_STR("HEAT_COOL");
case CLIMATE_MODE_AUTO:
return LOG_STR("AUTO");
case CLIMATE_MODE_COOL:
return LOG_STR("COOL");
case CLIMATE_MODE_HEAT:
return LOG_STR("HEAT");
case CLIMATE_MODE_FAN_ONLY:
return LOG_STR("FAN_ONLY");
case CLIMATE_MODE_DRY:
return LOG_STR("DRY");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_action_to_string(ClimateAction action) {
const LogString *climate_action_to_string(ClimateAction action) {
switch (action) {
case CLIMATE_ACTION_OFF:
return "OFF";
return LOG_STR("OFF");
case CLIMATE_ACTION_COOLING:
return "COOLING";
return LOG_STR("COOLING");
case CLIMATE_ACTION_HEATING:
return "HEATING";
return LOG_STR("HEATING");
case CLIMATE_ACTION_IDLE:
return "IDLE";
return LOG_STR("IDLE");
case CLIMATE_ACTION_DRYING:
return "DRYING";
return LOG_STR("DRYING");
case CLIMATE_ACTION_FAN:
return "FAN";
return LOG_STR("FAN");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_fan_mode_to_string(ClimateFanMode fan_mode) {
const LogString *climate_fan_mode_to_string(ClimateFanMode fan_mode) {
switch (fan_mode) {
case climate::CLIMATE_FAN_ON:
return "ON";
return LOG_STR("ON");
case climate::CLIMATE_FAN_OFF:
return "OFF";
return LOG_STR("OFF");
case climate::CLIMATE_FAN_AUTO:
return "AUTO";
return LOG_STR("AUTO");
case climate::CLIMATE_FAN_LOW:
return "LOW";
return LOG_STR("LOW");
case climate::CLIMATE_FAN_MEDIUM:
return "MEDIUM";
return LOG_STR("MEDIUM");
case climate::CLIMATE_FAN_HIGH:
return "HIGH";
return LOG_STR("HIGH");
case climate::CLIMATE_FAN_MIDDLE:
return "MIDDLE";
return LOG_STR("MIDDLE");
case climate::CLIMATE_FAN_FOCUS:
return "FOCUS";
return LOG_STR("FOCUS");
case climate::CLIMATE_FAN_DIFFUSE:
return "DIFFUSE";
return LOG_STR("DIFFUSE");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_swing_mode_to_string(ClimateSwingMode swing_mode) {
const LogString *climate_swing_mode_to_string(ClimateSwingMode swing_mode) {
switch (swing_mode) {
case climate::CLIMATE_SWING_OFF:
return "OFF";
return LOG_STR("OFF");
case climate::CLIMATE_SWING_BOTH:
return "BOTH";
return LOG_STR("BOTH");
case climate::CLIMATE_SWING_VERTICAL:
return "VERTICAL";
return LOG_STR("VERTICAL");
case climate::CLIMATE_SWING_HORIZONTAL:
return "HORIZONTAL";
return LOG_STR("HORIZONTAL");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *climate_preset_to_string(ClimatePreset preset) {
const LogString *climate_preset_to_string(ClimatePreset preset) {
switch (preset) {
case climate::CLIMATE_PRESET_NONE:
return "NONE";
return LOG_STR("NONE");
case climate::CLIMATE_PRESET_HOME:
return "HOME";
return LOG_STR("HOME");
case climate::CLIMATE_PRESET_ECO:
return "ECO";
return LOG_STR("ECO");
case climate::CLIMATE_PRESET_AWAY:
return "AWAY";
return LOG_STR("AWAY");
case climate::CLIMATE_PRESET_BOOST:
return "BOOST";
return LOG_STR("BOOST");
case climate::CLIMATE_PRESET_COMFORT:
return "COMFORT";
return LOG_STR("COMFORT");
case climate::CLIMATE_PRESET_SLEEP:
return "SLEEP";
return LOG_STR("SLEEP");
case climate::CLIMATE_PRESET_ACTIVITY:
return "ACTIVITY";
return LOG_STR("ACTIVITY");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
+6 -5
View File
@@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include "esphome/core/log.h"
namespace esphome {
namespace climate {
@@ -96,19 +97,19 @@ enum ClimatePreset : uint8_t {
};
/// Convert the given ClimateMode to a human-readable string.
const char *climate_mode_to_string(ClimateMode mode);
const LogString *climate_mode_to_string(ClimateMode mode);
/// Convert the given ClimateAction to a human-readable string.
const char *climate_action_to_string(ClimateAction action);
const LogString *climate_action_to_string(ClimateAction action);
/// Convert the given ClimateFanMode to a human-readable string.
const char *climate_fan_mode_to_string(ClimateFanMode mode);
const LogString *climate_fan_mode_to_string(ClimateFanMode mode);
/// Convert the given ClimateSwingMode to a human-readable string.
const char *climate_swing_mode_to_string(ClimateSwingMode mode);
const LogString *climate_swing_mode_to_string(ClimateSwingMode mode);
/// Convert the given ClimateSwingMode to a human-readable string.
const char *climate_preset_to_string(ClimatePreset preset);
const LogString *climate_preset_to_string(ClimatePreset preset);
} // namespace climate
} // namespace esphome