Skip to content

Commit

Permalink
Clarify cloud error (#12540)
Browse files Browse the repository at this point in the history
* Clarify cloud error

* Fix tests
  • Loading branch information
balloob authored and pvizeli committed Feb 20, 2018
1 parent 17bdcac commit 39847ea
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
3 changes: 0 additions & 3 deletions homeassistant/components/cloud/auth_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""Package to communicate with the authentication API."""
import logging

_LOGGER = logging.getLogger(__name__)


class CloudError(Exception):
Expand Down
12 changes: 6 additions & 6 deletions homeassistant/components/cloud/iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def _handle_hass_stop(event):
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:
Expand Down Expand Up @@ -131,8 +129,8 @@ def _handle_hass_stop(event):
_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:
Expand All @@ -150,7 +148,9 @@ def _handle_hass_stop(event):
_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:
Expand Down
8 changes: 4 additions & 4 deletions tests/components/cloud/test_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Expand Down Expand Up @@ -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])


Expand Down

0 comments on commit 39847ea

Please sign in to comment.