diff --git a/homeassistant/components/lg_thinq/mqtt.py b/homeassistant/components/lg_thinq/mqtt.py index 539437dd14c1..25ed3c2c6190 100644 --- a/homeassistant/components/lg_thinq/mqtt.py +++ b/homeassistant/components/lg_thinq/mqtt.py @@ -6,6 +6,7 @@ import json import logging from typing import Any +from aiohttp import ClientError from thinqconnect import ( DeviceType, ThinQApi, @@ -58,7 +59,8 @@ class ThinQMQTT: if self.client is not None: try: await self.client.async_disconnect() - except ThinQAPIException, TypeError, ValueError: + except ThinQAPIException, TypeError, ValueError, ClientError, TimeoutError: + # Saying goodbye is a courtesy, never a reason to fail the unload _LOGGER.exception("Failed to disconnect") def _get_failed_device_count( diff --git a/tests/components/lg_thinq/test_init.py b/tests/components/lg_thinq/test_init.py index 9b49bb44be90..00c742b226b6 100644 --- a/tests/components/lg_thinq/test_init.py +++ b/tests/components/lg_thinq/test_init.py @@ -34,6 +34,41 @@ async def test_load_unload_entry( assert mock_config_entry.state is ConfigEntryState.NOT_LOADED +@pytest.mark.parametrize( + "exception", + [ + ThinQAPIException(code="1309", message="Not allowed api call", headers={}), + TypeError(), + ValueError(), + ClientError(), + TimeoutError(), + ], +) +async def test_unload_entry_with_failing_disconnect( + hass: HomeAssistant, + mock_thinq_api: AsyncMock, + mock_config_entry: MockConfigEntry, + exception: Exception, +) -> None: + """Test the entry unloads even when telling LG we are leaving fails.""" + with patch( + "homeassistant.components.lg_thinq.ThinQMQTT.async_connect", + return_value=True, + ): + await setup_integration(hass, mock_config_entry) + + assert mock_config_entry.state is ConfigEntryState.LOADED + + mqtt_client = mock_config_entry.runtime_data.mqtt_client + mqtt_client.client = AsyncMock() + mqtt_client.client.async_disconnect.side_effect = exception + + assert await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + + @pytest.mark.parametrize( "exception", [AttributeError(), TypeError(), ValueError(), ClientError(), TimeoutError()],