Print prometheus names from flash, keep the inline short string encode off ESP8266

This commit is contained in:
J. Nick Koston
2026-10-08 22:07:33 -10:00
parent 096921cddb
commit b989221c71
6 changed files with 17 additions and 10 deletions
+1 -1
View File
@@ -474,7 +474,7 @@ uint16_t APIConnection::fill_and_encode_entity_info(EntityBase *entity, InfoResp
msg.key = entity->get_object_id_hash();
if (entity->has_own_name()) {
msg.name = entity->get_name_progmem(); // encoded straight from flash
msg.name = entity->get_name_progmem();
}
// Set common EntityBase properties
+9 -2
View File
@@ -410,7 +410,7 @@ class ProtoEncode {
[[nodiscard]] static inline uint8_t *encode_short_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
uint8_t tag, const StringRef &ref) {
#ifdef USE_ESP8266
// memcpy_P reads RAM too, so RAM strings share the outlined copy
// memcpy_P reads RAM too, so RAM strings share the progmem copy
return encode_short_progmem_string_force(pos PROTO_ENCODE_DEBUG_ARG, tag, ProgmemStringRef(ref));
#else
#ifdef ESPHOME_DEBUG_API
@@ -424,7 +424,8 @@ class ProtoEncode {
#endif
}
/// encode_short_string_force for a string that may be in flash (PROGMEM).
[[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *encode_short_progmem_string_force(
#ifdef USE_ESP8266
[[nodiscard]] static inline uint8_t *encode_short_progmem_string_force(
uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, const ProgmemStringRef &ref) {
#ifdef ESPHOME_DEBUG_API
assert(ref.size() < 128 && "encode_short_progmem_string_force: string exceeds max_data_length < 128");
@@ -435,6 +436,12 @@ class ProtoEncode {
progmem_memcpy(pos + 2, ref.progmem_ptr(), ref.size());
return pos + 2 + ref.size();
}
#else
[[nodiscard]] static inline uint8_t *encode_short_progmem_string_force(
uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, const ProgmemStringRef &ref) {
return encode_short_string_force(pos PROTO_ENCODE_DEBUG_ARG, tag, ref.ram_ref());
}
#endif
/// Write a precomputed tag byte + 32-bit value. Outlined on embedded: one copy beats inline stores per field.
[[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *write_tag_and_fixed32(
uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, uint32_t value) {
+1 -1
View File
@@ -29,7 +29,7 @@ class PIDAutotuner {
void dump_config();
/// Name shown in log lines; must outlive the autotuner (the climate entity's name).
/// Name shown in log lines; must outlive the autotuner.
void set_autotuner_id(const LogString *id) { this->id_ = id; }
void set_noiseband(float noiseband) {
@@ -125,8 +125,7 @@ void PrometheusHandler::print_relabel_name_(AsyncResponseStream *stream, EntityB
stream->print(item->second.c_str());
return;
}
char name_buf[ENTITY_NAME_BUF_SIZE];
stream->print(obj->get_name_to(name_buf).c_str());
stream->print(reinterpret_cast<ProgmemStr>(obj->get_name_progmem().progmem_ptr()));
}
void PrometheusHandler::add_area_label_(AsyncResponseStream *stream, std::string &area) {
+1 -1
View File
@@ -336,7 +336,7 @@ class Sprinkler final : public Component {
void reset_resume();
/// returns a pointer to a valve's name string object; returns nullptr if valve_number is invalid
ESPDEPRECATED("Use valve_log_name() or valve_switch()->get_name_to() instead. Removed in 2027.5.0", "2026.11.0")
ESPDEPRECATED("Use valve_log_name() or control_switch()->get_name_to() instead. Removed in 2027.5.0", "2026.11.0")
const char *valve_name(size_t valve_number);
/// returns a valve's name for a "%s" log argument; returns nullptr if valve_number is invalid
const LogString *valve_log_name(size_t valve_number);
+4 -3
View File
@@ -34,7 +34,6 @@ static constexpr size_t ESPHOME_FRIENDLY_NAME_MAX_LEN = 120;
// Maximum entity name length - keep in sync with NAME_MAX_LENGTH in esphome/config_validation.py
static constexpr size_t ESPHOME_ENTITY_NAME_MAX_LEN = 120;
// Buffer size for EntityBase::get_name_to()
static constexpr size_t ENTITY_NAME_BUF_SIZE = ESPHOME_ENTITY_NAME_MAX_LEN + 1;
// Maximum domain length (longest: "alarm_control_panel" = 19)
@@ -73,16 +72,18 @@ static constexpr uint8_t ENTITY_FIELD_ENTITY_CATEGORY_SHIFT = 26;
// The generic Entity base class that provides an interface common to all Entities.
class EntityBase {
public:
// Remove before 2027.5.0
ESPDEPRECATED("Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0", "2026.11.0")
#ifdef USE_ESP8266
/// The name is in flash here; the first call makes a RAM copy that is kept for the life of the device.
// Remove before 2027.5.0
ESPDEPRECATED("Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0", "2026.11.0")
const StringRef &get_name() const;
/// Get the name of this Entity, copied out of flash into buffer.
StringRef get_name_to(std::span<char, ENTITY_NAME_BUF_SIZE> buffer) const {
return StringRef(buffer.data(), this->write_name_to(buffer.data(), buffer.size()));
}
#else
// Remove before 2027.5.0
ESPDEPRECATED("Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0", "2026.11.0")
const StringRef &get_name() const { return this->name_.ram_ref(); }
const StringRef &get_name_to(std::span<char, ENTITY_NAME_BUF_SIZE> /*buffer*/) const { return this->name_.ram_ref(); }
#endif