ESPHome  2024.11.0
mqtt_switch.cpp
Go to the documentation of this file.
1 #include "mqtt_switch.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_SWITCH
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.switch";
13 
14 using namespace esphome::switch_;
15 
17 
19  this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20  switch (parse_on_off(payload.c_str())) {
21  case PARSE_ON:
22  this->switch_->turn_on();
23  break;
24  case PARSE_OFF:
25  this->switch_->turn_off();
26  break;
27  case PARSE_TOGGLE:
28  this->switch_->toggle();
29  break;
30  case PARSE_NONE:
31  default:
32  ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name().c_str(), payload.c_str());
33  this->status_momentary_warning("state", 5000);
34  break;
35  }
36  });
38  [this](bool enabled) { this->defer("send", [this, enabled]() { this->publish_state(enabled); }); });
39 }
41  ESP_LOGCONFIG(TAG, "MQTT Switch '%s': ", this->switch_->get_name().c_str());
42  LOG_MQTT_COMPONENT(true, true);
43 }
44 
45 std::string MQTTSwitchComponent::component_type() const { return "switch"; }
46 const EntityBase *MQTTSwitchComponent::get_entity() const { return this->switch_; }
48  if (this->switch_->assumed_state())
49  root[MQTT_OPTIMISTIC] = true;
50 }
52 
54  const char *state_s = state ? "ON" : "OFF";
55  return this->publish(this->get_state_topic_(), state_s);
56 }
57 
58 } // namespace mqtt
59 } // namespace esphome
60 
61 #endif
62 #endif // USE_MQTT
Base class for all switches.
Definition: switch.h:39
MQTTSwitchComponent(switch_::Switch *a_switch)
Definition: mqtt_switch.cpp:16
void status_momentary_warning(const std::string &name, uint32_t length=5000)
Definition: component.cpp:178
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition: component.cpp:130
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:58
constexpr const char *const MQTT_OPTIMISTIC
Definition: mqtt_const.h:124
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition: helpers.cpp:400
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition: switch.cpp:60
Simple Helper struct used for Home Assistant MQTT send_discovery().
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
void toggle()
Toggle this switch.
Definition: switch.cpp:19
constexpr const char * c_str() const
Definition: string_ref.h:68
std::string component_type() const override
"switch" component type.
Definition: mqtt_switch.cpp:45
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const EntityBase * get_entity() const override
Definition: mqtt_switch.cpp:46
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.
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
const StringRef & get_name() const
Definition: entity_base.cpp:10
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_switch.cpp:47
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:15