diff options
| author | diogo464 <[email protected]> | 2025-12-06 14:47:54 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-06 14:47:54 +0000 |
| commit | 3a974cec36cd8a48992e05629325d8279cf289b7 (patch) | |
| tree | 2e4c9457771f7d658235b2037835b87c47d202fd /src/lib.rs | |
| parent | 809b1b795ed530d20ceb6f3cb42af70daa7eadf9 (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]>
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 10 |
1 files changed, 9 insertions, 1 deletions
| @@ -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!( |
