mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Handle mqtt.WebsocketConnectionError when connecting to the MQTT broker (#133610)
fixes #132985
This commit is contained in:
parent
fdde9d3a52
commit
ff9df15cb0
@ -661,7 +661,7 @@ class MQTT:
|
||||
self.conf.get(CONF_PORT, DEFAULT_PORT),
|
||||
self.conf.get(CONF_KEEPALIVE, DEFAULT_KEEPALIVE),
|
||||
)
|
||||
except OSError as err:
|
||||
except (OSError, mqtt.WebsocketConnectionError) as err:
|
||||
_LOGGER.error("Failed to connect to MQTT server due to exception: %s", err)
|
||||
self._async_connection_result(False)
|
||||
finally:
|
||||
|
@ -1403,8 +1403,15 @@ async def test_handle_mqtt_timeout_on_callback(
|
||||
assert not mock_debouncer.is_set()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception",
|
||||
[
|
||||
OSError("Connection error"),
|
||||
paho_mqtt.WebsocketConnectionError("Connection error"),
|
||||
],
|
||||
)
|
||||
async def test_setup_raises_config_entry_not_ready_if_no_connect_broker(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, exception: Exception
|
||||
) -> None:
|
||||
"""Test for setup failure if connection to broker is missing."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
|
||||
@ -1413,7 +1420,7 @@ async def test_setup_raises_config_entry_not_ready_if_no_connect_broker(
|
||||
with patch(
|
||||
"homeassistant.components.mqtt.async_client.AsyncMQTTClient"
|
||||
) as mock_client:
|
||||
mock_client().connect = MagicMock(side_effect=OSError("Connection error"))
|
||||
mock_client().connect = MagicMock(side_effect=exception)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert "Failed to connect to MQTT server due to exception:" in caplog.text
|
||||
|
Loading…
x
Reference in New Issue
Block a user