ESPHome  2024.11.0
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 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.select";
13 
14 using namespace esphome::select;
15 
16 MQTTSelectComponent::MQTTSelectComponent(Select *select) : select_(select) {}
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 
32 std::string MQTTSelectComponent::component_type() const { return "select"; }
33 const 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 }
51 bool 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
MQTTSelectComponent(select::Select *select)
Construct this MQTTSelectComponent instance with the provided friendly_name and select.
Definition: mqtt_select.cpp:16
SelectTraits traits
Definition: select.h:34
constexpr const char *const MQTT_OPTIONS
Definition: mqtt_const.h:125
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
Simple Helper struct used for Home Assistant MQTT send_discovery().
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition: select.h:42
uint8_t options
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_select.cpp:35
constexpr const char * c_str() const
Definition: string_ref.h:68
void setup() override
Override setup.
Definition: mqtt_select.cpp:18
std::string state
Definition: select.h:33
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition: select.h:39
const EntityBase * get_entity() const override
Definition: mqtt_select.cpp:33
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string component_type() const override
Override for MQTTComponent, returns "select".
Definition: mqtt_select.cpp:32
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void add_on_state_callback(std::function< void(std::string, size_t)> &&callback)
Definition: select.cpp:22
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool publish_state(const std::string &value)
Definition: mqtt_select.cpp:51
bool state
Definition: fan.h:34