ESPHome 2025.5.0
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 switch (this->text_->traits.get_mode()) {
38 case TEXT_MODE_TEXT:
39 root[MQTT_MODE] = "text";
40 break;
42 root[MQTT_MODE] = "password";
43 break;
44 }
45
46 config.command_topic = true;
47}
49 if (this->text_->has_state()) {
50 return this->publish_state(this->text_->state);
51 } else {
52 return true;
53 }
54}
55bool MQTTTextComponent::publish_state(const std::string &value) {
56 return this->publish(this->get_state_topic_(), value);
57}
58
59} // namespace mqtt
60} // namespace esphome
61
62#endif
63#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
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:55
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:35
void add_on_state_callback(std::function< void(std::string)> &&callback)
Definition text.cpp:21
bool has_state() const
Return whether this text input has gotten a full state yet.
Definition text.h:32
std::string state
Definition text.h:26
TextTraits traits
Definition text.h:27
TextMode get_mode() const
Definition text_traits.h:29
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.