Skip to content

Commit

Permalink
Fix issue where MQTT Discovery fails on broker reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Nov 20, 2023
1 parent be0a06e commit 9b64dd9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ecowitt2mqtt/helpers/publisher/mqtt/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def __init__(self, config: Config, client: Client) -> None:
super().__init__(config, client)

self._discovery_infos: dict[str, HassDiscoveryInfo] = {}
self._first_publish = True

def _get_data_point_key(
self, payload_key: str, data_point: CalculatedDataPoint
Expand Down Expand Up @@ -555,7 +556,10 @@ async def async_publish(self, data: dict[str, CalculatedValueType]) -> None:
processed_data.device, payload_key, data_point
)

if self._discovery_infos.get(discovery_info.unique_id) != discovery_info:
if (
self._first_publish
or self._discovery_infos.get(discovery_info.unique_id) != discovery_info
):
LOGGER.debug(
"Publishing discovery info for %s", discovery_info.unique_id
)
Expand Down Expand Up @@ -605,5 +609,9 @@ async def async_publish(self, data: dict[str, CalculatedValueType]) -> None:
task.cancel()
raise

# If we've successful published our first payload, mark it:
if self._first_publish:
self._first_publish = False

LOGGER.info("Published to Home Assistant MQTT Discovery")
LOGGER.debug("Published data: %s", processed_data.output)

0 comments on commit 9b64dd9

Please sign in to comment.