# Buggy code
def ws_handler(message_obj):
    """Handle websocket messages.

    This allows push notifications from Alexa to update last_called
    and media state.
    """
    command = (
        message_obj.json_payload["command"]
        if isinstance(message_obj.json_payload, dict)
        and "command" in message_obj.json_payload
        else None
    )
    json_payload = (
        message_obj.json_payload["payload"]
        if isinstance(message_obj.json_payload, dict)
        and "payload" in message_obj.json_payload
        else None
    )
    if command and json_payload:
        _LOGGER.debug(
            "%s: Received websocket command: %s : %s",
            hide_email(email),
            command,
            json_payload,
        )
        if command == "PUSH_ACTIVITY":
            #  Last_Alexa Updated
            last_called = {
                "serialNumber": (json_payload["key"]["entryId"]).split("#")[2],
                "timestamp": json_payload["timestamp"],
            }
            update_last_called(login_obj, last_called)
        elif command == "PUSH_AUDIO_PLAYER_STATE":
            # Player update
            _LOGGER.debug("Updating media_player: %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )
        elif command == "PUSH_VOLUME_CHANGE":
            # Player volume update
            _LOGGER.debug("Updating media_player volume: %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )
        elif command == "PUSH_DOPPLER_CONNECTION_CHANGE":
            # Player volume update
            _LOGGER.debug("Updating media_player avalibility %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )


# Traceback (homeassistant.exceptions.HomeAssistantError)
2019-03-14 05:14:03 ERROR (SyncWorker_12) [alexapy.alexaapi] An error occured accessing AlexaAPI: An exception of type ConnectionError occurred. Arguments:
(ProtocolError('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),)
2019-03-14 05:14:09 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/Users/Media/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 344, in _async_add_entity
raise HomeAssistantError(msg)
homeassistant.exceptions.HomeAssistantError: Entity id already exists: media_player.ethan_s_echo. Platform alexa_media does not generate unique IDs
2019-03-14 05:14:09 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/Users/Media/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 344, in _async_add_entity
raise HomeAssistantError(msg)
homeassistant.exceptions.HomeAssistantError: Entity id already exists: media_player.master_bedroom. Platform alexa_media does not generate unique IDs

# Fixed code
def ws_handler(message_obj):
    """Handle websocket messages.

    This allows push notifications from Alexa to update last_called
    and media state.
    """
    command = (
        message_obj.json_payload["command"]
        if isinstance(message_obj.json_payload, dict)
        and "command" in message_obj.json_payload
        else None
    )
    json_payload = (
        message_obj.json_payload["payload"]
        if isinstance(message_obj.json_payload, dict)
        and "payload" in message_obj.json_payload
        else None
    )
    if command and json_payload:
        _LOGGER.debug(
            "%s: Received websocket command: %s : %s",
            hide_email(email),
            command,
            json_payload,
        )
        serial = None
        if command == "PUSH_ACTIVITY":
            #  Last_Alexa Updated
            serial = (json_payload["key"]["entryId"]).split("#")[2]
            last_called = {
                "serialNumber": serial,
                "timestamp": json_payload["timestamp"],
            }
            update_last_called(login_obj, last_called)
        elif command == "PUSH_AUDIO_PLAYER_STATE":
            # Player update
            serial = json_payload["dopplerId"]["deviceSerialNumber"]
            _LOGGER.debug("Updating media_player: %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )
        elif command == "PUSH_VOLUME_CHANGE":
            # Player volume update
            serial = json_payload["dopplerId"]["deviceSerialNumber"]
            _LOGGER.debug("Updating media_player volume: %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )
        elif command == "PUSH_DOPPLER_CONNECTION_CHANGE":
            # Player availability update
            serial = json_payload["dopplerId"]["deviceSerialNumber"]
            _LOGGER.debug("Updating media_player availability %s", json_payload)
            hass.bus.fire(
                ("{}_{}".format(DOMAIN, hide_email(email)))[0:32],
                {"player_state": json_payload},
            )
        if serial and serial not in (
            hass.data[DATA_ALEXAMEDIA]["accounts"][email]["entities"][
                "media_player"
            ].keys()
        ):
            _LOGGER.debug("Discovered new media_player %s", serial)
            (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["new_devices"]) = True
            update_devices(no_throttle=True)