ESPHome 2026.3.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"
4
5#include "mqtt_const.h"
6
7#ifdef USE_MQTT
8#ifdef USE_TEXT
9
10namespace esphome::mqtt {
11
12static const char *const TAG = "mqtt.text";
13
14using namespace esphome::text;
15
16// Text mode MQTT strings indexed by TextMode enum (0-1): TEXT, PASSWORD
17PROGMEM_STRING_TABLE(TextMqttModeStrings, "text", "password");
18
20
22 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
23 auto call = this->text_->make_call();
24 call.set_value(state);
25 call.perform();
26 });
27
28 this->text_->add_on_state_callback([this](const std::string &state) { this->publish_state(state); });
29}
30
32 ESP_LOGCONFIG(TAG, "MQTT text '%s':", this->text_->get_name().c_str());
33 LOG_MQTT_COMPONENT(true, true);
34}
35
37const EntityBase *MQTTTextComponent::get_entity() const { return this->text_; }
38
40 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
41 root[MQTT_MODE] = TextMqttModeStrings::get_progmem_str(static_cast<uint8_t>(this->text_->traits.get_mode()),
42 static_cast<uint8_t>(TEXT_MODE_TEXT));
43
44 config.command_topic = true;
45}
47 if (this->text_->has_state()) {
48 return this->publish_state(this->text_->state);
49 } else {
50 return true;
51 }
52}
53bool MQTTTextComponent::publish_state(const std::string &value) {
54 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
55 return this->publish(this->get_state_topic_to_(topic_buf), value.data(), value.size());
56}
57
58} // namespace esphome::mqtt
59
60#endif
61#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.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
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:21
MQTTTextComponent(text::Text *text)
Construct this MQTTTextComponent instance with the provided friendly_name and text.
Definition mqtt_text.cpp:19
bool publish_state(const std::string &value)
Definition mqtt_text.cpp:53
TextCall & set_value(const std::string &value)
Definition text_call.cpp:9
Base-class for all text inputs.
Definition text.h:21
TextCall make_call()
Instantiate a TextCall object to modify this text component's state.
Definition text.h:31
std::string state
Definition text.h:23
void add_on_state_callback(std::function< void(const std::string &)> &&callback)
Definition text.cpp:32
TextTraits traits
Definition text.h:24
TextMode get_mode() const
Definition text_traits.h:30
bool state
Definition fan.h:2
PROGMEM_STRING_TABLE(AlarmControlPanelStateStrings, "DISARMED", "ARMED_HOME", "ARMED_AWAY", "ARMED_NIGHT", "ARMED_VACATION", "ARMED_CUSTOM_BYPASS", "PENDING", "ARMING", "DISARMING", "TRIGGERED", "UNKNOWN")
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.