mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Handle WebsocketConnectionError during mqtt auto reconnect (#133697)
followup to #133610 to handle the exception in the auto reconnect path as well fixes #132985
This commit is contained in:
parent
989a3d1e24
commit
4e316429d3
@ -695,12 +695,15 @@ class MQTT:
|
|||||||
|
|
||||||
async def _reconnect_loop(self) -> None:
|
async def _reconnect_loop(self) -> None:
|
||||||
"""Reconnect to the MQTT server."""
|
"""Reconnect to the MQTT server."""
|
||||||
|
# pylint: disable-next=import-outside-toplevel
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
try:
|
try:
|
||||||
async with self._connection_lock, self._async_connect_in_executor():
|
async with self._connection_lock, self._async_connect_in_executor():
|
||||||
await self.hass.async_add_executor_job(self._mqttc.reconnect)
|
await self.hass.async_add_executor_job(self._mqttc.reconnect)
|
||||||
except OSError as err:
|
except (OSError, mqtt.WebsocketConnectionError) as err:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Error re-connecting to MQTT server due to exception: %s", err
|
"Error re-connecting to MQTT server due to exception: %s", err
|
||||||
)
|
)
|
||||||
|
@ -1888,10 +1888,18 @@ async def test_mqtt_subscribes_and_unsubscribes_in_chunks(
|
|||||||
assert len(mqtt_client_mock.unsubscribe.mock_calls[1][1][0]) == 2
|
assert len(mqtt_client_mock.unsubscribe.mock_calls[1][1][0]) == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"exception",
|
||||||
|
[
|
||||||
|
OSError,
|
||||||
|
paho_mqtt.WebsocketConnectionError,
|
||||||
|
],
|
||||||
|
)
|
||||||
async def test_auto_reconnect(
|
async def test_auto_reconnect(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_with_birth_msg_client_mock: MqttMockPahoClient,
|
setup_with_birth_msg_client_mock: MqttMockPahoClient,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
exception: Exception,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test reconnection is automatically done."""
|
"""Test reconnection is automatically done."""
|
||||||
mqtt_client_mock = setup_with_birth_msg_client_mock
|
mqtt_client_mock = setup_with_birth_msg_client_mock
|
||||||
@ -1902,7 +1910,7 @@ async def test_auto_reconnect(
|
|||||||
mqtt_client_mock.on_disconnect(None, None, 0)
|
mqtt_client_mock.on_disconnect(None, None, 0)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
mqtt_client_mock.reconnect.side_effect = OSError("foo")
|
mqtt_client_mock.reconnect.side_effect = exception("foo")
|
||||||
async_fire_time_changed(
|
async_fire_time_changed(
|
||||||
hass, utcnow() + timedelta(seconds=RECONNECT_INTERVAL_SECONDS)
|
hass, utcnow() + timedelta(seconds=RECONNECT_INTERVAL_SECONDS)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user