ESPHome 2025.5.0
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 JsonArray options = root.createNestedArray(MQTT_OPTIONS);
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 return this->publish_state(this->select_->state);
47 } else {
48 return true;
49 }
50}
51bool MQTTSelectComponent::publish_state(const std::string &value) {
52 return this->publish(this->get_state_topic_(), value);
53}
54
55} // namespace mqtt
56} // namespace esphome
57
58#endif
59#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
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:42
std::string state
Definition select.h:33
SelectTraits traits
Definition select.h:34
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition select.h:39
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.