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_source(),
41 [
this](
const std::string &topic,
const std::string &payload) {
43 if (!value.has_value()) {
44 ESP_LOGW(TAG,
"Invalid position value: '%s'", payload.c_str());
52 if (traits.get_supports_tilt()) {
53 this->
subscribe(this->get_tilt_command_topic_source(),
54 [
this](
const std::string &topic,
const std::string &payload) {
55 if (ESPHOME_strcasecmp_P(payload.c_str(), ESPHOME_PSTR(
"STOP")) == 0) {
60 if (!value.has_value()) {
61 ESP_LOGW(TAG,
"Invalid tilt value: '%s'", payload.c_str());
74 LOG_MQTT_COMPONENT(
true, has_command_topic);
75 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
76#ifdef USE_MQTT_COVER_JSON
78 ESP_LOGCONFIG(TAG,
" JSON State Payload: YES");
81 if (traits.get_supports_position()) {
82 ESP_LOGCONFIG(TAG,
" Position State Topic: '%s'", this->get_position_state_topic_to(topic_buf).c_str());
84 if (traits.get_supports_tilt()) {
85 ESP_LOGCONFIG(TAG,
" Tilt State Topic: '%s'", this->get_tilt_state_topic_to(topic_buf).c_str());
87#ifdef USE_MQTT_COVER_JSON
90 if (traits.get_supports_position()) {
91 ESP_LOGCONFIG(TAG,
" Position Command Topic: '%s'", this->get_position_command_topic_to(topic_buf).c_str());
93 if (traits.get_supports_tilt()) {
94 ESP_LOGCONFIG(TAG,
" Tilt Command Topic: '%s'", this->get_tilt_command_topic_to(topic_buf).c_str());
100 if (traits.get_is_assumed_state()) {
101 root[MQTT_OPTIMISTIC] =
true;
103 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
104#ifdef USE_MQTT_COVER_JSON
107 root[MQTT_VALUE_TEMPLATE] = ESPHOME_F(
"{{ value_json.state }}");
108 if (traits.get_supports_position()) {
110 root[MQTT_POSITION_TEMPLATE] = ESPHOME_F(
"{{ value_json.position }}");
111 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic_to(topic_buf);
113 if (traits.get_supports_tilt()) {
115 root[MQTT_TILT_STATUS_TEMPLATE] = ESPHOME_F(
"{{ value_json.tilt }}");
116 root[MQTT_TILT_COMMAND_TOPIC] = this->get_tilt_command_topic_to(topic_buf);
122 if (traits.get_supports_position()) {
123 root[MQTT_POSITION_TOPIC] = this->get_position_state_topic_to(topic_buf);
124 root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic_to(topic_buf);
126 if (traits.get_supports_tilt()) {
127 root[MQTT_TILT_STATUS_TOPIC] = this->get_tilt_state_topic_to(topic_buf);
128 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())))
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_command_stop_tilt()
Set the command to stop the cover tilt.
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(F &&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
void subscribe(T &&topic, F &&callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
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).
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
state state bool send_initial_state() override
MQTTCoverComponent(cover::Cover *cover)
void dump_config() override
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
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.