ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
mqtt_text.cpp
Go to the documentation of this file.
1#include "mqtt_text.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_TEXT
8
9namespace esphome {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.text";
13
14using namespace esphome::text;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
20 auto call = this->text_->make_call();
21 call.set_value(state);
22 call.perform();
23 });
24
25 this->text_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
26}
27
29 ESP_LOGCONFIG(TAG, "MQTT text '%s':", this->text_->get_name().c_str());
30 LOG_MQTT_COMPONENT(true, true)
31}
32
33std::string MQTTTextComponent::component_type() const { return "text"; }
34const EntityBase *MQTTTextComponent::get_entity() const { return this->text_; }
35
37 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 switch (this->text_->traits.get_mode()) {
39 case TEXT_MODE_TEXT:
40 root[MQTT_MODE] = "text";
41 break;
43 root[MQTT_MODE] = "password";
44 break;
45 }
46
47 config.command_topic = true;
48}
50 if (this->text_->has_state()) {
51 return this->publish_state(this->text_->state);
52 } else {
53 return true;
54 }
55}
56bool MQTTTextComponent::publish_state(const std::string &value) {
57 return this->publish(this->get_state_topic_(), value);
58}
59
60} // namespace mqtt
61} // namespace esphome
62
63#endif
64#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
Definition entity_base.h:78
constexpr const char * c_str() const
Definition string_ref.h:69
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
std::string component_type() const override
Override for MQTTComponent, returns "text".
Definition mqtt_text.cpp:33
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition mqtt_text.cpp:36
void setup() override
Override setup.
Definition mqtt_text.cpp:18
MQTTTextComponent(text::Text *text)
Construct this MQTTTextComponent instance with the provided friendly_name and text.
Definition mqtt_text.cpp:16
const EntityBase * get_entity() const override
Definition mqtt_text.cpp:34
bool publish_state(const std::string &value)
Definition mqtt_text.cpp:56
TextCall & set_value(const std::string &value)
Definition text_call.cpp:10
Base-class for all text inputs.
Definition text.h:24
TextCall make_call()
Instantiate a TextCall object to modify this text component's state.
Definition text.h:32
void add_on_state_callback(std::function< void(std::string)> &&callback)
Definition text.cpp:21
std::string state
Definition text.h:26
TextTraits traits
Definition text.h:27
TextMode get_mode() const
Definition text_traits.h:31
bool state
Definition fan.h:0
constexpr const char *const MQTT_MODE
Definition mqtt_const.h:116
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.