ESPHome  2024.11.1
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 
5 namespace esphome {
6 namespace text {
7 
8 static const char *const TAG = "text";
9 
10 TextCall &TextCall::set_value(const std::string &value) {
11  this->value_ = value;
12  return *this;
13 }
14 
16  const auto *name = this->parent_->get_name().c_str();
17 
18  if (!this->value_.has_value()) {
19  ESP_LOGW(TAG, "'%s' - No value set for TextCall", name);
20  return;
21  }
22 
23  int sz = this->value_.value().size();
24 
25  if (sz > this->parent_->traits.get_max_length()) {
26  ESP_LOGW(TAG, "'%s' - Value set for TextCall is too long", name);
27  this->value_.reset();
28  return;
29  }
30 
31  if (sz < this->parent_->traits.get_min_length()) {
32  ESP_LOGW(TAG, "'%s' - Value set for TextCall is too short", name);
33  this->value_.reset();
34  return;
35  }
36 }
37 
39  this->validate_();
40  if (!this->value_.has_value()) {
41  ESP_LOGW(TAG, "'%s' - No value set for TextCall", this->parent_->get_name().c_str());
42  return;
43  }
44  std::string target_value = this->value_.value();
45 
46  if (this->parent_->traits.get_mode() == TEXT_MODE_PASSWORD) {
47  ESP_LOGD(TAG, "'%s' - Setting password value: " LOG_SECRET("'%s'"), this->parent_->get_name().c_str(),
48  target_value.c_str());
49  } else {
50  ESP_LOGD(TAG, "'%s' - Setting text value: %s", this->parent_->get_name().c_str(), target_value.c_str());
51  }
52  this->parent_->control(target_value);
53 }
54 
55 } // namespace text
56 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
const char * name
Definition: stm32flash.h:78
TextMode get_mode() const
Definition: text_traits.h:29
TextTraits traits
Definition: text.h:27
TextCall & set_value(const std::string &value)
Definition: text_call.cpp:10
Text *const parent_
Definition: text_call.h:19
bool has_value() const
Definition: optional.h:87
int get_max_length() const
Definition: text_traits.h:21
const char *const TAG
Definition: spi.cpp:8
constexpr const char * c_str() const
Definition: string_ref.h:68
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...
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
int get_min_length() const
Definition: text_traits.h:19
const StringRef & get_name() const
Definition: entity_base.cpp:10
optional< std::string > value_
Definition: text_call.h:20