12static const char *
const TAG =
"mqtt.cover";
18 return ESPHOME_F(
"opening");
20 return ESPHOME_F(
"closing");
22 return ESPHOME_F(
"closed");
24 return ESPHOME_F(
"open");
25 if (supports_position)
26 return ESPHOME_F(
"open");
27 return ESPHOME_F(
"unknown");
39 if (traits.get_supports_position()) {
40 this->
subscribe(this->get_position_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
42 if (!value.has_value()) {
43 ESP_LOGW(TAG,
"Invalid position value: '%s'", payload.c_str());
51 if (traits.get_supports_tilt()) {
52 this->
subscribe(this->get_tilt_command_topic(), [
this](
const std::string &topic,
const std::string &payload) {
54 if (!value.has_value()) {
55 ESP_LOGW(TAG,
"Invalid tilt value: '%s'", payload.c_str());
69 LOG_MQTT_COMPONENT(
true, has_command_topic);
70 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
71#ifdef USE_MQTT_COVER_JSON
73 ESP_LOGCONFIG(TAG,
" JSON State Payload: YES");
76 if (traits.get_supports_position()) {
77 ESP_LOGCONFIG(TAG,
" Position State Topic: '%s'", this->get_position_state_topic_to(topic_buf).c_str());
79 if (traits.get_supports_tilt()) {
80 ESP_LOGCONFIG(TAG,
" Tilt State Topic: '%s'", this->get_tilt_state_topic_to(topic_buf).c_str());
82#ifdef USE_MQTT_COVER_JSON
85 if (traits.get_supports_position()) {
86 ESP_LOGCONFIG(TAG,
" Position Command Topic: '%s'", this->get_position_command_topic_to(topic_buf).c_str());
88 if (traits.get_supports_tilt()) {
89 ESP_LOGCONFIG(TAG,
" Tilt Command Topic: '%s'", this->get_tilt_command_topic_to(topic_buf).c_str());
95 if (!device_class.empty()) {
96 root[MQTT_DEVICE_CLASS] = device_class;
101 if (traits.get_is_assumed_state()) {
102 root[MQTT_OPTIMISTIC] =
true;
104 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
105#ifdef USE_MQTT_COVER_JSON
108 root[MQTT_VALUE_TEMPLATE] = ESPHOME_F(
"{{ value_json.state }}");
109 if (traits.get_supports_position()) {
111 root[MQTT_POSITION_TEMPLATE] = ESPHOME_F(
"{{ value_json.position }}");
112 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic_to(topic_buf);
114 if (traits.get_supports_tilt()) {
116 root[MQTT_TILT_STATUS_TEMPLATE] = ESPHOME_F(
"{{ value_json.tilt }}");
117 root[MQTT_TILT_COMMAND_TOPIC] = this->get_tilt_command_topic_to(topic_buf);
123 if (traits.get_supports_position()) {
124 root[MQTT_POSITION_TOPIC] = this->get_position_state_topic_to(topic_buf);
125 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic_to(topic_buf);
127 if (traits.get_supports_tilt()) {
128 root[MQTT_TILT_STATUS_TOPIC] = this->get_tilt_state_topic_to(topic_buf);
129 root[MQTT_TILT_COMMAND_TOPIC] = this->get_tilt_command_topic_to(topic_buf);
132 if (traits.get_supports_tilt() && !traits.get_supports_position()) {
143 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
144#ifdef USE_MQTT_COVER_JSON
149 traits.get_supports_position());
150 if (traits.get_supports_position()) {
151 root[ESPHOME_F(
"position")] =
static_cast<int>(roundf(this->
cover_->
position * 100));
153 if (traits.get_supports_tilt()) {
154 root[ESPHOME_F(
"tilt")] =
static_cast<int>(roundf(this->
cover_->
tilt * 100));
161 if (traits.get_supports_position()) {
162 char pos[VALUE_ACCURACY_MAX_LEN];
164 if (!this->
publish(this->get_position_state_topic_to(topic_buf),
pos,
len))
167 if (traits.get_supports_tilt()) {
168 char pos[VALUE_ACCURACY_MAX_LEN];
170 if (!this->
publish(this->get_tilt_state_topic_to(topic_buf),
pos,
len))
175 traits.get_supports_position())))
StringRef get_device_class_ref() const
Get the device class as StringRef.
const StringRef & get_name() const
constexpr const char * c_str() const
CoverCall & set_command(const char *command)
Set the command as a string, "STOP", "OPEN", "CLOSE", "TOGGLE".
void perform()
Perform the cover call.
CoverCall & set_position(float position)
Set the call to a certain target position.
CoverCall & set_tilt(float tilt)
Set the call to a certain target tilt.
Base class for all cover devices.
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
void add_on_state_callback(std::function< void()> &&f)
CoverCall make_call()
Construct a new cover call used to control the cover.
float tilt
The current tilt value of the cover from 0.0 to 1.0.
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
virtual CoverTraits get_traits()=0
bool get_supports_position() const
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
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
state state bool send_initial_state() override
MQTTCoverComponent(cover::Cover *cover)
void dump_config() override
CoverOperation
Enum encoding the current operation of a cover.
@ COVER_OPERATION_OPENING
The cover is currently opening.
@ COVER_OPERATION_CLOSING
The cover is currently closing.
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
const __FlashStringHelper * ProgmemStr
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.