ESPHome 2026.11.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
12ESPHOME_LOG_TAG(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->command_topic_source_(), [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
31void MQTTTextComponent::dump_config() { LOG_MQTT_COMPONENT(true, true); }
32
34const EntityBase *MQTTTextComponent::get_entity() const { return this->text_; }
35
37 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 root[MQTT_MODE] = TextMqttModeStrings::get_progmem_str(static_cast<uint8_t>(this->text_->traits.get_mode()),
39 static_cast<uint8_t>(TEXT_MODE_TEXT));
40
41 config.command_topic = true;
42}
44 if (this->text_->has_state()) {
45 return this->publish_state(this->text_->state);
46 } else {
47 return true;
48 }
49}
50bool MQTTTextComponent::publish_state(const std::string &value) {
51 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
52 return this->publish(this->get_state_topic_to_(topic_buf), value.data(), value.size());
53}
54
55} // namespace esphome::mqtt
56
57#endif
58#endif // USE_MQTT
bool has_state() const
void subscribe(T &&topic, F &&callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
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).
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:50
TextCall & set_value(const std::string &value)
Definition text_call.cpp:9
Base-class for all text inputs.
Definition text.h:20
void add_on_state_callback(F &&callback)
Definition text.h:32
TextCall make_call()
Instantiate a TextCall object to modify this text component's state.
Definition text.h:30
std::string state
Definition text.h:22
TextTraits traits
Definition text.h:23
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")
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
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.