# Buggy code
async def ws_close_handler():
    """Handle websocket close.

    This should attempt to reconnect up to 5 times
    """
    from asyncio import sleep
    import time

    email: Text = login_obj.email
    errors: int = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    delay: int = 5 * 2**errors
    last_attempt = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
        "websocket_lastattempt"
    ]
    now = time.time()
    if (now - last_attempt) < delay:
        return
    while (
        errors < 5 and not (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"])
    ):
        _LOGGER.debug(
            "%s: Websocket closed; reconnect #%i in %is",
            hide_email(email),
            errors,
            delay,
        )
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket_lastattempt"] = (
            time.time()
        )
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = await ws_connect()
        errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = (
            hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] + 1
        )
        delay = 5 * 2**errors
        await sleep(delay)
        errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    _LOGGER.debug("%s: Websocket closed; retries exceeded; polling", hide_email(email))
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
    await update_devices(  # pylint: disable=unexpected-keyword-arg
        login_obj, no_throttle=True
    )


# Traceback (ValueError)
2019-10-26 14:43:05 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry ***REDACTED*** - amazon.com for media_player
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 190, in async_setup
hass, self
File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 340, in async_setup_entry
return await hass.data[DOMAIN].async_setup_entry(entry)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 151, in async_setup_entry
raise ValueError("Config entry has already been setup!")
ValueError: Config entry has already been setup!
2019-10-26 14:43:05 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry ***REDACTED*** - amazon.com for alarm_control_panel
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 190, in async_setup
hass, self
File "/usr/src/homeassistant/homeassistant/components/alarm_control_panel/__init__.py", line 74, in async_setup_entry
return await hass.data[DOMAIN].async_setup_entry(entry)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 151, in async_setup_entry
raise ValueError("Config entry has already been setup!")
ValueError: Config entry has already been setup!
2019-10-26 14:43:05 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry ***REDACTED*** - amazon.com for switch
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 190, in async_setup
hass, self
File "/usr/src/homeassistant/homeassistant/components/switch/__init__.py", line 79, in async_setup_entry
return await hass.data[DOMAIN].async_setup_entry(entry)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 151, in async_setup_entry
raise ValueError("Config entry has already been setup!")
ValueError: Config entry has already been setup!
2019-10-26 14:43:05 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry ***REDACTED*** - amazon.com for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 190, in async_setup
hass, self
File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 60, in async_setup_entry
return await hass.data[DOMAIN].async_setup_entry(entry)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 151, in async_setup_entry
raise ValueError("Config entry has already been setup!")
ValueError: Config entry has already been setup!**System details**```

# Fixed code
async def ws_close_handler():
    """Handle websocket close.

    This should attempt to reconnect up to 5 times
    """
    from asyncio import sleep
    import time

    email: Text = login_obj.email
    errors: int = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    delay: int = 5 * 2**errors
    last_attempt = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
        "websocket_lastattempt"
    ]
    now = time.time()
    if (now - last_attempt) < delay:
        return
    while (
        errors < 5 and not (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"])
    ):
        _LOGGER.debug(
            "%s: Websocket closed; reconnect #%i in %is",
            hide_email(email),
            errors,
            delay,
        )
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket_lastattempt"] = (
            time.time()
        )
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = await ws_connect()
        errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] = (
            hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"] + 1
        )
        delay = 5 * 2**errors
        await sleep(delay)
        errors = hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocketerror"]
    _LOGGER.debug("%s: Websocket closed; retries exceeded; polling", hide_email(email))
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["websocket"] = None
    coordinator = hass.data[DATA_ALEXAMEDIA]["accounts"][email].get("coordinator")
    if coordinator:
        coordinator.update_interval = scan_interval
        await coordinator.async_request_refresh()