From 39847ea651c438a819f52d6abaf6bbc63da4e67e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 20 Feb 2018 03:31:43 -0800 Subject: [PATCH] Clarify cloud error (#12540) * Clarify cloud error * Fix tests --- homeassistant/components/cloud/auth_api.py | 3 --- homeassistant/components/cloud/iot.py | 12 ++++++------ tests/components/cloud/test_iot.py | 8 ++++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/cloud/auth_api.py b/homeassistant/components/cloud/auth_api.py index c7679c0f262..118a9857158 100644 --- a/homeassistant/components/cloud/auth_api.py +++ b/homeassistant/components/cloud/auth_api.py @@ -1,7 +1,4 @@ """Package to communicate with the authentication API.""" -import logging - -_LOGGER = logging.getLogger(__name__) class CloudError(Exception): diff --git a/homeassistant/components/cloud/iot.py b/homeassistant/components/cloud/iot.py index 2d3ab025e43..5f61263824b 100644 --- a/homeassistant/components/cloud/iot.py +++ b/homeassistant/components/cloud/iot.py @@ -90,9 +90,7 @@ class CloudIoT: while not client.closed: msg = yield from client.receive() - if msg.type in (WSMsgType.ERROR, WSMsgType.CLOSED, - WSMsgType.CLOSING): - disconnect_warn = 'Connection cancelled.' + if msg.type in (WSMsgType.CLOSED, WSMsgType.CLOSING): break elif msg.type != WSMsgType.TEXT: @@ -131,8 +129,8 @@ class CloudIoT: _LOGGER.debug("Publishing message: %s", response) yield from client.send_json(response) - except auth_api.CloudError: - _LOGGER.warning("Unable to connect: Unable to refresh token.") + except auth_api.CloudError as err: + _LOGGER.warning("Unable to connect: %s", err) except client_exceptions.WSServerHandshakeError as err: if err.code == 401: @@ -150,7 +148,9 @@ class CloudIoT: _LOGGER.exception("Unexpected error") finally: - if disconnect_warn is not None: + if disconnect_warn is None: + _LOGGER.info("Connection closed") + else: _LOGGER.warning("Connection closed: %s", disconnect_warn) if remove_hass_stop_listener is not None: diff --git a/tests/components/cloud/test_iot.py b/tests/components/cloud/test_iot.py index 53340ecede1..ff382b697cf 100644 --- a/tests/components/cloud/test_iot.py +++ b/tests/components/cloud/test_iot.py @@ -162,7 +162,7 @@ def test_cloud_getting_disconnected_by_server(mock_client, caplog, mock_cloud): yield from conn.connect() - assert 'Connection closed: Connection cancelled.' in caplog.text + assert 'Connection closed' in caplog.text assert 'connect' in str(mock_cloud.hass.async_add_job.mock_calls[-1][1][0]) @@ -197,13 +197,13 @@ def test_cloud_sending_invalid_json(mock_client, caplog, mock_cloud): @asyncio.coroutine def test_cloud_check_token_raising(mock_client, caplog, mock_cloud): - """Test cloud sending invalid JSON.""" + """Test cloud unable to check token.""" conn = iot.CloudIoT(mock_cloud) - mock_client.receive.side_effect = auth_api.CloudError + mock_client.receive.side_effect = auth_api.CloudError("BLA") yield from conn.connect() - assert 'Unable to connect: Unable to refresh token.' in caplog.text + assert 'Unable to connect: BLA' in caplog.text assert 'connect' in str(mock_cloud.hass.async_add_job.mock_calls[-1][1][0])