1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-19 11:42:20 +01:00

Add text component (#5336)

Co-authored-by: Maurits <maurits@vloop.nl>
Co-authored-by: mauritskorse <mauritskorse@gmail.com>
Co-authored-by: Daniel Dunn <dannydunn@eternityforest.com>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Daniel Dunn
2023-10-25 03:00:32 -06:00
committed by GitHub
parent cf7df9e360
commit e80bd8ed3d
50 changed files with 1545 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ from esphome.const import (
CONF_OPTIMISTIC,
CONF_RESTORE_VALUE,
CONF_STEP,
CONF_SET_ACTION,
)
from .. import template_ns
@@ -18,8 +19,6 @@ TemplateNumber = template_ns.class_(
"TemplateNumber", number.Number, cg.PollingComponent
)
CONF_SET_ACTION = "set_action"
def validate_min_max(config):
if config[CONF_MAX_VALUE] <= config[CONF_MIN_VALUE]:

View File

@@ -9,6 +9,7 @@ from esphome.const import (
CONF_OPTIONS,
CONF_OPTIMISTIC,
CONF_RESTORE_VALUE,
CONF_SET_ACTION,
)
from .. import template_ns
@@ -16,8 +17,6 @@ TemplateSelect = template_ns.class_(
"TemplateSelect", select.Select, cg.PollingComponent
)
CONF_SET_ACTION = "set_action"
def validate(config):
if CONF_LAMBDA in config:

View File

@@ -0,0 +1,92 @@
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text
from esphome.const import (
CONF_INITIAL_VALUE,
CONF_LAMBDA,
CONF_OPTIMISTIC,
CONF_RESTORE_VALUE,
CONF_MAX_LENGTH,
CONF_MIN_LENGTH,
CONF_PATTERN,
CONF_SET_ACTION,
)
from .. import template_ns
TemplateText = template_ns.class_("TemplateText", text.Text, cg.PollingComponent)
TextSaverBase = template_ns.class_("TemplateTextSaverBase")
TextSaverTemplate = template_ns.class_("TextSaver", TextSaverBase)
CONF_MAX_RESTORE_DATA_LENGTH = "max_restore_data_length"
def validate(config):
if CONF_LAMBDA in config:
if config[CONF_OPTIMISTIC]:
raise cv.Invalid("optimistic cannot be used with lambda")
if CONF_INITIAL_VALUE in config:
raise cv.Invalid("initial_value cannot be used with lambda")
if CONF_RESTORE_VALUE in config:
raise cv.Invalid("restore_value cannot be used with lambda")
elif CONF_INITIAL_VALUE not in config:
config[CONF_INITIAL_VALUE] = ""
if not config[CONF_OPTIMISTIC] and CONF_SET_ACTION not in config:
raise cv.Invalid(
"Either optimistic mode must be enabled, or set_action must be set, to handle the text input being set."
)
with cv.prepend_path(CONF_MIN_LENGTH):
if config[CONF_MIN_LENGTH] >= config[CONF_MAX_LENGTH]:
raise cv.Invalid("min_length must be less than max_length")
return config
CONFIG_SCHEMA = cv.All(
text.TEXT_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(TemplateText),
cv.Optional(CONF_MIN_LENGTH, default=0): cv.int_range(min=0, max=255),
cv.Optional(CONF_MAX_LENGTH, default=255): cv.int_range(min=0, max=255),
cv.Optional(CONF_PATTERN): cv.string,
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
cv.Optional(CONF_INITIAL_VALUE): cv.string_strict,
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean,
}
).extend(cv.polling_component_schema("60s")),
validate,
)
async def to_code(config):
var = await text.new_text(
config,
min_length=config[CONF_MIN_LENGTH],
max_length=config[CONF_MAX_LENGTH],
pattern=config.get(CONF_PATTERN),
)
await cg.register_component(var, config)
if CONF_LAMBDA in config:
template_ = await cg.process_lambda(
config[CONF_LAMBDA], [], return_type=cg.optional.template(cg.std_string)
)
cg.add(var.set_template(template_))
else:
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
if initial_value_config := config.get(CONF_INITIAL_VALUE):
cg.add(var.set_initial_value(initial_value_config))
if config[CONF_RESTORE_VALUE]:
args = cg.TemplateArguments(config[CONF_MAX_LENGTH])
saver = TextSaverTemplate.template(args).new()
cg.add(var.set_value_saver(saver))
if CONF_SET_ACTION in config:
await automation.build_automation(
var.get_set_trigger(), [(cg.std_string, "x")], config[CONF_SET_ACTION]
)

View File

@@ -0,0 +1,64 @@
#include "template_text.h"
#include "esphome/core/log.h"
namespace esphome {
namespace template_ {
static const char *const TAG = "template.text";
void TemplateText::setup() {
if (!(this->f_ == nullptr)) {
if (this->f_.has_value())
return;
}
std::string value;
ESP_LOGD(TAG, "Setting up Template Text Input");
value = this->initial_value_;
if (!this->pref_) {
ESP_LOGD(TAG, "State from initial: %s", value.c_str());
} else {
uint32_t key = this->get_object_id_hash();
key += this->traits.get_min_length() << 2;
key += this->traits.get_max_length() << 4;
key += fnv1_hash(this->traits.get_pattern()) << 6;
this->pref_->setup(key, value);
}
if (!value.empty())
this->publish_state(value);
}
void TemplateText::update() {
if (this->f_ == nullptr)
return;
if (!this->f_.has_value())
return;
auto val = (*this->f_)();
if (!val.has_value())
return;
this->publish_state(*val);
}
void TemplateText::control(const std::string &value) {
this->set_trigger_->trigger(value);
if (this->optimistic_)
this->publish_state(value);
if (this->pref_) {
if (!this->pref_->save(value)) {
ESP_LOGW(TAG, "Text value too long to save");
}
}
}
void TemplateText::dump_config() {
LOG_TEXT("", "Template Text Input", this);
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
LOG_UPDATE_INTERVAL(this);
}
} // namespace template_
} // namespace esphome

View File

@@ -0,0 +1,87 @@
#pragma once
#include "esphome/components/text/text.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
namespace esphome {
namespace template_ {
// We keep this separate so we don't have to template and duplicate
// the text input for each different size flash allocation.
class TemplateTextSaverBase {
public:
virtual bool save(const std::string &value) { return true; }
virtual void setup(uint32_t id, std::string &value) {}
protected:
ESPPreferenceObject pref_;
std::string prev_;
};
template<uint8_t SZ> class TextSaver : public TemplateTextSaverBase {
public:
bool save(const std::string &value) override {
int diff = value.compare(this->prev_);
if (diff != 0) {
// If string is bigger than the allocation, do not save it.
// We don't need to waste ram setting prev_value either.
int size = value.size();
if (size <= SZ) {
// Make it into a length prefixed thing
unsigned char temp[SZ + 1];
memcpy(temp + 1, value.c_str(), size);
// SZ should be pre checked at the schema level, it can't go past the char range.
temp[0] = ((unsigned char) size);
this->pref_.save(&temp);
this->prev_.assign(value);
return true;
}
}
return false;
}
// Make the preference object. Fill the provided location with the saved data
// If it is available, else leave it alone
void setup(uint32_t id, std::string &value) override {
this->pref_ = global_preferences->make_preference<uint8_t[SZ + 1]>(id);
char temp[SZ + 1];
bool hasdata = this->pref_.load(&temp);
if (hasdata) {
value.assign(temp + 1, (size_t) temp[0]);
}
this->prev_.assign(value);
}
};
class TemplateText : public text::Text, public PollingComponent {
public:
void set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; }
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
void set_initial_value(const std::string &initial_value) { this->initial_value_ = initial_value; }
void set_value_saver(TemplateTextSaverBase *restore_value_saver) { this->pref_ = restore_value_saver; }
protected:
void control(const std::string &value) override;
bool optimistic_ = false;
std::string initial_value_;
Trigger<std::string> *set_trigger_ = new Trigger<std::string>();
optional<std::function<optional<std::string>()>> f_{nullptr};
TemplateTextSaverBase *pref_ = nullptr;
};
} // namespace template_
} // namespace esphome