ESPHome 2026.11.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
11ESPHOME_LOG_TAG(TAG, "mqtt.select");
12
13using namespace esphome::select;
14
16
18 this->subscribe(this->command_topic_source_(), [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
26void MQTTSelectComponent::dump_config() { LOG_MQTT_COMPONENT(true, false); }
27
29const EntityBase *MQTTSelectComponent::get_entity() const { return this->select_; }
30
32 const auto &traits = select_->traits;
33 // https://www.home-assistant.io/integrations/select.mqtt/
34 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
35 JsonArray options = root[MQTT_OPTIONS].to<JsonArray>();
36 for (const auto &option : traits.get_options())
37 options.add(option);
38
39 config.command_topic = true;
40}
42 if (this->select_->has_state()) {
43 auto option = this->select_->current_option();
44 return this->publish_state(std::string(option.c_str(), option.size()));
45 } else {
46 return true;
47 }
48}
49bool MQTTSelectComponent::publish_state(const std::string &value) {
50 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
51 return this->publish(this->get_state_topic_to_(topic_buf), value.data(), value.size());
52}
53
54} // namespace esphome::mqtt
55
56#endif
57#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 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:28
void add_on_state_callback(F &&callback)
Definition select.h:73
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:60
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:47
SelectTraits traits
Definition select.h:30
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.h:42
uint8_t options
bool state
Definition fan.h:2
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.