12 static const char *
const TAG =
"mqtt.cover";
22 call.set_command(payload.c_str());
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());
33 call.set_position(*value / 100.0f);
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());
45 call.set_tilt(*value / 100.0f);
55 LOG_MQTT_COMPONENT(
true, has_command_topic)
56 if (traits.get_supports_position()) {
57 ESP_LOGCONFIG(TAG,
" Position State Topic: '%s'", this->get_position_state_topic().c_str());
58 ESP_LOGCONFIG(TAG,
" Position Command Topic: '%s'", this->get_position_command_topic().c_str());
60 if (traits.get_supports_tilt()) {
61 ESP_LOGCONFIG(TAG,
" Tilt State Topic: '%s'", this->get_tilt_state_topic().c_str());
62 ESP_LOGCONFIG(TAG,
" Tilt Command Topic: '%s'", this->get_tilt_command_topic().c_str());
70 if (traits.get_is_assumed_state()) {
73 if (traits.get_supports_position()) {
77 if (traits.get_supports_tilt()) {
81 if (traits.get_supports_tilt() && !traits.get_supports_position()) {
93 if (traits.get_supports_position()) {
95 if (!this->
publish(this->get_position_state_topic(), pos))
98 if (traits.get_supports_tilt()) {
100 if (!this->
publish(this->get_tilt_state_topic(), pos))
107 : traits.get_supports_position() ?
"open"
Base class for all cover devices.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
std::string get_device_class()
Get the device class, using the manual override if set.
CoverCall make_call()
Construct a new cover call used to control the cover.
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
bool get_supports_position() const
The cover is currently closing.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
const EntityBase * get_entity() const override
constexpr const char *const MQTT_OPTIMISTIC
bool command_topic
If the command topic should be included. Default to true.
float tilt
The current tilt value of the cover from 0.0 to 1.0.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
virtual CoverTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&f)
constexpr const char *const MQTT_SET_POSITION_TOPIC
std::string component_type() const override
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
state state bool send_initial_state() override
Simple Helper struct used for Home Assistant MQTT send_discovery().
void dump_config() override
constexpr const char * c_str() const
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Implementation of SPI Controller mode.
constexpr const char *const MQTT_DEVICE_CLASS
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
MQTTCoverComponent(cover::Cover *cover)
constexpr const char *const MQTT_POSITION_TOPIC
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
The cover is currently opening.
const StringRef & get_name() const
constexpr const char *const MQTT_TILT_STATUS_TOPIC
constexpr const char *const MQTT_TILT_COMMAND_TOPIC