Join both actions together

This commit is contained in:
Rapsssito 2024-09-18 22:18:06 +02:00
parent 179baecbc5
commit 0a6b1ea17e
2 changed files with 22 additions and 41 deletions

View file

@ -64,7 +64,6 @@ def AUTO_LOAD():
CONF_DISCOVER_IP = "discover_ip"
CONF_IDF_SEND_ASYNC = "idf_send_async"
CONF_SKIP_CERT_CN_CHECK = "skip_cert_cn_check"
CONF_DISCOVERY_ACTION_ID_ = "discovery_action_id_"
CONF_CONNECTION_INFO_ACTION_ID_ = "connection_info_action_id_"
@ -231,9 +230,6 @@ CONFIG_SCHEMA = cv.All(
cv.SplitDefault(CONF_SKIP_CERT_CN_CHECK, esp32_idf=False): cv.All(
cv.boolean, cv.only_with_esp_idf
),
cv.GenerateID(CONF_DISCOVERY_ACTION_ID_): cv.declare_id(
MQTTSetDiscoveryAction
),
cv.GenerateID(CONF_CONNECTION_INFO_ACTION_ID_): cv.declare_id(
MQTTSetConnectionInfoAction
),
@ -364,47 +360,43 @@ async def to_code(config):
await cg.templatable(config[CONF_CLIENT_ID], [], cg.std_string)
)
)
cg.add(connection_info_action.play())
discovery_action = cg.new_Pvariable(
config[CONF_DISCOVERY_ACTION_ID_], cg.TemplateArguments(None), var
)
if config[CONF_DISCOVERY] == "CLEAN":
cg.add(discovery_action.set_enable(True))
cg.add(discovery_action.set_clean(True))
cg.add(connection_info_action.set_enable(True))
cg.add(connection_info_action.set_clean(True))
else:
cg.add(
discovery_action.set_enable(
connection_info_action.set_enable(
await cg.templatable(config[CONF_DISCOVERY], [], bool)
)
)
cg.add(discovery_action.set_clean(False))
cg.add(connection_info_action.set_clean(False))
cg.add(
discovery_action.set_prefix(
connection_info_action.set_prefix(
await cg.templatable(config[CONF_DISCOVERY_PREFIX], [], cg.std_string)
)
)
cg.add(
discovery_action.set_retain(
connection_info_action.set_retain(
await cg.templatable(config[CONF_DISCOVERY_RETAIN], [], bool)
)
)
cg.add(
discovery_action.set_unique_id_generator(
connection_info_action.set_unique_id_generator(
config[CONF_DISCOVERY_UNIQUE_ID_GENERATOR]
)
)
cg.add(
discovery_action.set_discover_ip(
connection_info_action.set_discover_ip(
await cg.templatable(config[CONF_DISCOVER_IP], [], bool)
)
)
cg.add(
discovery_action.set_object_id_generator(
connection_info_action.set_object_id_generator(
config[CONF_DISCOVERY_OBJECT_ID_GENERATOR]
)
)
cg.add(discovery_action.play())
cg.add(connection_info_action.play())
cg.add(var.set_topic_prefix(config[CONF_TOPIC_PREFIX]))

View file

@ -403,9 +403,14 @@ template<typename... Ts> class MQTTPublishJsonAction : public Action<Ts...> {
MQTTClientComponent *parent_;
};
template<typename... Ts> class MQTTSetDiscoveryAction : public Action<Ts...> {
template<typename... Ts> class MQTTSetConnectionInfoAction : public Action<Ts...> {
public:
MQTTSetDiscoveryAction(MQTTClientComponent *parent) : parent_(parent) {}
MQTTSetConnectionInfoAction(MQTTClientComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, broker_address)
TEMPLATABLE_VALUE(uint16_t, broker_port)
TEMPLATABLE_VALUE(std::string, username)
TEMPLATABLE_VALUE(std::string, password)
TEMPLATABLE_VALUE(std::string, client_id)
TEMPLATABLE_VALUE(bool, enable)
TEMPLATABLE_VALUE(std::string, prefix)
TEMPLATABLE_VALUE(bool, retain)
@ -422,6 +427,11 @@ template<typename... Ts> class MQTTSetDiscoveryAction : public Action<Ts...> {
void set_clean(bool clean) { this->clean_ = clean; }
void play(Ts... x) override {
this->parent_->set_broker_address(this->broker_address_.value(x...));
this->parent_->set_broker_port(this->broker_port_.value(x...));
this->parent_->set_username(this->username_.value(x...));
this->parent_->set_password(this->password_.value(x...));
this->parent_->set_client_id(this->client_id_.value(x...));
bool enable = this->enable_.value(x...);
bool discover_ip = this->discover_ip_.value(x...);
if (!enable and !discover_ip) {
@ -444,27 +454,6 @@ template<typename... Ts> class MQTTSetDiscoveryAction : public Action<Ts...> {
bool clean_{false};
};
template<typename... Ts> class MQTTSetConnectionInfoAction : public Action<Ts...> {
public:
MQTTSetConnectionInfoAction(MQTTClientComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, broker_address)
TEMPLATABLE_VALUE(uint16_t, broker_port)
TEMPLATABLE_VALUE(std::string, username)
TEMPLATABLE_VALUE(std::string, password)
TEMPLATABLE_VALUE(std::string, client_id)
void play(Ts... x) override {
this->parent_->set_broker_address(this->broker_address_.value(x...));
this->parent_->set_broker_port(this->broker_port_.value(x...));
this->parent_->set_username(this->username_.value(x...));
this->parent_->set_password(this->password_.value(x...));
this->parent_->set_client_id(this->client_id_.value(x...));
}
protected:
MQTTClientComponent *parent_;
};
template<typename... Ts> class MQTTConnectedCondition : public Condition<Ts...> {
public:
MQTTConnectedCondition(MQTTClientComponent *parent) : parent_(parent) {}