2019-04-17 12:06:00 +02:00
|
|
|
#include "mqtt_cover.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
2021-10-31 03:34:08 +01:00
|
|
|
#include "mqtt_const.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_MQTT
|
2019-04-17 12:06:00 +02:00
|
|
|
#ifdef USE_COVER
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace mqtt {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "mqtt.cover";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
using namespace esphome::cover;
|
|
|
|
|
|
|
|
MQTTCoverComponent::MQTTCoverComponent(Cover *cover) : cover_(cover) {}
|
|
|
|
void MQTTCoverComponent::setup() {
|
|
|
|
auto traits = this->cover_->get_traits();
|
|
|
|
this->cover_->add_on_state_callback([this]() { this->publish_state(); });
|
|
|
|
this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
|
|
|
|
auto call = this->cover_->make_call();
|
|
|
|
call.set_command(payload.c_str());
|
|
|
|
call.perform();
|
|
|
|
});
|
|
|
|
if (traits.get_supports_position()) {
|
|
|
|
this->subscribe(this->get_position_command_topic(), [this](const std::string &topic, const std::string &payload) {
|
2021-11-10 19:15:06 +01:00
|
|
|
auto value = parse_number<float>(payload);
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!value.has_value()) {
|
|
|
|
ESP_LOGW(TAG, "Invalid position value: '%s'", payload.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto call = this->cover_->make_call();
|
|
|
|
call.set_position(*value / 100.0f);
|
|
|
|
call.perform();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (traits.get_supports_tilt()) {
|
|
|
|
this->subscribe(this->get_tilt_command_topic(), [this](const std::string &topic, const std::string &payload) {
|
2021-11-10 19:15:06 +01:00
|
|
|
auto value = parse_number<float>(payload);
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!value.has_value()) {
|
|
|
|
ESP_LOGW(TAG, "Invalid tilt value: '%s'", payload.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto call = this->cover_->make_call();
|
|
|
|
call.set_tilt(*value / 100.0f);
|
|
|
|
call.perform();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MQTTCoverComponent::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "MQTT cover '%s':", this->cover_->get_name().c_str());
|
|
|
|
auto traits = this->cover_->get_traits();
|
2022-05-16 05:35:27 +02:00
|
|
|
bool has_command_topic = traits.get_supports_position() || !traits.get_supports_tilt();
|
|
|
|
LOG_MQTT_COMPONENT(true, has_command_topic)
|
|
|
|
if (traits.get_supports_position()) {
|
2019-04-17 12:06:00 +02:00
|
|
|
ESP_LOGCONFIG(TAG, " Position State Topic: '%s'", this->get_position_state_topic().c_str());
|
|
|
|
ESP_LOGCONFIG(TAG, " Position Command Topic: '%s'", this->get_position_command_topic().c_str());
|
|
|
|
}
|
|
|
|
if (traits.get_supports_tilt()) {
|
|
|
|
ESP_LOGCONFIG(TAG, " Tilt State Topic: '%s'", this->get_tilt_state_topic().c_str());
|
|
|
|
ESP_LOGCONFIG(TAG, " Tilt Command Topic: '%s'", this->get_tilt_command_topic().c_str());
|
|
|
|
}
|
|
|
|
}
|
2022-01-01 10:31:43 +01:00
|
|
|
void MQTTCoverComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
2021-07-29 16:08:48 +02:00
|
|
|
if (!this->cover_->get_device_class().empty())
|
2021-10-31 03:34:08 +01:00
|
|
|
root[MQTT_DEVICE_CLASS] = this->cover_->get_device_class();
|
2021-07-29 16:08:48 +02:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
auto traits = this->cover_->get_traits();
|
|
|
|
if (traits.get_is_assumed_state()) {
|
2021-10-31 03:34:08 +01:00
|
|
|
root[MQTT_OPTIMISTIC] = true;
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
if (traits.get_supports_position()) {
|
2021-10-31 03:34:08 +01:00
|
|
|
root[MQTT_POSITION_TOPIC] = this->get_position_state_topic();
|
|
|
|
root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic();
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
if (traits.get_supports_tilt()) {
|
2021-10-31 03:34:08 +01:00
|
|
|
root[MQTT_TILT_STATUS_TOPIC] = this->get_tilt_state_topic();
|
|
|
|
root[MQTT_TILT_COMMAND_TOPIC] = this->get_tilt_command_topic();
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
2019-06-18 19:42:36 +02:00
|
|
|
if (traits.get_supports_tilt() && !traits.get_supports_position()) {
|
|
|
|
config.command_topic = false;
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MQTTCoverComponent::component_type() const { return "cover"; }
|
2021-10-10 10:37:05 +02:00
|
|
|
const EntityBase *MQTTCoverComponent::get_entity() const { return this->cover_; }
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
bool MQTTCoverComponent::send_initial_state() { return this->publish_state(); }
|
|
|
|
bool MQTTCoverComponent::publish_state() {
|
|
|
|
auto traits = this->cover_->get_traits();
|
|
|
|
bool success = true;
|
2022-05-16 05:35:27 +02:00
|
|
|
if (traits.get_supports_position()) {
|
2019-04-17 12:06:00 +02:00
|
|
|
std::string pos = value_accuracy_to_string(roundf(this->cover_->position * 100), 0);
|
|
|
|
if (!this->publish(this->get_position_state_topic(), pos))
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
if (traits.get_supports_tilt()) {
|
|
|
|
std::string pos = value_accuracy_to_string(roundf(this->cover_->tilt * 100), 0);
|
|
|
|
if (!this->publish(this->get_tilt_state_topic(), pos))
|
|
|
|
success = false;
|
|
|
|
}
|
2022-05-16 05:35:27 +02:00
|
|
|
const char *state_s = this->cover_->current_operation == COVER_OPERATION_OPENING ? "opening"
|
|
|
|
: this->cover_->current_operation == COVER_OPERATION_CLOSING ? "closing"
|
|
|
|
: this->cover_->position == COVER_CLOSED ? "closed"
|
|
|
|
: this->cover_->position == COVER_OPEN ? "open"
|
|
|
|
: traits.get_supports_position() ? "open"
|
|
|
|
: "unknown";
|
|
|
|
if (!this->publish(this->get_state_topic_(), state_s))
|
|
|
|
success = false;
|
2019-04-17 12:06:00 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mqtt
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|
2021-09-20 11:47:51 +02:00
|
|
|
#endif // USE_MQTT
|