ESPHome 2026.1.4
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::mqtt {
10
11static const char *const TAG = "mqtt.text";
12
13using namespace esphome::text;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
19 auto call = this->text_->make_call();
20 call.set_value(state);
21 call.perform();
22 });
23
24 this->text_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
25}
26
28 ESP_LOGCONFIG(TAG, "MQTT text '%s':", this->text_->get_name().c_str());
29 LOG_MQTT_COMPONENT(true, true)
30}
31
33const EntityBase *MQTTTextComponent::get_entity() const { return this->text_; }
34
36 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
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 esphome::mqtt
60
61#endif
62#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
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.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void setup() override
Override setup.
Definition mqtt_text.cpp:17
MQTTTextComponent(text::Text *text)
Construct this MQTTTextComponent instance with the provided friendly_name and text.
Definition mqtt_text.cpp:15
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:34
std::string state
Definition text.h:26
void add_on_state_callback(std::function< void(const std::string &)> &&callback)
Definition text.cpp:33
TextTraits traits
Definition text.h:27
TextMode get_mode() const
Definition text_traits.h:31
bool state
Definition fan.h:0
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.