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 &)>;
104 void disable_last_will();
109 void disable_birth_message();
112 void disable_shutdown_message();
115 void set_keep_alive(uint16_t keep_alive_s);
131 void disable_discovery();
132 bool is_discovery_enabled()
const;
133 bool is_discovery_ip_enabled()
const;
135 #if ASYNC_TCP_SSL_ENABLED 148 void add_ssl_fingerprint(
const std::array<uint8_t, SHA1_SIZE> &fingerprint);
153 void set_cl_key(
const char *key) { this->mqtt_backend_.set_cl_key(key); }
166 void set_topic_prefix(
const std::string &topic_prefix);
168 const std::string &get_topic_prefix()
const;
171 void set_log_message_template(
MQTTMessage &&message);
172 void set_log_level(
int level);
174 void disable_log_message();
175 bool is_log_message_enabled()
const;
203 void unsubscribe(
const std::string &topic);
217 bool publish(
const std::string &topic,
const std::string &payload, uint8_t
qos = 0,
bool retain =
false);
219 bool publish(
const std::string &topic,
const char *payload,
size_t payload_length, uint8_t
qos = 0,
220 bool retain =
false);
228 bool publish_json(
const std::string &topic,
const json::json_build_t &f, uint8_t
qos = 0,
bool retain =
false);
231 void setup()
override;
232 void dump_config()
override;
234 void loop()
override;
236 float get_setup_priority()
const override;
238 void on_message(
const std::string &topic,
const std::string &payload);
240 bool can_proceed()
override;
242 void check_connected();
244 void set_reboot_timeout(uint32_t reboot_timeout);
250 void on_shutdown()
override;
254 void set_username(
const std::string &username) { this->credentials_.username = username; }
255 void set_password(
const std::string &password) { this->credentials_.password = password; }
256 void set_client_id(
const std::string &client_id) { this->credentials_.client_id = client_id; }
261 void send_device_info_();
264 void start_connect_();
265 void start_dnslookup_();
266 void check_dnslookup_();
267 #if defined(USE_ESP8266) && LWIP_VERSION_MAJOR == 1 268 static void dns_found_callback(
const char *
name,
ip_addr_t *ipaddr,
void *callback_arg);
270 static void dns_found_callback(
const char *name,
const ip_addr_t *ipaddr,
void *callback_arg);
274 void recalculate_availability_();
276 bool subscribe_(
const char *topic, uint8_t
qos);
278 void resubscribe_subscriptions_();
287 bool sent_birth_message_{
false};
294 .
prefix =
"homeassistant",
301 std::string topic_prefix_{};
304 int log_level_{ESPHOME_LOG_LEVEL};
307 #if defined(USE_ESP32) 309 #elif defined(USE_ESP8266) 311 #elif defined(USE_LIBRETINY) 317 bool dns_resolved_{
false};
318 bool dns_resolve_error_{
false};
320 uint32_t reboot_timeout_{300000};
322 uint32_t last_connected_{0};
332 void set_qos(uint8_t
qos);
333 void set_payload(
const std::string &payload);
334 void setup()
override;
335 void dump_config()
override;
336 float get_setup_priority()
const override;
348 topic, [
this](
const std::string &topic, JsonObject root) { this->trigger(root); },
qos);
355 client->
set_on_connect([
this](
bool session_present) { this->trigger(); });
369 TEMPLATABLE_VALUE(std::string,
topic)
370 TEMPLATABLE_VALUE(std::string, payload)
371 TEMPLATABLE_VALUE(uint8_t,
qos)
372 TEMPLATABLE_VALUE(
bool, retain)
374 void play(Ts...
x)
override {
375 this->parent_->publish(this->topic_.value(
x...), this->payload_.value(
x...), this->qos_.value(
x...),
376 this->retain_.value(
x...));
386 TEMPLATABLE_VALUE(std::string,
topic)
387 TEMPLATABLE_VALUE(uint8_t,
qos)
388 TEMPLATABLE_VALUE(
bool, retain)
390 void set_payload(
std::function<
void(Ts..., JsonObject)> payload) { this->payload_ = payload; }
394 auto topic = this->topic_.value(
x...);
395 auto qos = this->qos_.value(
x...);
396 auto retain = this->retain_.value(
x...);
397 this->parent_->publish_json(
topic, f,
qos, retain);
401 void encode_(Ts...
x, JsonObject root) { this->payload_(
x..., root); }
409 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_
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.
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...