feat(MQTT): Add QoS option for each MQTT component (#6279)

This commit is contained in:
Rodrigo Martín 2024-03-11 01:12:52 +01:00 committed by GitHub
parent c899a33d1a
commit 6a8a2aaefb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 6 deletions

View file

@ -490,6 +490,8 @@ def get_default_topic_for(data, component_type, name, suffix):
async def register_mqtt_component(var, config): async def register_mqtt_component(var, config):
await cg.register_component(var, {}) await cg.register_component(var, {})
if CONF_QOS in config:
cg.add(var.set_qos(config[CONF_QOS]))
if CONF_RETAIN in config: if CONF_RETAIN in config:
cg.add(var.set_retain(config[CONF_RETAIN])) cg.add(var.set_retain(config[CONF_RETAIN]))
if not config.get(CONF_DISCOVERY, True): if not config.get(CONF_DISCOVERY, True):

View file

@ -474,8 +474,8 @@ bool MQTTClientComponent::publish(const MQTTMessage &message) {
if (!logging_topic) { if (!logging_topic) {
if (ret) { if (ret) {
ESP_LOGV(TAG, "Publish(topic='%s' payload='%s' retain=%d)", message.topic.c_str(), message.payload.c_str(), ESP_LOGV(TAG, "Publish(topic='%s' payload='%s' retain=%d qos=%d)", message.topic.c_str(), message.payload.c_str(),
message.retain); message.retain, message.qos);
} else { } else {
ESP_LOGV(TAG, "Publish failed for topic='%s' (len=%u). will retry later..", message.topic.c_str(), ESP_LOGV(TAG, "Publish failed for topic='%s' (len=%u). will retry later..", message.topic.c_str(),
message.payload.length()); message.payload.length());

View file

@ -14,6 +14,8 @@ namespace mqtt {
static const char *const TAG = "mqtt.component"; 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; } void MQTTComponent::set_retain(bool retain) { this->retain_ = retain; }
std::string MQTTComponent::get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const { std::string MQTTComponent::get_discovery_topic_(const MQTTDiscoveryInfo &discovery_info) const {
@ -47,13 +49,13 @@ std::string MQTTComponent::get_command_topic_() const {
bool MQTTComponent::publish(const std::string &topic, const std::string &payload) { bool MQTTComponent::publish(const std::string &topic, const std::string &payload) {
if (topic.empty()) if (topic.empty())
return false; 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) { bool MQTTComponent::publish_json(const std::string &topic, const json::json_build_t &f) {
if (topic.empty()) if (topic.empty())
return false; 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_() { bool MQTTComponent::send_discovery_() {
@ -61,7 +63,7 @@ bool MQTTComponent::send_discovery_() {
if (discovery_info.clean) { if (discovery_info.clean) {
ESP_LOGV(TAG, "'%s': Cleaning discovery...", this->friendly_name().c_str()); 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()); ESP_LOGV(TAG, "'%s': Sending discovery...", this->friendly_name().c_str());
@ -155,9 +157,11 @@ bool MQTTComponent::send_discovery_() {
device_info[MQTT_DEVICE_MANUFACTURER] = "espressif"; device_info[MQTT_DEVICE_MANUFACTURER] = "espressif";
device_info[MQTT_DEVICE_SUGGESTED_AREA] = node_area; 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::get_retain() const { return this->retain_; }
bool MQTTComponent::is_discovery_enabled() const { bool MQTTComponent::is_discovery_enabled() const {

View file

@ -77,6 +77,10 @@ class MQTTComponent : public Component {
virtual bool is_internal(); 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. /// Set whether state message should be retained.
void set_retain(bool retain); void set_retain(bool retain);
bool get_retain() const; bool get_retain() const;
@ -199,6 +203,7 @@ class MQTTComponent : public Component {
bool command_retain_{false}; bool command_retain_{false};
bool retain_{true}; bool retain_{true};
uint8_t qos_{0};
bool discovery_enabled_{true}; bool discovery_enabled_{true};
bool resend_state_{false}; bool resend_state_{false};
}; };

View file

@ -29,6 +29,7 @@ from esphome.const import (
CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE,
CONF_RETAIN, CONF_RETAIN,
CONF_QOS,
CONF_SETUP_PRIORITY, CONF_SETUP_PRIORITY,
CONF_STATE_TOPIC, CONF_STATE_TOPIC,
CONF_TOPIC, CONF_TOPIC,
@ -1873,6 +1874,7 @@ MQTT_COMPONENT_AVAILABILITY_SCHEMA = Schema(
MQTT_COMPONENT_SCHEMA = Schema( 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_RETAIN): All(requires_component("mqtt"), boolean),
Optional(CONF_DISCOVERY): All(requires_component("mqtt"), boolean), Optional(CONF_DISCOVERY): All(requires_component("mqtt"), boolean),
Optional(CONF_STATE_TOPIC): All(requires_component("mqtt"), publish_topic), Optional(CONF_STATE_TOPIC): All(requires_component("mqtt"), publish_topic),

View file

@ -693,6 +693,7 @@ sensor:
internal: true internal: true
address: 0x23 address: 0x23
update_interval: 30s update_interval: 30s
qos: 2
retain: false retain: false
availability: availability:
state_topic: livingroom/custom_state_topic state_topic: livingroom/custom_state_topic