Fix MQTT discovery reliability for Home Assistant#24
Open
Conversation
Three fixes to prevent sensors from becoming unavailable after broker restarts or HA reboots: 1. Remove device_class "aqi" from AQI sensor - causes compatibility issues with some Home Assistant versions where the entity stays unavailable after discovery 2. Track individual discovery publish failures - publishDiscoverySensor now returns bool and publishDiscovery only sets discoveryPublished when ALL sensors succeed, ensuring failed sensors are retried 3. Add periodic discovery re-publish every 60 minutes - if the MQTT broker restarts without persistent storage, retained discovery messages are lost. This ensures they are refreshed regularly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
device_class: "aqi"from AQI sensor discovery - causes HA compatibility issues where the entity stays unavailable after rediscoverypublishDiscoverySensornow returnsbool,publishDiscoveryonly marks complete when ALL sensors succeed, ensuring failed sensors are retried on the next loop cycleBackground
When the MQTT broker restarts, retained discovery messages are lost. The device continues sending sensor data to
tele/<topic>/state, but Home Assistant marks entities asunavailable/restoredbecause it has no discovery config to map the data. Previously, discovery was only published on MQTT connect - if the TCP connection survived the broker restart, discovery was never re-sent.Test plan
Generated with Claude Code