[valve] Store valve state strings in flash on ESP8266 (#12202)

This commit is contained in:
J. Nick Koston
2025-12-02 10:02:51 -06:00
committed by GitHub
parent d1583456e9
commit 3f08cacf71
4 changed files with 20 additions and 14 deletions

View File

@@ -895,7 +895,11 @@ void PrometheusHandler::valve_row_(AsyncResponseStream *stream, valve::Valve *ob
stream->print(ESPHOME_F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(ESPHOME_F("\",operation=\""));
stream->print(valve::valve_operation_to_str(obj->current_operation));
#ifdef USE_STORE_LOG_STR_IN_FLASH
stream->print((const __FlashStringHelper *) valve::valve_operation_to_str(obj->current_operation));
#else
stream->print((const char *) valve::valve_operation_to_str(obj->current_operation));
#endif
stream->print(ESPHOME_F("\"} "));
stream->print(ESPHOME_F("1.0"));
stream->print(ESPHOME_F("\n"));

View File

@@ -12,25 +12,25 @@ static const char *const TAG = "valve";
const float VALVE_OPEN = 1.0f;
const float VALVE_CLOSED = 0.0f;
const char *valve_command_to_str(float pos) {
const LogString *valve_command_to_str(float pos) {
if (pos == VALVE_OPEN) {
return "OPEN";
return LOG_STR("OPEN");
} else if (pos == VALVE_CLOSED) {
return "CLOSE";
return LOG_STR("CLOSE");
} else {
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
const char *valve_operation_to_str(ValveOperation op) {
const LogString *valve_operation_to_str(ValveOperation op) {
switch (op) {
case VALVE_OPERATION_IDLE:
return "IDLE";
return LOG_STR("IDLE");
case VALVE_OPERATION_OPENING:
return "OPENING";
return LOG_STR("OPENING");
case VALVE_OPERATION_CLOSING:
return "CLOSING";
return LOG_STR("CLOSING");
default:
return "UNKNOWN";
return LOG_STR("UNKNOWN");
}
}
@@ -82,7 +82,7 @@ void ValveCall::perform() {
if (traits.get_supports_position()) {
ESP_LOGD(TAG, " Position: %.0f%%", *this->position_ * 100.0f);
} else {
ESP_LOGD(TAG, " Command: %s", valve_command_to_str(*this->position_));
ESP_LOGD(TAG, " Command: %s", LOG_STR_ARG(valve_command_to_str(*this->position_)));
}
}
if (this->toggle_.has_value()) {
@@ -146,7 +146,7 @@ void Valve::publish_state(bool save) {
ESP_LOGD(TAG, " State: UNKNOWN");
}
}
ESP_LOGD(TAG, " Current Operation: %s", valve_operation_to_str(this->current_operation));
ESP_LOGD(TAG, " Current Operation: %s", LOG_STR_ARG(valve_operation_to_str(this->current_operation)));
this->state_callback_.call();
#if defined(USE_VALVE) && defined(USE_CONTROLLER_REGISTRY)

View File

@@ -3,6 +3,7 @@
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/preferences.h"
#include "valve_traits.h"
@@ -81,7 +82,7 @@ enum ValveOperation : uint8_t {
VALVE_OPERATION_CLOSING,
};
const char *valve_operation_to_str(ValveOperation op);
const LogString *valve_operation_to_str(ValveOperation op);
/** Base class for all valve devices.
*

View File

@@ -1565,7 +1565,8 @@ std::string WebServer::valve_json(valve::Valve *obj, JsonDetail start_config) {
set_json_icon_state_value(root, obj, "valve", obj->is_fully_closed() ? "CLOSED" : "OPEN", obj->position,
start_config);
root["current_operation"] = valve::valve_operation_to_str(obj->current_operation);
char buf[PSTR_LOCAL_SIZE];
root["current_operation"] = PSTR_LOCAL(valve::valve_operation_to_str(obj->current_operation));
if (obj->get_traits().get_supports_position())
root["position"] = obj->position;