Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-12-07 13:22:04 -06:00
9 changed files with 12 additions and 32 deletions
-1
View File
@@ -19,7 +19,6 @@ from esphome.cpp_generator import ( # noqa: F401
RawExpression,
RawStatement,
Statement,
StringRefLiteral,
StructInitializer,
TemplateArguments,
add,
@@ -16,7 +16,7 @@ void TemplateText::setup() {
uint32_t key = this->get_preference_hash();
key += this->traits.get_min_length() << 2;
key += this->traits.get_max_length() << 4;
key += fnv1_hash(this->traits.get_pattern()) << 6;
key += fnv1_hash(this->traits.get_pattern_c_str()) << 6;
this->pref_->setup(key, value);
}
if (!value.empty())
+5 -5
View File
@@ -1,8 +1,7 @@
#pragma once
#include <utility>
#include <string>
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
namespace esphome {
@@ -22,8 +21,9 @@ class TextTraits {
int get_max_length() const { return this->max_length_; }
// Set/get the pattern.
void set_pattern(std::string pattern) { this->pattern_ = std::move(pattern); }
std::string get_pattern() const { return this->pattern_; }
void set_pattern(const char *pattern) { this->pattern_ = pattern; }
std::string get_pattern() const { return std::string(this->pattern_); }
const char *get_pattern_c_str() const { return this->pattern_; }
StringRef get_pattern_ref() const { return StringRef(this->pattern_); }
// Set/get the frontend mode.
@@ -33,7 +33,7 @@ class TextTraits {
protected:
int min_length_;
int max_length_;
std::string pattern_;
const char *pattern_{""};
TextMode mode_{TEXT_MODE_TEXT};
};
+1 -1
View File
@@ -1211,7 +1211,7 @@ std::string WebServer::text_json(text::Text *obj, const std::string &value, Json
set_json_icon_state_value(root, obj, "text", state, value, start_config);
root[ESPHOME_F("min_length")] = obj->traits.get_min_length();
root[ESPHOME_F("max_length")] = obj->traits.get_max_length();
root[ESPHOME_F("pattern")] = obj->traits.get_pattern();
root[ESPHOME_F("pattern")] = obj->traits.get_pattern_c_str();
if (start_config == DETAIL_ALL) {
root[ESPHOME_F("mode")] = (int) obj->traits.get_mode();
this->add_sorting_info_(root, obj);
@@ -142,7 +142,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
stream.print(R"(" maxlength=")");
stream.print(text->traits.get_max_length());
stream.print(R"(" pattern=")");
stream.print(text->traits.get_pattern().c_str());
stream.print(text->traits.get_pattern_c_str());
stream.print(R"(" value=")");
stream.print(text->state.c_str());
stream.print(R"("/>)");
-17
View File
@@ -243,23 +243,6 @@ class LogStringLiteral(Literal):
return f"LOG_STR({cpp_string_escape(self.string)})"
class StringRefLiteral(Literal):
"""A StringRef literal using from_lit() for compile-time string references.
Uses StringRef::from_lit() which stores pointer + length (8 bytes) instead of
std::string (24-32 bytes + heap allocation). The string data stays in flash.
"""
__slots__ = ("string",)
def __init__(self, string: str) -> None:
super().__init__()
self.string = string
def __str__(self) -> str:
return f"StringRef::from_lit({cpp_string_escape(self.string)})"
class IntLiteral(Literal):
__slots__ = ("i",)
@@ -143,6 +143,7 @@ text:
mode: text
min_length: 0
max_length: 255
pattern: "[A-Za-z0-9 ]+"
initial_value: "Initial value"
update_interval: 5.0s
@@ -141,6 +141,9 @@ async def test_api_message_size_batching(
assert text_input.max_length == 255, (
f"Expected max_length 255, got {text_input.max_length}"
)
assert text_input.pattern == "[A-Za-z0-9 ]+", (
f"Expected pattern '[A-Za-z0-9 ]+', got '{text_input.pattern}'"
)
# Verify total entity count - messages of various sizes were batched successfully
# We have: 3 selects + 3 text sensors + 1 text input + 1 number = 8 total
-6
View File
@@ -248,12 +248,6 @@ class TestLiterals:
(cg.FloatLiteral(4.2), "4.2f"),
(cg.FloatLiteral(1.23456789), "1.23456789f"),
(cg.FloatLiteral(math.nan), "NAN"),
(cg.StringRefLiteral("foo"), 'StringRef::from_lit("foo")'),
(cg.StringRefLiteral(""), 'StringRef::from_lit("")'),
(
cg.StringRefLiteral('with "quotes"'),
'StringRef::from_lit("with \\042quotes\\042")',
),
),
)
def test_str__simple(self, target: cg.Literal, expected: str):