ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
text.cpp
Go to the documentation of this file.
1#include "text.h"
4#include "esphome/core/log.h"
5#include <cstring>
6
7namespace esphome::text {
8
9static const char *const TAG = "text";
10
11void Text::publish_state(const std::string &state) { this->publish_state(state.data(), state.size()); }
12
13void Text::publish_state(const char *state) { this->publish_state(state, strlen(state)); }
14
15void Text::publish_state(const char *state, size_t len) {
16 this->set_has_state(true);
17 // Only assign if changed to avoid heap allocation
18 if (len != this->state.size() || memcmp(state, this->state.data(), len) != 0) {
19 this->state.assign(state, len);
20 }
21 if (this->traits.get_mode() == TEXT_MODE_PASSWORD) {
22 ESP_LOGV(TAG, "'%s' >> " LOG_SECRET("'%s'"), this->get_name().c_str(), this->state.c_str());
23 } else {
24 ESP_LOGV(TAG, "'%s' >> '%s'", this->get_name().c_str(), this->state.c_str());
25 }
26 this->state_callback_.call(this->state);
27#if defined(USE_TEXT) && defined(USE_CONTROLLER_REGISTRY)
28 ControllerRegistry::notify_text_update(this);
29#endif
30}
31
32} // namespace esphome::text
const StringRef & get_name() const
Definition entity_base.h:71
void set_has_state(bool state)
constexpr const char * c_str() const
Definition string_ref.h:73
LazyCallbackManager< void(const std::string &)> state_callback_
Definition text.h:48
void publish_state(const std::string &state)
Definition text.cpp:11
std::string state
Definition text.h:23
TextTraits traits
Definition text.h:24
TextMode get_mode() const
Definition text_traits.h:30
bool state
Definition fan.h:2
const char *const TAG
Definition spi.cpp:7
std::string size_t len
Definition helpers.h:1045