ESPHome 2025.9.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 {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.select";
13
14using namespace esphome::select;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &state) {
20 auto call = this->select_->make_call();
21 call.set_option(state);
22 call.perform();
23 });
24 this->select_->add_on_state_callback([this](const std::string &state, size_t index) { this->publish_state(state); });
25}
26
28 ESP_LOGCONFIG(TAG, "MQTT Select '%s':", this->select_->get_name().c_str());
29 LOG_MQTT_COMPONENT(true, false)
30}
31
32std::string MQTTSelectComponent::component_type() const { return "select"; }
33const EntityBase *MQTTSelectComponent::get_entity() const { return this->select_; }
34
36 const auto &traits = select_->traits;
37 // https://www.home-assistant.io/integrations/select.mqtt/
38 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
39 JsonArray options = root[MQTT_OPTIONS].to<JsonArray>();
40 for (const auto &option : traits.get_options())
41 options.add(option);
42
43 config.command_topic = true;
44}
46 if (this->select_->has_state()) {
47 return this->publish_state(this->select_->state);
48 } else {
49 return true;
50 }
51}
52bool MQTTSelectComponent::publish_state(const std::string &value) {
53 return this->publish(this->get_state_topic_(), value);
54}
55
56} // namespace mqtt
57} // namespace esphome
58
59#endif
60#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
Definition entity_base.h:78
constexpr const char * c_str() const
Definition string_ref.h:69
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
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.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
const EntityBase * get_entity() const override
void setup() override
Override setup.
std::string component_type() const override
Override for MQTTComponent, returns "select".
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 std::string &option)
Base-class for all selects.
Definition select.h:31
void add_on_state_callback(std::function< void(std::string, size_t)> &&callback)
Definition select.cpp:22
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:39
std::string state
Definition select.h:33
SelectTraits traits
Definition select.h:34
uint8_t options
bool state
Definition fan.h:0
constexpr const char *const MQTT_OPTIONS
Definition mqtt_const.h:127
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.