From 935041204e090ce4d25209365f804790e9e3733b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 6 Feb 2025 12:58:35 +0100 Subject: [PATCH] add text component --- src/ha.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ha.h b/src/ha.h index 3fd17a4..3fc75f5 100644 --- a/src/ha.h +++ b/src/ha.h @@ -258,6 +258,27 @@ namespace Ha { } }; + struct Text : Component, StatefulCommand { + unsigned int min = 1; + unsigned int max = 100; + const char* pattern = nullptr; + + Text(const char* name, const char* id, onMessage f) : Component(id, name, "text"), StatefulCommand(this, f) {} + + void updateState(unsigned int value) { + State::updateState(to_string(value).c_str()); + } + + void buildConfig(JsonDocument& jsonDoc) override { + Component::buildConfig(jsonDoc); + StatefulCommand::buildConfig(jsonDoc); + jsonDoc["min"] = min; + jsonDoc["max"] = max; + jsonDoc["platform"] = "text"; + if (pattern) jsonDoc["pattern"] = pattern; + } + }; + struct Sensor : Component, State { const char* unitMeasure = nullptr; const char* valueTemplate = nullptr; @@ -391,6 +412,11 @@ namespace Ha { return *this; } + Builder& withPattern(const char* value) { + cmp->pattern = value; + return *this; + } + Builder& withJsonAttributes(const char* value) { cmp->jsonAttributesTemplate = value; return *this;