# Buggy code
async def update_last_called(login_obj, last_called=None):
    """Update the last called device for the login_obj.

    This will store the last_called in hass.data and also fire an event
    to notify listeners.
    """
    from alexapy import AlexaAPI

    if not last_called:
        last_called = await AlexaAPI.get_last_device_serial(login_obj)
    _LOGGER.debug(
        "%s: Updated last_called: %s", hide_email(email), hide_serial(last_called)
    )
    stored_data = hass.data[DATA_ALEXAMEDIA]["accounts"][email]
    if ("last_called" in stored_data and last_called != stored_data["last_called"]) or (
        "last_called" not in stored_data and last_called is not None
    ):
        _LOGGER.debug(
            "%s: last_called changed: %s to %s",
            hide_email(email),
            hide_serial(
                stored_data["last_called"] if "last_called" in stored_data else None
            ),
            hide_serial(last_called),
        )
        hass.bus.async_fire(
            f"{DOMAIN}_{hide_email(email)}"[0:32],
            {"last_called_change": last_called},
        )
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["last_called"] = last_called


# 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 update_last_called(login_obj, last_called=None):
    """Update the last called device for the login_obj.

    This will store the last_called in hass.data and also fire an event
    to notify listeners.
    """
    from alexapy import AlexaAPI

    if not last_called:
        last_called = await AlexaAPI.get_last_device_serial(login_obj)
    _LOGGER.debug(
        "%s: Updated last_called: %s", hide_email(email), hide_serial(last_called)
    )
    stored_data = hass.data[DATA_ALEXAMEDIA]["accounts"][email]
    if ("last_called" in stored_data and last_called != stored_data["last_called"]) or (
        "last_called" not in stored_data and last_called is not None
    ):
        _LOGGER.debug(
            "%s: last_called changed: %s to %s",
            hide_email(email),
            hide_serial(
                stored_data["last_called"] if "last_called" in stored_data else None
            ),
            hide_serial(last_called),
        )
        async_dispatcher_send(
            hass,
            f"{DOMAIN}_{hide_email(email)}"[0:32],
            {"last_called_change": last_called},
        )
    hass.data[DATA_ALEXAMEDIA]["accounts"][email]["last_called"] = last_called