ESPHome  2024.10.2
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 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.text";
13 
14 using 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 
33 std::string MQTTTextComponent::component_type() const { return "text"; }
34 const 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;
41  case TEXT_MODE_PASSWORD:
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 }
55 bool 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
TextMode get_mode() const
Definition: text_traits.h:29
std::string state
Definition: text.h:26
TextTraits traits
Definition: text.h:27
MQTTTextComponent(text::Text *text)
Construct this MQTTTextComponent instance with the provided friendly_name and text.
Definition: mqtt_text.cpp:16
void setup() override
Override setup.
Definition: mqtt_text.cpp:18
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
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
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
Definition: mqtt_text.cpp:36
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
bool send_initial_state() override
Definition: mqtt_text.cpp:48
Simple Helper struct used for Home Assistant MQTT send_discovery().
constexpr const char *const MQTT_MODE
Definition: mqtt_const.h:113
constexpr const char * c_str() const
Definition: string_ref.h:68
const EntityBase * get_entity() const override
Definition: mqtt_text.cpp:34
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string component_type() const override
Override for MQTTComponent, returns "text".
Definition: mqtt_text.cpp:33
bool publish_state(const std::string &value)
Definition: mqtt_text.cpp:55
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.
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34