Remove reconnect logic from MQTT client. (#34556)

This commit is contained in:
Erik Montnemery 2020-04-22 23:28:36 +02:00 committed by GitHub
parent 116012680a
commit 6631bbc6f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 55 deletions

View File

@ -9,7 +9,6 @@ from operator import attrgetter
import os import os
import ssl import ssl
import sys import sys
import time
from typing import Any, Callable, List, Optional, Union from typing import Any, Callable, List, Optional, Union
import attr import attr
@ -929,7 +928,6 @@ class MQTT:
"Unable to connect to the MQTT broker: %s", "Unable to connect to the MQTT broker: %s",
mqtt.connack_string(result_code), mqtt.connack_string(result_code),
) )
self._mqttc.disconnect()
return return
self.connected = True self.connected = True
@ -999,31 +997,7 @@ class MQTT:
def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code: int) -> None: def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code: int) -> None:
"""Disconnected callback.""" """Disconnected callback."""
self.connected = False self.connected = False
_LOGGER.warning("Disconnected from MQTT (%s).", result_code)
# 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
def _raise_on_error(result_code: int) -> None: def _raise_on_error(result_code: int) -> None:

View File

@ -571,34 +571,6 @@ class TestMQTTCallbacks(unittest.TestCase):
assert self.calls[0][0].topic == topic assert self.calls[0][0].topic == topic
assert self.calls[0][0].payload == payload 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): def test_retained_message_on_subscribe_received(self):
"""Test every subscriber receives retained message on subscribe.""" """Test every subscriber receives retained message on subscribe."""