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
+56
View File
@@ -0,0 +1,56 @@
#include "text_call.h"
#include "esphome/core/log.h"
#include "text.h"
namespace esphome {
namespace text {
static const char *const TAG = "text";
TextCall &TextCall::set_value(const std::string &value) {
this->value_ = value;
return *this;
}
void TextCall::validate_() {
const auto *name = this->parent_->get_name().c_str();
if (!this->value_.has_value()) {
ESP_LOGW(TAG, "'%s' - No value set for TextCall", name);
return;
}
int sz = this->value_.value().size();
if (sz > this->parent_->traits.get_max_length()) {
ESP_LOGW(TAG, "'%s' - Value set for TextCall is too long", name);
this->value_.reset();
return;
}
if (sz < this->parent_->traits.get_min_length()) {
ESP_LOGW(TAG, "'%s' - Value set for TextCall is too short", name);
this->value_.reset();
return;
}
}
void TextCall::perform() {
this->validate_();
if (!this->value_.has_value()) {
ESP_LOGW(TAG, "'%s' - No value set for TextCall", this->parent_->get_name().c_str());
return;
}
std::string target_value = this->value_.value();
if (this->parent_->traits.get_mode() == TEXT_MODE_PASSWORD) {
ESP_LOGD(TAG, "'%s' - Setting password value: " LOG_SECRET("'%s'"), this->parent_->get_name().c_str(),
target_value.c_str());
} else {
ESP_LOGD(TAG, "'%s' - Setting text value: %s", this->parent_->get_name().c_str(), target_value.c_str());
}
this->parent_->control(target_value);
}
} // namespace text
} // namespace esphome