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 &)>;
106 void disable_last_will();
111 void disable_birth_message();
114 void disable_shutdown_message();
117 void set_keep_alive(uint16_t keep_alive_s);
133 void disable_discovery();
134 bool is_discovery_enabled()
const;
135 bool is_discovery_ip_enabled()
const;
137 #if ASYNC_TCP_SSL_ENABLED 150 void add_ssl_fingerprint(
const std::array<uint8_t, SHA1_SIZE> &fingerprint);
155 void set_cl_key(
const char *key) { this->mqtt_backend_.set_cl_key(key); }
168 void set_topic_prefix(
const std::string &topic_prefix);
170 const std::string &get_topic_prefix()
const;
173 void set_log_message_template(
MQTTMessage &&message);
174 void set_log_level(
int level);
176 void disable_log_message();
177 bool is_log_message_enabled()
const;
205 void unsubscribe(
const std::string &topic);
219 bool publish(
const std::string &topic,
const std::string &payload, uint8_t
qos = 0,
bool retain =
false);
221 bool publish(
const std::string &topic,
const char *payload,
size_t payload_length, uint8_t
qos = 0,
222 bool retain =
false);
230 bool publish_json(
const std::string &topic,
const json::json_build_t &f, uint8_t
qos = 0,
bool retain =
false);
233 void setup()
override;
234 void dump_config()
override;
236 void loop()
override;
238 float get_setup_priority()
const override;
240 void on_message(
const std::string &topic,
const std::string &payload);
242 bool can_proceed()
override;
244 void check_connected();
246 void set_reboot_timeout(uint32_t reboot_timeout);
255 void on_shutdown()
override;
259 void set_username(
const std::string &username) { this->credentials_.username = username; }
260 void set_password(
const std::string &password) { this->credentials_.password = password; }
261 void set_client_id(
const std::string &client_id) { this->credentials_.client_id = client_id; }
262 void set_clean_session(
const bool &clean_session) { this->credentials_.clean_session = clean_session; }
267 void send_device_info_();
270 void start_connect_();
271 void start_dnslookup_();
272 void check_dnslookup_();
273 #if defined(USE_ESP8266) && LWIP_VERSION_MAJOR == 1 274 static void dns_found_callback(
const char *
name,
ip_addr_t *ipaddr,
void *callback_arg);
276 static void dns_found_callback(
const char *name,
const ip_addr_t *ipaddr,
void *callback_arg);
280 void recalculate_availability_();
282 bool subscribe_(
const char *topic, uint8_t
qos);
284 void resubscribe_subscriptions_();
293 bool sent_birth_message_{
false};
300 .
prefix =
"homeassistant",
307 std::string topic_prefix_{};
310 int log_level_{ESPHOME_LOG_LEVEL};
313 #if defined(USE_ESP32) 315 #elif defined(USE_ESP8266) 317 #elif defined(USE_LIBRETINY) 323 bool dns_resolved_{
false};
324 bool dns_resolve_error_{
false};
325 bool enable_on_boot_{
true};
327 uint32_t reboot_timeout_{300000};
329 uint32_t last_connected_{0};
339 void set_qos(uint8_t
qos);
340 void set_payload(
const std::string &payload);
341 void setup()
override;
342 void dump_config()
override;
343 float get_setup_priority()
const override;
355 topic, [
this](
const std::string &topic, JsonObject root) { this->trigger(root); },
qos);
362 client->
set_on_connect([
this](
bool session_present) { this->trigger(); });
376 TEMPLATABLE_VALUE(std::string,
topic)
377 TEMPLATABLE_VALUE(std::string, payload)
378 TEMPLATABLE_VALUE(uint8_t,
qos)
379 TEMPLATABLE_VALUE(
bool, retain)
381 void play(Ts...
x)
override {
382 this->parent_->publish(this->topic_.value(
x...), this->payload_.value(
x...), this->qos_.value(
x...),
383 this->retain_.value(
x...));
393 TEMPLATABLE_VALUE(std::string,
topic)
394 TEMPLATABLE_VALUE(uint8_t,
qos)
395 TEMPLATABLE_VALUE(
bool, retain)
397 void set_payload(
std::function<
void(Ts..., JsonObject)> payload) { this->payload_ = payload; }
401 auto topic = this->topic_.value(
x...);
402 auto qos = this->qos_.value(
x...);
403 auto retain = this->retain_.value(
x...);
404 this->parent_->publish_json(
topic, f,
qos, retain);
408 void encode_(Ts...
x, JsonObject root) { this->payload_(
x..., root); }
416 bool check(Ts...
x)
override {
return this->parent_->is_connected(); }
426 void play(Ts...
x)
override { this->parent_->enable(); }
436 void play(Ts...
x)
override { this->parent_->disable(); }
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.
MQTTClientComponent * parent_
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)
void set_enable_on_boot(bool enable_on_boot)
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
MQTTDisableAction(MQTTClientComponent *parent)
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_
MQTTEnableAction(MQTTClientComponent *parent)
MQTTMessage shutdown_message_
Simple data struct for Home Assistant component availability.
MQTTClientComponent * parent_
std::function< MQTTBackend::on_disconnect_callback_t > mqtt_on_disconnect_callback_t
void play(Ts... x) override
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)
void play(Ts... x) override
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...