12 #if defined(USE_ESP32) 14 #elif defined(USE_ESP8266) 16 #elif defined(USE_LIBRETINY) 19 #include "lwip/ip_addr.h" 35 using mqtt_callback_t = std::function<void(const std::string &, const std::string &)>;
105 void disable_last_will();
110 void disable_birth_message();
113 void disable_shutdown_message();
116 void set_keep_alive(uint16_t keep_alive_s);
132 void disable_discovery();
133 bool is_discovery_enabled()
const;
134 bool is_discovery_ip_enabled()
const;
136 #if ASYNC_TCP_SSL_ENABLED 149 void add_ssl_fingerprint(
const std::array<uint8_t, SHA1_SIZE> &fingerprint);
154 void set_cl_key(
const char *key) { this->mqtt_backend_.set_cl_key(key); }
167 void set_topic_prefix(
const std::string &topic_prefix);
169 const std::string &get_topic_prefix()
const;
172 void set_log_message_template(
MQTTMessage &&message);
173 void set_log_level(
int level);
175 void disable_log_message();
176 bool is_log_message_enabled()
const;
204 void unsubscribe(
const std::string &topic);
218 bool publish(
const std::string &topic,
const std::string &payload, uint8_t
qos = 0,
bool retain =
false);
220 bool publish(
const std::string &topic,
const char *payload,
size_t payload_length, uint8_t
qos = 0,
221 bool retain =
false);
229 bool publish_json(
const std::string &topic,
const json::json_build_t &f, uint8_t
qos = 0,
bool retain =
false);
232 void setup()
override;
233 void dump_config()
override;
235 void loop()
override;
237 float get_setup_priority()
const override;
239 void on_message(
const std::string &topic,
const std::string &payload);
241 bool can_proceed()
override;
243 void check_connected();
245 void set_reboot_timeout(uint32_t reboot_timeout);
251 void on_shutdown()
override;
255 void set_username(
const std::string &username) { this->credentials_.username = username; }
256 void set_password(
const std::string &password) { this->credentials_.password = password; }
257 void set_client_id(
const std::string &client_id) { this->credentials_.client_id = client_id; }
258 void set_clean_session(
const bool &clean_session) { this->credentials_.clean_session = clean_session; }
263 void send_device_info_();
266 void start_connect_();
267 void start_dnslookup_();
268 void check_dnslookup_();
269 #if defined(USE_ESP8266) && LWIP_VERSION_MAJOR == 1 270 static void dns_found_callback(
const char *
name,
ip_addr_t *ipaddr,
void *callback_arg);
272 static void dns_found_callback(
const char *name,
const ip_addr_t *ipaddr,
void *callback_arg);
276 void recalculate_availability_();
278 bool subscribe_(
const char *topic, uint8_t
qos);
280 void resubscribe_subscriptions_();
289 bool sent_birth_message_{
false};
296 .
prefix =
"homeassistant",
303 std::string topic_prefix_{};
306 int log_level_{ESPHOME_LOG_LEVEL};
309 #if defined(USE_ESP32) 311 #elif defined(USE_ESP8266) 313 #elif defined(USE_LIBRETINY) 319 bool dns_resolved_{
false};
320 bool dns_resolve_error_{
false};
322 uint32_t reboot_timeout_{300000};
324 uint32_t last_connected_{0};
334 void set_qos(uint8_t
qos);
335 void set_payload(
const std::string &payload);
336 void setup()
override;
337 void dump_config()
override;
338 float get_setup_priority()
const override;
350 topic, [
this](
const std::string &topic, JsonObject root) { this->trigger(root); },
qos);
357 client->
set_on_connect([
this](
bool session_present) { this->trigger(); });
371 TEMPLATABLE_VALUE(std::string,
topic)
372 TEMPLATABLE_VALUE(std::string, payload)
373 TEMPLATABLE_VALUE(uint8_t,
qos)
374 TEMPLATABLE_VALUE(
bool, retain)
376 void play(Ts...
x)
override {
377 this->parent_->publish(this->topic_.value(
x...), this->payload_.value(
x...), this->qos_.value(
x...),
378 this->retain_.value(
x...));
388 TEMPLATABLE_VALUE(std::string,
topic)
389 TEMPLATABLE_VALUE(uint8_t,
qos)
390 TEMPLATABLE_VALUE(
bool, retain)
392 void set_payload(
std::function<
void(Ts..., JsonObject)> payload) { this->payload_ = payload; }
396 auto topic = this->topic_.value(
x...);
397 auto qos = this->qos_.value(
x...);
398 auto retain = this->retain_.value(
x...);
399 this->parent_->publish_json(
topic, f,
qos, retain);
403 void encode_(Ts...
x, JsonObject root) { this->payload_(
x..., root); }
411 bool check(Ts...
x)
override {
return this->parent_->is_connected(); }
uint32_t resubscribe_timeout
MQTTConnectedCondition(MQTTClientComponent *parent)
MQTTClientComponent * parent_
std::function< void(Ts..., JsonObject)> payload_
bool clean_session
Whether the session will be cleaned or remembered between connects.
optional< std::string > payload_
void set_client_id(const std::string &client_id)
MQTTPublishAction(MQTTClientComponent *parent)
std::string topic
Empty means disabled.
MQTTDiscoveryUniqueIdGenerator unique_id_generator
Internal struct for MQTT Home Assistant discovery.
void set_cl_certificate(const char *cert)
std::function< void(const std::string &, const std::string &)> mqtt_callback_t
Callback for MQTT subscriptions.
std::string client_id
The client ID. Will automatically be truncated to 23 characters.
void set_broker_port(uint16_t port)
internal struct for MQTT messages.
std::vector< MQTTComponent * > children_
void encode_(Ts... x, JsonObject root)
MQTTMessage last_will_
The last will message.
bool discover_ip
Enable the Home Assistant device discovery.
std::string payload_available
std::string prefix
The Home Assistant discovery prefix. Empty means disabled.
std::function< MQTTBackend::on_connect_callback_t > mqtt_on_connect_callback_t
Callback for MQTT events.
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
void set_skip_cert_cn_check(bool skip_check)
void set_on_connect(mqtt_on_connect_callback_t &&callback)
void set_password(const std::string &password)
MQTTBackendESP32 mqtt_backend_
void set_broker_address(const std::string &address)
MQTTClientComponent * global_mqtt_client
void play(Ts... x) override
Base class for all automation conditions.
MQTTJsonMessageTrigger(const std::string &topic, uint8_t qos)
MQTTBackendESP8266 mqtt_backend_
uint16_t port
The port number of the server.
MQTTDiscoveryUniqueIdGenerator
available discovery unique_id generators
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
bool check(Ts... x) override
MQTTPublishJsonAction(MQTTClientComponent *parent)
void set_ca_certificate(const char *cert)
MQTTDiscoveryObjectIdGenerator
available discovery object_id generators
internal struct for MQTT subscriptions.
MQTTBackendLibreTiny mqtt_backend_
MQTTClientDisconnectReason
std::string address
The address of the server without port number.
MQTTCredentials credentials_
MQTTMessage shutdown_message_
Simple data struct for Home Assistant component availability.
std::function< MQTTBackend::on_disconnect_callback_t > mqtt_on_disconnect_callback_t
MQTTMessage birth_message_
The birth message (e.g.
void set_clean_session(const bool &clean_session)
Implementation of SPI Controller mode.
std::string payload_not_available
MQTTConnectTrigger(MQTTClientComponent *&client)
std::vector< MQTTSubscription > subscriptions_
MQTTDiscoveryObjectIdGenerator object_id_generator
void set_cl_key(const char *key)
void set_username(const std::string &username)
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
std::string payload_buffer_
internal struct for MQTT credentials.
MQTTDisconnectTrigger(MQTTClientComponent *&client)
void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback)
MQTTClientComponent * parent_
bool retain
Whether to retain discovery messages.
std::function< void(const std::string &, JsonObject)> mqtt_json_callback_t
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...