ESPHome 2025.6.3
Loading...
Searching...
No Matches
mqtt_cover.cpp
Go to the documentation of this file.
1#include "mqtt_cover.h"
2#include "esphome/core/log.h"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_COVER
8
9namespace esphome {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.cover";
13
14using namespace esphome::cover;
15
18 auto traits = this->cover_->get_traits();
19 this->cover_->add_on_state_callback([this]() { this->publish_state(); });
20 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
21 auto call = this->cover_->make_call();
22 call.set_command(payload.c_str());
23 call.perform();
24 });
25 if (traits.get_supports_position()) {
26 this->subscribe(this->get_position_command_topic(), [this](const std::string &topic, const std::string &payload) {
27 auto value = parse_number<float>(payload);
28 if (!value.has_value()) {
29 ESP_LOGW(TAG, "Invalid position value: '%s'", payload.c_str());
30 return;
31 }
32 auto call = this->cover_->make_call();
33 call.set_position(*value / 100.0f);
34 call.perform();
35 });
36 }
37 if (traits.get_supports_tilt()) {
38 this->subscribe(this->get_tilt_command_topic(), [this](const std::string &topic, const std::string &payload) {
39 auto value = parse_number<float>(payload);
40 if (!value.has_value()) {
41 ESP_LOGW(TAG, "Invalid tilt value: '%s'", payload.c_str());
42 return;
43 }
44 auto call = this->cover_->make_call();
45 call.set_tilt(*value / 100.0f);
46 call.perform();
47 });
48 }
49}
50
52 ESP_LOGCONFIG(TAG, "MQTT cover '%s':", this->cover_->get_name().c_str());
53 auto traits = this->cover_->get_traits();
54 bool has_command_topic = traits.get_supports_position() || !traits.get_supports_tilt();
55 LOG_MQTT_COMPONENT(true, has_command_topic)
56 if (traits.get_supports_position()) {
57 ESP_LOGCONFIG(TAG,
58 " Position State Topic: '%s'\n"
59 " Position Command Topic: '%s'",
60 this->get_position_state_topic().c_str(), this->get_position_command_topic().c_str());
61 }
62 if (traits.get_supports_tilt()) {
63 ESP_LOGCONFIG(TAG,
64 " Tilt State Topic: '%s'\n"
65 " Tilt Command Topic: '%s'",
66 this->get_tilt_state_topic().c_str(), this->get_tilt_command_topic().c_str());
67 }
68}
70 if (!this->cover_->get_device_class().empty())
72
73 auto traits = this->cover_->get_traits();
74 if (traits.get_is_assumed_state()) {
75 root[MQTT_OPTIMISTIC] = true;
76 }
77 if (traits.get_supports_position()) {
78 root[MQTT_POSITION_TOPIC] = this->get_position_state_topic();
79 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic();
80 }
81 if (traits.get_supports_tilt()) {
82 root[MQTT_TILT_STATUS_TOPIC] = this->get_tilt_state_topic();
83 root[MQTT_TILT_COMMAND_TOPIC] = this->get_tilt_command_topic();
84 }
85 if (traits.get_supports_tilt() && !traits.get_supports_position()) {
86 config.command_topic = false;
87 }
88}
89
90std::string MQTTCoverComponent::component_type() const { return "cover"; }
91const EntityBase *MQTTCoverComponent::get_entity() const { return this->cover_; }
92
95 auto traits = this->cover_->get_traits();
96 bool success = true;
97 if (traits.get_supports_position()) {
98 std::string pos = value_accuracy_to_string(roundf(this->cover_->position * 100), 0);
99 if (!this->publish(this->get_position_state_topic(), pos))
100 success = false;
101 }
102 if (traits.get_supports_tilt()) {
103 std::string pos = value_accuracy_to_string(roundf(this->cover_->tilt * 100), 0);
104 if (!this->publish(this->get_tilt_state_topic(), pos))
105 success = false;
106 }
107 const char *state_s = this->cover_->current_operation == COVER_OPERATION_OPENING ? "opening"
108 : this->cover_->current_operation == COVER_OPERATION_CLOSING ? "closing"
109 : this->cover_->position == COVER_CLOSED ? "closed"
110 : this->cover_->position == COVER_OPEN ? "open"
111 : traits.get_supports_position() ? "open"
112 : "unknown";
113 if (!this->publish(this->get_state_topic_(), state_s))
114 success = false;
115 return success;
116}
117
118} // namespace mqtt
119} // namespace esphome
120
121#endif
122#endif // USE_MQTT
std::string get_device_class()
Get the device class, using the manual override if set.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
Definition cover.cpp:37
void perform()
Perform the cover call.
Definition cover.cpp:75
CoverCall & set_position(float position)
Set the call to a certain target position.
Definition cover.cpp:67
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Definition cover.cpp:71
Base class for all cover devices.
Definition cover.h:111
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:116
void add_on_state_callback(std::function< void()> &&f)
Definition cover.cpp:165
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition cover.cpp:149
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition cover.h:124
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:122
virtual CoverTraits get_traits()=0
bool get_supports_position() const
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.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
MQTTCoverComponent(cover::Cover *cover)
const EntityBase * get_entity() const override
state state bool send_initial_state() override
std::string component_type() const override
const float COVER_CLOSED
Definition cover.cpp:10
const float COVER_OPEN
Definition cover.cpp:9
@ COVER_OPERATION_OPENING
The cover is currently opening.
Definition cover.h:84
@ COVER_OPERATION_CLOSING
The cover is currently closing.
Definition cover.h:86
constexpr const char *const MQTT_POSITION_TOPIC
Definition mqtt_const.h:177
constexpr const char *const MQTT_OPTIMISTIC
Definition mqtt_const.h:126
constexpr const char *const MQTT_TILT_STATUS_TOPIC
Definition mqtt_const.h:261
constexpr const char *const MQTT_TILT_COMMAND_TOPIC
Definition mqtt_const.h:254
constexpr const char *const MQTT_DEVICE_CLASS
Definition mqtt_const.h:58
constexpr const char *const MQTT_SET_POSITION_TOPIC
Definition mqtt_const.h:205
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
Definition helpers.cpp:435
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:314
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.