# Buggy code
async def ws_error_handler(message):
    """Handle websocket error.

    This currently logs the error.  In the future, this should invalidate
    the websocket and determine if a reconnect should be done. By
    specification, websockets will issue a close after every error.
    """
    email: Text = login_obj.email
    errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    _LOGGER.debug(
        "%s: Received websocket error #%i %s: type %s",
        hide_email(email),
        errors,
        message,
        type(message),
    )
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
    if message == "<class 'aiohttp.streams.EofStream'>":
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = 5
        _LOGGER.debug("%s: Immediate abort on EoFstream", hide_email(email))
        return
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = errors + 1


# 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_error_handler(message):
    """Handle websocket error.

    This currently logs the error.  In the future, this should invalidate
    the websocket and determine if a reconnect should be done. By
    specification, websockets will issue a close after every error.
    """
    email: Text = login_obj.email
    errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    _LOGGER.debug(
        "%s: Received websocket error #%i %s: type %s",
        hide_email(email),
        errors,
        message,
        type(message),
    )
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
    if login_obj.session.closed or message == "<class 'aiohttp.streams.EofStream'>":
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = 5
        _LOGGER.debug("%s: Immediate abort on EoFstream", hide_email(email))
        return
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = errors + 1