aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-06 14:47:54 +0000
committerdiogo464 <[email protected]>2025-12-06 14:47:54 +0000
commit3a974cec36cd8a48992e05629325d8279cf289b7 (patch)
tree2e4c9457771f7d658235b2037835b87c47d202fd
parent809b1b795ed530d20ceb6f3cb42af70daa7eadf9 (diff)
Fix availability handling with MQTT retain flag
Added retain flag support to MQTT messages to properly handle device availability in Home Assistant. Both the availability "online" publish and the last will "offline" message now use retain=true, ensuring HA always sees the current device status even when subscribing after the messages were sent. Changes: - Added will_retain field to embedded_mqtt::ConnectParams - Set retain=true for availability publish using publish_with() - Set will_retain=true in connect params for last will message 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
-rw-r--r--embedded-mqtt/src/lib.rs3
-rw-r--r--src/lib.rs10
2 files changed, 11 insertions, 2 deletions
diff --git a/embedded-mqtt/src/lib.rs b/embedded-mqtt/src/lib.rs
index 096af63..7367b53 100644
--- a/embedded-mqtt/src/lib.rs
+++ b/embedded-mqtt/src/lib.rs
@@ -71,6 +71,7 @@ where
71pub struct ConnectParams<'a> { 71pub struct ConnectParams<'a> {
72 pub will_topic: Option<&'a str>, 72 pub will_topic: Option<&'a str>,
73 pub will_payload: Option<&'a [u8]>, 73 pub will_payload: Option<&'a [u8]>,
74 pub will_retain: bool,
74 pub username: Option<&'a str>, 75 pub username: Option<&'a str>,
75 pub password: Option<&'a [u8]>, 76 pub password: Option<&'a [u8]>,
76 pub keepalive: Option<u16>, 77 pub keepalive: Option<u16>,
@@ -197,7 +198,7 @@ where
197 password: params.password, 198 password: params.password,
198 will_topic: params.will_topic, 199 will_topic: params.will_topic,
199 will_payload: params.will_payload, 200 will_payload: params.will_payload,
200 will_retain: false, 201 will_retain: params.will_retain,
201 keepalive: None, 202 keepalive: None,
202 }, 203 },
203 ); 204 );
diff --git a/src/lib.rs b/src/lib.rs
index 3353663..ca2ab82 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -541,6 +541,7 @@ impl<'a> Device<'a> {
541 let connect_params = embedded_mqtt::ConnectParams { 541 let connect_params = embedded_mqtt::ConnectParams {
542 will_topic: Some(availability_topic), 542 will_topic: Some(availability_topic),
543 will_payload: Some(NOT_AVAILABLE_PAYLOAD.as_bytes()), 543 will_payload: Some(NOT_AVAILABLE_PAYLOAD.as_bytes()),
544 will_retain: true,
544 ..Default::default() 545 ..Default::default()
545 }; 546 };
546 if let Err(err) = client 547 if let Err(err) = client
@@ -665,7 +666,14 @@ impl<'a> Device<'a> {
665 } 666 }
666 667
667 if let Err(err) = client 668 if let Err(err) = client
668 .publish(availability_topic, AVAILABLE_PAYLOAD.as_bytes()) 669 .publish_with(
670 availability_topic,
671 AVAILABLE_PAYLOAD.as_bytes(),
672 embedded_mqtt::PublishParams {
673 retain: true,
674 ..Default::default()
675 },
676 )
669 .await 677 .await
670 { 678 {
671 crate::log::error!( 679 crate::log::error!(