Skip to content

Commit

Permalink
Update to 1.2.3 (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse authored Mar 20, 2019
2 parents bd48da1 + a4d001b commit 31c2cb9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
57 changes: 45 additions & 12 deletions alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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': {}}
Expand Down
2 changes: 1 addition & 1 deletion custom_components.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 31c2cb9

Please sign in to comment.