# Buggy code
async def ws_connect() -> WebsocketEchoClient:
    """Open WebSocket connection.

    This will only attempt one login before failing.
    """
    websocket: Optional[WebsocketEchoClient] = None
    try:
        websocket = WebsocketEchoClient(
            login_obj,
            ws_handler,
            ws_open_handler,
            ws_close_handler,
            ws_error_handler,
        )
        _LOGGER.debug("%s: Websocket created: %s", hide_email(email), websocket)
        await websocket.async_run()
    except BaseException as exception_:  # pylint: disable=broad-except
        _LOGGER.debug(
            "%s: Websocket creation failed: %s", hide_email(email), exception_
        )
        return
    return websocket


# Traceback (RuntimeError)
2020-04-27 13:53:41 ERROR (MainThread) [custom_components.alexa_media] Unexpected error fetching alexa_media data: Session is closed
Traceback (most recent call last):
File "/config/custom_components/alexa_media/__init__.py", line 277, in async_update_data
) = await asyncio.gather(*tasks)
File "/usr/local/lib/python3.7/site-packages/alexapy/alexaapi.py", line 680, in get_devices
"get", login, "/api/devices-v2/device", query=None
File "/usr/local/lib/python3.7/site-packages/alexapy/alexaapi.py", line 167, in _static_request
ssl=login._ssl,
File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 357, in _request
raise RuntimeError('Session is closed')
RuntimeError: Session is closed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 136, in async_refresh
self.data = await self._async_update_data()
File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 124, in _async_update_data
return await self.update_method()
File "/config/custom_components/alexa_media/__init__.py", line 294, in async_update_data
await login_obj.login_with_cookie()
File "/usr/local/lib/python3.7/site-packages/alexapy/alexalogin.py", line 168, in login_with_cookie
await self.login(cookies=self._cookies)
File "/usr/local/lib/python3.7/site-packages/alexapy/alexalogin.py", line 333, in login
if cookies and await self.test_loggedin(cookies):
File "/usr/local/lib/python3.7/site-packages/alexapy/alexalogin.py", line 238, in test_loggedin
self._prefix + self._url + "/api/bootstrap", cookies=cookies, ssl=self._ssl
File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 357, in _request
raise RuntimeError('Session is closed')
RuntimeError: Session is closed

# Fixed code
async def ws_connect() -> WebsocketEchoClient:
    """Open WebSocket connection.

    This will only attempt one login before failing.
    """
    websocket: Optional[WebsocketEchoClient] = None
    try:
        if login_obj.session.closed:
            _LOGGER.debug(
                "%s: Websocket creation aborted. Session is closed.",
                hide_email(email),
            )
            return
        websocket = WebsocketEchoClient(
            login_obj,
            ws_handler,
            ws_open_handler,
            ws_close_handler,
            ws_error_handler,
        )
        _LOGGER.debug("%s: Websocket created: %s", hide_email(email), websocket)
        await websocket.async_run()
    except BaseException as exception_:  # pylint: disable=broad-except
        _LOGGER.debug(
            "%s: Websocket creation failed: %s", hide_email(email), exception_
        )
        return
    return websocket