ESPHome 2025.12.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 });
25 [this](const std::string &state, size_t index) { this->publish_state(this->select_->option_at(index)); });
26}
27
29 ESP_LOGCONFIG(TAG, "MQTT Select '%s':", this->select_->get_name().c_str());
30 LOG_MQTT_COMPONENT(true, false)
31}
32
33std::string MQTTSelectComponent::component_type() const { return "select"; }
34const EntityBase *MQTTSelectComponent::get_entity() const { return this->select_; }
35
37 const auto &traits = select_->traits;
38 // https://www.home-assistant.io/integrations/select.mqtt/
39 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
40 JsonArray options = root[MQTT_OPTIONS].to<JsonArray>();
41 for (const auto &option : traits.get_options())
42 options.add(option);
43
44 config.command_topic = true;
45}
47 if (this->select_->has_state()) {
48 return this->publish_state(this->select_->current_option());
49 } else {
50 return true;
51 }
52}
53bool MQTTSelectComponent::publish_state(const std::string &value) {
54 return this->publish(this->get_state_topic_(), value);
55}
56
57} // namespace mqtt
58} // namespace esphome
59
60#endif
61#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
Definition entity_base.h:90
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:45
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:87
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
const char * current_option() const
Return the currently selected option (as const char* from flash).
Definition select.cpp:43
SelectTraits traits
Definition select.h:33
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.