diff --git a/alexa_media/__init__.py b/alexa_media/__init__.py index 8adfc125..cf54988a 100644 --- a/alexa_media/__init__.py +++ b/alexa_media/__init__.py @@ -26,9 +26,9 @@ # from .config_flow import configured_instances -REQUIREMENTS = ['alexapy==0.4.0'] +REQUIREMENTS = ['alexapy==0.4.2'] -__version__ = '1.2.2' +__version__ = '1.2.3' _LOGGER = logging.getLogger(__name__) @@ -356,6 +356,26 @@ def last_call_handler(call): login_obj = account_dict['login_obj'] update_last_called(login_obj) + def ws_connect(): + """Open WebSocket connection. + + This will only attempt one login before failing. + """ + from alexapy import WebsocketEchoClient + try: + websocket = WebsocketEchoClient(login_obj, + ws_handler, + ws_close_handler, + ws_error_handler) + _LOGGER.debug("%s: Websocket created: %s", hide_email(email), + websocket) + except BaseException as exception_: + _LOGGER.debug("%s: Websocket failed: %s falling back to polling", + hide_email(email), + exception_) + websocket = None + return websocket + def ws_handler(message_obj): """Handle websocket messages. @@ -396,20 +416,33 @@ def ws_handler(message_obj): hide_email(email)))[0:32], {'player_state': json_payload}) + def ws_close_handler(): + """Handle websocket close. + + This should attempt to reconnect. + """ + email = login_obj.email + _LOGGER.debug("%s: Received websocket close; attempting reconnect", + hide_email(email)) + (hass.data[DOMAIN]['accounts'][email]['websocket']) = ws_connect() + + 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 = login_obj.email + _LOGGER.debug("%s: Received websocket error %s", + hide_email(email), + message) + (hass.data[DOMAIN]['accounts'][email]['websocket']) = None include = config.get(CONF_INCLUDE_DEVICES) exclude = config.get(CONF_EXCLUDE_DEVICES) scan_interval = config.get(CONF_SCAN_INTERVAL) email = login_obj.email - from alexapy import WebsocketEchoClient - try: - websocket = WebsocketEchoClient(login_obj, ws_handler) - _LOGGER.debug("%s: Websocket created: %s", hide_email(email), - websocket) - except BaseException as exception_: - _LOGGER.exception("%s: Websocket failed: %s", hide_email(email), - exception_) - websocket = None - (hass.data[DOMAIN]['accounts'][email]['websocket']) = websocket + (hass.data[DOMAIN]['accounts'][email]['websocket']) = ws_connect() (hass.data[DOMAIN]['accounts'][email]['login_obj']) = login_obj (hass.data[DOMAIN]['accounts'][email]['devices']) = {'media_player': {}} (hass.data[DOMAIN]['accounts'][email]['entities']) = {'media_player': {}} diff --git a/custom_components.json b/custom_components.json index cf7a0521..e17839ac 100644 --- a/custom_components.json +++ b/custom_components.json @@ -1,6 +1,6 @@ { "alexa_media": { - "version": "1.2.2", + "version": "1.2.3", "local_location": "/custom_components/alexa_media/__init__.py", "remote_location": "https://raw.githubusercontent.com/keatontaylor/alexa_media_player/master/alexa_media/__init__.py", "visit_repo": "https://github.com/keatontaylor/alexa_media_player",