ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mqtt_select.cpp
Go to the documentation of this file.
1#include "mqtt_select.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_SELECT
8
9namespace esphome::mqtt {
10
11static const char *const TAG = "mqtt.select";
12
13using namespace esphome::select;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
19 auto call = this->select_->make_call();
20 call.set_option(state);
21 call.perform();
22 });
23 this->select_->add_on_state_callback([this](size_t index) { this->publish_state(this->select_->option_at(index)); });
24}
25
27 ESP_LOGCONFIG(TAG, "MQTT Select '%s':", this->select_->get_name().c_str());
28 LOG_MQTT_COMPONENT(true, false);
29}
30
32const EntityBase *MQTTSelectComponent::get_entity() const { return this->select_; }
33
35 const auto &traits = select_->traits;
36 // https://www.home-assistant.io/integrations/select.mqtt/
37 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 JsonArray options = root[MQTT_OPTIONS].to<JsonArray>();
39 for (const auto &option : traits.get_options())
40 options.add(option);
41
42 config.command_topic = true;
43}
45 if (this->select_->has_state()) {
46 auto option = this->select_->current_option();
47 return this->publish_state(std::string(option.c_str(), option.size()));
48 } else {
49 return true;
50 }
51}
52bool MQTTSelectComponent::publish_state(const std::string &value) {
53 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
54 return this->publish(this->get_state_topic_to_(topic_buf), value.data(), value.size());
55}
56
57} // namespace esphome::mqtt
58
59#endif
60#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 setup() override
Override setup.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
bool publish_state(const std::string &value)
MQTTSelectComponent(select::Select *select)
Construct this MQTTSelectComponent instance with the provided friendly_name and select.
SelectCall & set_option(const char *option, size_t len)
Base-class for all selects.
Definition select.h:29
const char * option_at(size_t index) const
Return the option value at the provided index offset (as const char* from flash).
Definition select.cpp:85
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
void add_on_state_callback(std::function< void(size_t)> &&callback)
Definition select.cpp:45
SelectTraits traits
Definition select.h:31
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:41
uint8_t options
bool state
Definition fan.h:2
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.