ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
text_call.cpp
Go to the documentation of this file.
1#include "text_call.h"
2#include "esphome/core/log.h"
3#include "text.h"
4
5namespace esphome::text {
6
7static const char *const TAG = "text";
8
9TextCall &TextCall::set_value(const std::string &value) {
10 this->value_ = value;
11 return *this;
12}
13
14TextCall &TextCall::set_value(const char *value, size_t len) {
15 this->value_ = std::string(value, len);
16 return *this;
17}
18
20 const auto *name = this->parent_->get_name().c_str();
21
22 if (!this->value_.has_value()) {
23 ESP_LOGW(TAG, "'%s' - No value set for TextCall", name);
24 return;
25 }
26
27 int sz = this->value_.value().size();
28
29 if (sz > this->parent_->traits.get_max_length()) {
30 ESP_LOGW(TAG, "'%s' - Value set for TextCall is too long", name);
31 this->value_.reset();
32 return;
33 }
34
35 if (sz < this->parent_->traits.get_min_length()) {
36 ESP_LOGW(TAG, "'%s' - Value set for TextCall is too short", name);
37 this->value_.reset();
38 return;
39 }
40}
41
43 this->validate_();
44 if (!this->value_.has_value()) {
45 ESP_LOGW(TAG, "'%s' - No value set for TextCall", this->parent_->get_name().c_str());
46 return;
47 }
48 std::string target_value = this->value_.value();
49
50 if (this->parent_->traits.get_mode() == TEXT_MODE_PASSWORD) {
51 ESP_LOGD(TAG, "'%s' - Setting password value: " LOG_SECRET("'%s'"), this->parent_->get_name().c_str(),
52 target_value.c_str());
53 } else {
54 ESP_LOGD(TAG, "'%s' - Setting text value: %s", this->parent_->get_name().c_str(), target_value.c_str());
55 }
56 this->parent_->control(target_value);
57}
58
59} // namespace esphome::text
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
TextCall & set_value(const std::string &value)
Definition text_call.cpp:9
optional< std::string > value_
Definition text_call.h:20
virtual void control(const std::string &value)=0
Set the value of the text input, this is a virtual method that each text input integration must imple...
TextTraits traits
Definition text.h:24
TextMode get_mode() const
Definition text_traits.h:30
const char *const TAG
Definition spi.cpp:7
std::string size_t len
Definition helpers.h:817