Skip to content

Commit

Permalink
feat(MQTT): Add QoS option for each MQTT component (esphome#6279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito authored Mar 11, 2024
1 parent c899a33 commit 6a8a2aa
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions esphome/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ def get_default_topic_for(data, component_type, name, suffix):
async def register_mqtt_component(var, config):
await cg.register_component(var, {})

if CONF_QOS in config:
cg.add(var.set_qos(config[CONF_QOS]))
if CONF_RETAIN in config:
cg.add(var.set_retain(config[CONF_RETAIN]))
if not config.get(CONF_DISCOVERY, True):
Expand Down
4 changes: 2 additions & 2 deletions esphome/components/mqtt/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ bool MQTTClientComponent::publish(const MQTTMessage &message) {

if (!logging_topic) {
if (ret) {
ESP_LOGV(TAG, "Publish(topic='%s' payload='%s' retain=%d)", message.topic.c_str(), message.payload.c_str(),
message.retain);
ESP_LOGV(TAG, "Publish(topic='%s' payload='%s' retain=%d qos=%d)", message.topic.c_str(), message.payload.c_str(),
message.retain, message.qos);
} else {
ESP_LOGV(TAG, "Publish failed for topic='%s' (len=%u). will retry later..", message.topic.c_str(),
message.payload.length());
Expand Down
12 changes: 8 additions & 4 deletions esphome/components/mqtt/mqtt_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace mqtt {

static const char *const TAG = "mqtt.component";

void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }

void MQTTComponent::set_retain(bool retain) { this->retain_ = retain; }

std::string MQTTComponent::get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const {
Expand Down Expand Up @@ -47,21 +49,21 @@ std::string MQTTComponent::get_command_topic_() const {
bool MQTTComponent::publish(const std::string &topic, const std::string &payload) {
if (topic.empty())
return false;
return global_mqtt_client->publish(topic, payload, 0, this->retain_);
return global_mqtt_client->publish(topic, payload, this->qos_, this->retain_);
}

bool MQTTComponent::publish_json(const std::string &topic, const json::json_build_t &f) {
if (topic.empty())
return false;
return global_mqtt_client->publish_json(topic, f, 0, this->retain_);
return global_mqtt_client->publish_json(topic, f, this->qos_, this->retain_);
}

bool MQTTComponent::send_discovery_() {
const MQTTDiscoveryInfo &discovery_info = global_mqtt_client->get_discovery_info();

if (discovery_info.clean) {
ESP_LOGV(TAG, "'%s': Cleaning discovery...", this->friendly_name().c_str());
return global_mqtt_client->publish(this->get_discovery_topic_(discovery_info), "", 0, 0, true);
return global_mqtt_client->publish(this->get_discovery_topic_(discovery_info), "", 0, this->qos_, true);
}

ESP_LOGV(TAG, "'%s': Sending discovery...", this->friendly_name().c_str());
Expand Down Expand Up @@ -155,9 +157,11 @@ bool MQTTComponent::send_discovery_() {
device_info[MQTT_DEVICE_MANUFACTURER] = "espressif";
device_info[MQTT_DEVICE_SUGGESTED_AREA] = node_area;
},
0, discovery_info.retain);
this->qos_, discovery_info.retain);
}

uint8_t MQTTComponent::get_qos() const { return this->qos_; }

bool MQTTComponent::get_retain() const { return this->retain_; }

bool MQTTComponent::is_discovery_enabled() const {
Expand Down
5 changes: 5 additions & 0 deletions esphome/components/mqtt/mqtt_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class MQTTComponent : public Component {

virtual bool is_internal();

/// Set QOS for state messages.
void set_qos(uint8_t qos);
uint8_t get_qos() const;

/// Set whether state message should be retained.
void set_retain(bool retain);
bool get_retain() const;
Expand Down Expand Up @@ -199,6 +203,7 @@ class MQTTComponent : public Component {

bool command_retain_{false};
bool retain_{true};
uint8_t qos_{0};
bool discovery_enabled_{true};
bool resend_state_{false};
};
Expand Down
2 changes: 2 additions & 0 deletions esphome/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
CONF_RETAIN,
CONF_QOS,
CONF_SETUP_PRIORITY,
CONF_STATE_TOPIC,
CONF_TOPIC,
Expand Down Expand Up @@ -1873,6 +1874,7 @@ def entity_category(value):

MQTT_COMPONENT_SCHEMA = Schema(
{
Optional(CONF_QOS): All(requires_component("mqtt"), int_range(min=0, max=2)),
Optional(CONF_RETAIN): All(requires_component("mqtt"), boolean),
Optional(CONF_DISCOVERY): All(requires_component("mqtt"), boolean),
Optional(CONF_STATE_TOPIC): All(requires_component("mqtt"), publish_topic),
Expand Down
1 change: 1 addition & 0 deletions tests/test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ sensor:
internal: true
address: 0x23
update_interval: 30s
qos: 2
retain: false
availability:
state_topic: livingroom/custom_state_topic
Expand Down

0 comments on commit 6a8a2aa

Please sign in to comment.