mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 00:18:11 +01:00
Command retain option for MQTT component (#3078)
This commit is contained in:
parent
97681d142e
commit
8187a4bce9
7 changed files with 304 additions and 289 deletions
|
@ -11,6 +11,7 @@ from esphome.const import (
|
|||
CONF_BROKER,
|
||||
CONF_CLIENT_ID,
|
||||
CONF_COMMAND_TOPIC,
|
||||
CONF_COMMAND_RETAIN,
|
||||
CONF_DISCOVERY,
|
||||
CONF_DISCOVERY_PREFIX,
|
||||
CONF_DISCOVERY_RETAIN,
|
||||
|
@ -392,6 +393,8 @@ async def register_mqtt_component(var, config):
|
|||
cg.add(var.set_custom_state_topic(config[CONF_STATE_TOPIC]))
|
||||
if CONF_COMMAND_TOPIC in config:
|
||||
cg.add(var.set_custom_command_topic(config[CONF_COMMAND_TOPIC]))
|
||||
if CONF_COMMAND_RETAIN in config:
|
||||
cg.add(var.set_command_retain(config[CONF_COMMAND_RETAIN]))
|
||||
if CONF_AVAILABILITY in config:
|
||||
availability = config[CONF_AVAILABILITY]
|
||||
if not availability:
|
||||
|
|
|
@ -92,6 +92,8 @@ bool MQTTComponent::send_discovery_() {
|
|||
root[MQTT_STATE_TOPIC] = this->get_state_topic_();
|
||||
if (config.command_topic)
|
||||
root[MQTT_COMMAND_TOPIC] = this->get_command_topic_();
|
||||
if (this->command_retain_)
|
||||
root[MQTT_COMMAND_RETAIN] = true;
|
||||
|
||||
if (this->availability_ == nullptr) {
|
||||
if (!global_mqtt_client->get_availability().topic.empty()) {
|
||||
|
@ -165,6 +167,7 @@ void MQTTComponent::set_custom_state_topic(const std::string &custom_state_topic
|
|||
void MQTTComponent::set_custom_command_topic(const std::string &custom_command_topic) {
|
||||
this->custom_command_topic_ = custom_command_topic;
|
||||
}
|
||||
void MQTTComponent::set_command_retain(bool command_retain) { this->command_retain_ = command_retain; }
|
||||
|
||||
void MQTTComponent::set_availability(std::string topic, std::string payload_available,
|
||||
std::string payload_not_available) {
|
||||
|
|
|
@ -91,6 +91,8 @@ class MQTTComponent : public Component {
|
|||
void set_custom_state_topic(const std::string &custom_state_topic);
|
||||
/// Set a custom command topic. Set to "" for default behavior.
|
||||
void set_custom_command_topic(const std::string &custom_command_topic);
|
||||
/// Set whether command message should be retained.
|
||||
void set_command_retain(bool command_retain);
|
||||
|
||||
/// MQTT_COMPONENT setup priority.
|
||||
float get_setup_priority() const override;
|
||||
|
@ -188,6 +190,7 @@ class MQTTComponent : public Component {
|
|||
|
||||
std::string custom_state_topic_{};
|
||||
std::string custom_command_topic_{};
|
||||
bool command_retain_{false};
|
||||
bool retain_{true};
|
||||
bool discovery_enabled_{true};
|
||||
std::unique_ptr<Availability> availability_;
|
||||
|
|
|
@ -45,6 +45,7 @@ constexpr const char *const MQTT_CLEANING_TEMPLATE = "cln_tpl";
|
|||
constexpr const char *const MQTT_COMMAND_OFF_TEMPLATE = "cmd_off_tpl";
|
||||
constexpr const char *const MQTT_COMMAND_ON_TEMPLATE = "cmd_on_tpl";
|
||||
constexpr const char *const MQTT_COMMAND_TOPIC = "cmd_t";
|
||||
constexpr const char *const MQTT_COMMAND_RETAIN = "ret";
|
||||
constexpr const char *const MQTT_COMMAND_TEMPLATE = "cmd_tpl";
|
||||
constexpr const char *const MQTT_CODE_ARM_REQUIRED = "cod_arm_req";
|
||||
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "cod_dis_req";
|
||||
|
@ -297,6 +298,7 @@ constexpr const char *const MQTT_CLEANING_TEMPLATE = "cleaning_template";
|
|||
constexpr const char *const MQTT_COMMAND_OFF_TEMPLATE = "command_off_template";
|
||||
constexpr const char *const MQTT_COMMAND_ON_TEMPLATE = "command_on_template";
|
||||
constexpr const char *const MQTT_COMMAND_TOPIC = "command_topic";
|
||||
constexpr const char *const MQTT_COMMAND_RETAIN = "retain";
|
||||
constexpr const char *const MQTT_COMMAND_TEMPLATE = "command_template";
|
||||
constexpr const char *const MQTT_CODE_ARM_REQUIRED = "code_arm_required";
|
||||
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "code_disarm_required";
|
||||
|
|
|
@ -17,6 +17,7 @@ from esphome.const import (
|
|||
ALLOWED_NAME_CHARS,
|
||||
CONF_AVAILABILITY,
|
||||
CONF_COMMAND_TOPIC,
|
||||
CONF_COMMAND_RETAIN,
|
||||
CONF_DISABLED_BY_DEFAULT,
|
||||
CONF_DISCOVERY,
|
||||
CONF_ENTITY_CATEGORY,
|
||||
|
@ -1591,6 +1592,7 @@ MQTT_COMPONENT_SCHEMA = Schema(
|
|||
MQTT_COMMAND_COMPONENT_SCHEMA = MQTT_COMPONENT_SCHEMA.extend(
|
||||
{
|
||||
Optional(CONF_COMMAND_TOPIC): All(requires_component("mqtt"), subscribe_topic),
|
||||
Optional(CONF_COMMAND_RETAIN): All(requires_component("mqtt"), boolean),
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -106,6 +106,7 @@ CONF_COLOR_MODE = "color_mode"
|
|||
CONF_COLOR_TEMPERATURE = "color_temperature"
|
||||
CONF_COLORS = "colors"
|
||||
CONF_COMMAND = "command"
|
||||
CONF_COMMAND_RETAIN = "command_retain"
|
||||
CONF_COMMAND_TOPIC = "command_topic"
|
||||
CONF_COMMENT = "comment"
|
||||
CONF_COMMIT = "commit"
|
||||
|
|
579
tests/test1.yaml
579
tests/test1.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue