feat(MQTT): Add subscribe QoS to discovery (#7648)

This commit is contained in:
Rodrigo Martín 2024-10-22 05:38:25 +02:00 committed by GitHub
parent 2597975ae0
commit a932ca2f64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 20 additions and 1 deletions

View file

@ -41,6 +41,7 @@ from esphome.const import (
CONF_SHUTDOWN_MESSAGE, CONF_SHUTDOWN_MESSAGE,
CONF_SSL_FINGERPRINTS, CONF_SSL_FINGERPRINTS,
CONF_STATE_TOPIC, CONF_STATE_TOPIC,
CONF_SUBSCRIBE_QOS,
CONF_TOPIC, CONF_TOPIC,
CONF_TOPIC_PREFIX, CONF_TOPIC_PREFIX,
CONF_TRIGGER_ID, CONF_TRIGGER_ID,
@ -518,6 +519,8 @@ async def register_mqtt_component(var, config):
cg.add(var.set_qos(config[CONF_QOS])) 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 CONF_SUBSCRIBE_QOS in config:
cg.add(var.set_subscribe_qos(config[CONF_SUBSCRIBE_QOS]))
if not config.get(CONF_DISCOVERY, True): if not config.get(CONF_DISCOVERY, True):
cg.add(var.disable_discovery()) cg.add(var.disable_discovery())
if CONF_STATE_TOPIC in config: if CONF_STATE_TOPIC in config:

View file

@ -16,6 +16,8 @@ static const char *const TAG = "mqtt.component";
void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; } void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }
void MQTTComponent::set_subscribe_qos(uint8_t qos) { this->subscribe_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 {
@ -76,6 +78,10 @@ bool MQTTComponent::send_discovery_() {
config.command_topic = true; config.command_topic = true;
this->send_discovery(root, config); this->send_discovery(root, config);
// Set subscription QoS (default is 0)
if (this->subscribe_qos_ != 0) {
root[MQTT_QOS] = this->subscribe_qos_;
}
// Fields from EntityBase // Fields from EntityBase
if (this->get_entity()->has_own_name()) { if (this->get_entity()->has_own_name()) {

View file

@ -89,6 +89,9 @@ class MQTTComponent : public Component {
void disable_discovery(); void disable_discovery();
bool is_discovery_enabled() const; bool is_discovery_enabled() const;
/// Set the QOS for subscribe messages (used in discovery).
void set_subscribe_qos(uint8_t qos);
/// Override this method to return the component type (e.g. "light", "sensor", ...) /// Override this method to return the component type (e.g. "light", "sensor", ...)
virtual std::string component_type() const = 0; virtual std::string component_type() const = 0;
@ -204,6 +207,7 @@ class MQTTComponent : public Component {
bool command_retain_{false}; bool command_retain_{false};
bool retain_{true}; bool retain_{true};
uint8_t qos_{0}; uint8_t qos_{0};
uint8_t subscribe_qos_{0};
bool discovery_enabled_{true}; bool discovery_enabled_{true};
bool resend_state_{false}; bool resend_state_{false};
}; };

View file

@ -180,6 +180,7 @@ constexpr const char *const MQTT_PRESET_MODE_COMMAND_TOPIC = "pr_mode_cmd_t";
constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "pr_mode_stat_t"; constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "pr_mode_stat_t";
constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "pr_mode_val_tpl"; constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "pr_mode_val_tpl";
constexpr const char *const MQTT_PRESET_MODES = "pr_modes"; constexpr const char *const MQTT_PRESET_MODES = "pr_modes";
constexpr const char *const MQTT_QOS = "qos";
constexpr const char *const MQTT_RED_TEMPLATE = "r_tpl"; constexpr const char *const MQTT_RED_TEMPLATE = "r_tpl";
constexpr const char *const MQTT_RETAIN = "ret"; constexpr const char *const MQTT_RETAIN = "ret";
constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_cmd_tpl"; constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_cmd_tpl";
@ -441,6 +442,7 @@ constexpr const char *const MQTT_PRESET_MODE_COMMAND_TOPIC = "preset_mode_comman
constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "preset_mode_state_topic"; constexpr const char *const MQTT_PRESET_MODE_STATE_TOPIC = "preset_mode_state_topic";
constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "preset_mode_value_template"; constexpr const char *const MQTT_PRESET_MODE_VALUE_TEMPLATE = "preset_mode_value_template";
constexpr const char *const MQTT_PRESET_MODES = "preset_modes"; constexpr const char *const MQTT_PRESET_MODES = "preset_modes";
constexpr const char *const MQTT_QOS = "qos";
constexpr const char *const MQTT_RED_TEMPLATE = "red_template"; constexpr const char *const MQTT_RED_TEMPLATE = "red_template";
constexpr const char *const MQTT_RETAIN = "retain"; constexpr const char *const MQTT_RETAIN = "retain";
constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_command_template"; constexpr const char *const MQTT_RGB_COMMAND_TEMPLATE = "rgb_command_template";

View file

@ -40,6 +40,7 @@ from esphome.const import (
CONF_SECOND, CONF_SECOND,
CONF_SETUP_PRIORITY, CONF_SETUP_PRIORITY,
CONF_STATE_TOPIC, CONF_STATE_TOPIC,
CONF_SUBSCRIBE_QOS,
CONF_TOPIC, CONF_TOPIC,
CONF_TYPE, CONF_TYPE,
CONF_TYPE_ID, CONF_TYPE_ID,
@ -1893,9 +1894,10 @@ 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_QOS): All(requires_component("mqtt"), mqtt_qos),
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_SUBSCRIBE_QOS): All(requires_component("mqtt"), mqtt_qos),
Optional(CONF_STATE_TOPIC): All(requires_component("mqtt"), publish_topic), Optional(CONF_STATE_TOPIC): All(requires_component("mqtt"), publish_topic),
Optional(CONF_AVAILABILITY): All( Optional(CONF_AVAILABILITY): All(
requires_component("mqtt"), Any(None, MQTT_COMPONENT_AVAILABILITY_SCHEMA) requires_component("mqtt"), Any(None, MQTT_COMPONENT_AVAILABILITY_SCHEMA)

View file

@ -819,6 +819,7 @@ CONF_STOP = "stop"
CONF_STOP_ACTION = "stop_action" CONF_STOP_ACTION = "stop_action"
CONF_STORE_BASELINE = "store_baseline" CONF_STORE_BASELINE = "store_baseline"
CONF_SUBNET = "subnet" CONF_SUBNET = "subnet"
CONF_SUBSCRIBE_QOS = "subscribe_qos"
CONF_SUBSTITUTIONS = "substitutions" CONF_SUBSTITUTIONS = "substitutions"
CONF_SUM = "sum" CONF_SUM = "sum"
CONF_SUPPLEMENTAL_COOLING_ACTION = "supplemental_cooling_action" CONF_SUPPLEMENTAL_COOLING_ACTION = "supplemental_cooling_action"

View file

@ -227,6 +227,7 @@ datetime:
type: date type: date
state_topic: some/topic/date state_topic: some/topic/date
qos: 2 qos: 2
subscribe_qos: 2
set_action: set_action:
- logger.log: "set_value" - logger.log: "set_value"
on_value: on_value: