Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os
import ssl
import sys
import time
from typing import Any, Callable, List, Optional, Union

import attr
Expand Down Expand Up @@ -928,7 +927,6 @@ def _mqtt_on_connect(self, _mqttc, _userdata, _flags, result_code: int) -> None:
"Unable to connect to the MQTT broker: %s",
mqtt.connack_string(result_code),
)
self._mqttc.disconnect()
return

self.connected = True
Expand Down Expand Up @@ -989,31 +987,7 @@ def _mqtt_handle_message(self, msg) -> None:
def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code: int) -> None:
"""Disconnected callback."""
self.connected = False

# When disconnected because of calling disconnect()
if result_code == 0:
return

tries = 0

while True:
try:
if self._mqttc.reconnect() == 0:
self.connected = True
_LOGGER.info("Successfully reconnected to the MQTT server")
break
except OSError:
pass

wait_time = min(2 ** tries, MAX_RECONNECT_WAIT)
_LOGGER.warning(
"Disconnected from MQTT (%s). Trying to reconnect in %s s",
result_code,
wait_time,
)
# It is ok to sleep here as we are in the MQTT thread.
time.sleep(wait_time)
tries += 1
_LOGGER.warning("Disconnected from MQTT (%s).", result_code)


def _raise_on_error(result_code: int) -> None:
Expand Down
28 changes: 0 additions & 28 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,34 +571,6 @@ def test_subscribe_special_characters(self):
assert self.calls[0][0].topic == topic
assert self.calls[0][0].payload == payload

def test_mqtt_failed_connection_results_in_disconnect(self):
"""Test if connection failure leads to disconnect."""
for result_code in range(1, 6):
self.hass.data["mqtt"]._mqttc = mock.MagicMock()
self.hass.data["mqtt"]._mqtt_on_connect(
None, {"topics": {}}, 0, result_code
)
assert self.hass.data["mqtt"]._mqttc.disconnect.called

def test_mqtt_disconnect_tries_no_reconnect_on_stop(self):
"""Test the disconnect tries."""
self.hass.data["mqtt"]._mqtt_on_disconnect(None, None, 0)
assert not self.hass.data["mqtt"]._mqttc.reconnect.called

@mock.patch("homeassistant.components.mqtt.time.sleep")
def test_mqtt_disconnect_tries_reconnect(self, mock_sleep):
"""Test the re-connect tries."""
self.hass.data["mqtt"].subscriptions = [
mqtt.Subscription("test/progress", None, 0),
mqtt.Subscription("test/progress", None, 1),
mqtt.Subscription("test/topic", None, 2),
]
self.hass.data["mqtt"]._mqttc.reconnect.side_effect = [1, 1, 1, 0]
self.hass.data["mqtt"]._mqtt_on_disconnect(None, None, 1)
assert self.hass.data["mqtt"]._mqttc.reconnect.called
assert len(self.hass.data["mqtt"]._mqttc.reconnect.mock_calls) == 4
assert [call[1][0] for call in mock_sleep.mock_calls] == [1, 2, 4]

def test_retained_message_on_subscribe_received(self):
"""Test every subscriber receives retained message on subscribe."""

Expand Down