diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 5c0f3108960..248769099ae 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -9,7 +9,6 @@ from operator import attrgetter import os import ssl import sys -import time from typing import Any, Callable, List, Optional, Union import attr @@ -929,7 +928,6 @@ class MQTT: "Unable to connect to the MQTT broker: %s", mqtt.connack_string(result_code), ) - self._mqttc.disconnect() return self.connected = True @@ -999,31 +997,7 @@ class MQTT: 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: diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 9b5f70a95ec..75c6d49d156 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -571,34 +571,6 @@ class TestMQTTCallbacks(unittest.TestCase): 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."""