Merge branch 'text_pattern' into integration

This commit is contained in:
J. Nick Koston
2025-12-07 13:21:55 -06:00
6 changed files with 12 additions and 8 deletions
@@ -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"("/>)");
@@ -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