ESPHome
2026.1.5
Loading...
Searching...
No Matches
esphome
components
text
text.cpp
Go to the documentation of this file.
1
#include "
text.h
"
2
#include "
esphome/core/defines.h
"
3
#include "
esphome/core/controller_registry.h
"
4
#include "
esphome/core/log.h
"
5
#include <cstring>
6
7
namespace
esphome
{
8
namespace
text {
9
10
static
const
char
*
const
TAG
=
"text"
;
11
12
void
Text::publish_state
(
const
std::string &
state
) { this->
publish_state
(state.data(),
state
.size()); }
13
14
void
Text::publish_state
(
const
char
*
state
) { this->
publish_state
(state, strlen(
state
)); }
15
16
void
Text::publish_state
(
const
char
*
state
,
size_t
len
) {
17
this->
set_has_state
(
true
);
18
// Only assign if changed to avoid heap allocation
19
if
(len != this->state.size() || memcmp(
state
, this->state.data(),
len
) != 0) {
20
this->state.assign(
state
,
len
);
21
}
22
if
(this->
traits
.
get_mode
() ==
TEXT_MODE_PASSWORD
) {
23
ESP_LOGD(TAG,
"'%s' >> "
LOG_SECRET(
"'%s'"
), this->
get_name
().c_str(), this->state.
c_str
());
24
}
else
{
25
ESP_LOGD(TAG,
"'%s' >> '%s'"
, this->
get_name
().c_str(), this->state.
c_str
());
26
}
27
this->
state_callback_
.call(this->state);
28
#if defined(USE_TEXT) && defined(USE_CONTROLLER_REGISTRY)
29
ControllerRegistry::notify_text_update
(
this
);
30
#endif
31
}
32
33
void
Text::add_on_state_callback
(std::function<
void
(
const
std::string &)> &&callback) {
34
this->
state_callback_
.add(std::move(callback));
35
}
36
37
}
// namespace text
38
}
// namespace esphome
esphome::ControllerRegistry::notify_text_update
static void notify_text_update(text::Text *obj)
esphome::EntityBase::get_name
const StringRef & get_name() const
Definition
entity_base.cpp:11
esphome::EntityBase::set_has_state
void set_has_state(bool state)
Definition
entity_base.h:123
esphome::StringRef::c_str
constexpr const char * c_str() const
Definition
string_ref.h:73
esphome::text::Text::state_callback_
LazyCallbackManager< void(const std::string &)> state_callback_
Definition
text.h:49
esphome::text::Text::publish_state
void publish_state(const std::string &state)
Definition
text.cpp:12
esphome::text::Text::state
std::string state
Definition
text.h:26
esphome::text::Text::add_on_state_callback
void add_on_state_callback(std::function< void(const std::string &)> &&callback)
Definition
text.cpp:33
esphome::text::Text::traits
TextTraits traits
Definition
text.h:27
esphome::text::TextTraits::get_mode
TextMode get_mode() const
Definition
text_traits.h:31
controller_registry.h
defines.h
state
bool state
Definition
fan.h:0
log.h
esphome::spi::TAG
const char *const TAG
Definition
spi.cpp:7
esphome::text::TEXT_MODE_PASSWORD
@ TEXT_MODE_PASSWORD
Definition
text_traits.h:12
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
esphome::len
std::string size_t len
Definition
helpers.h:595
text.h
Generated by
1.12.0