-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support/Implement WebSocket events for State Changes #119
Comments
I'm not too familiar with WebSockets. Will this give us push functionality so we can avoid polling? |
That's the goal, looks like amazon is pushing events over web sockets for:
So in theory this could eliminate all polling. |
Looks like this is absolutely possible and the messages can be properly decoded with little effort. Here is the output from a sample program I wrote. Looks like each time that someone talks to an echo device a message is pushed to connected WebSocket clients. This is the "PUSH_ACTIVITY" you see in the screenshot. You can also see other push messages for different messages. Each message does contain the deviceID that is being affected by the player state change or the device that initiated the activity. Getting PUSH_ACTIVITY can be used to gather information from the last_called alexa device and other messages can directly update media players or give us a hint on when to fetch/pull information like track, playlist, etc when things change. @alandtse what do you think? |
This looks good. Can you implement websockets in alexapi in a parallel set of functions so we can fallback to the http? Or does this require switching to a purely async model? I'm not planning to do any major work in the API for a little bit if you need it stable. |
Currently I want to do as minimal changes as possible. Specifically, we can instantiate a WebSocket class within the device alexa_setup() method and simply get a callback for messages from the WebSocket code. This will be entirely one way, only digesting events over the WebSocket and not sending any commands to amazon, those will still be handled with the http RESTful calls. Basically, once we have a callback with a particular message, when can use the same mechanism that is used to update the devices with the last called to fire an HA event that can be consumed by the individual media players to update their state. |
I agree we can use WS for listen only as that's the biggest issue to solve. So we're on the same page, the current architecture has HA Is your thought that during I think that should work, but wonder if architecturally it makes sense to define a websocket object outside alexaapi. Or perhaps I'm misunderstanding you on that point. |
@keatontaylor alternatively, if you share the sample program code, I may have some time to integrate it in this weekend. |
@alandtse uploading it as a new class in a branch on GitLab once I get the code cleaned up a bit. Look for it in an hour or so. |
Oh awesome! Looking forward to it. |
Not perfect, but should work: https://gitlab.com/keatontaylor/alexapy/tree/websocket-notify Create an instance of WebSocket_EchoClient with the login session, and a python function you'd like to be called when a message is received. The Message and Content class describe the contents of each message. |
@keatontaylor I think there's something wrong with your cookie building routine. You're not actually using for cookie in self._cookies:
cookies += cookies + "; "
cookies = "Cookie: " + cookie
url += url + str(cookies['ubid-main'])
url += "-" + str(int(time.time())) + "000" The initial error is that EDIT: Looks like urls should be fixed too. cookies = "dp-gw-na-js "
for cookie in self._cookies:
cookies += cookie + "; "
cookies = "Cookie: " + cookies
url += str(self._cookies['ubid-main']) Note, I blanked out my ubid-main value below, but otherwise untouched.
|
The error is caused by the |
I'll have to take a closer look at this, I didn't immediately test with a valid cookie once in the class, but will. |
Do you have a spec or example you're using for how to build the handshake? |
Branch on gitlab updated. handshake and message parsing is pulled from the OpenHAB Alexa component: https://github.com/openhab/openhab2-addons/blob/master/addons/binding/org.openhab.binding.amazonechocontrol/src/main/java/org/openhab/binding/amazonechocontrol/internal/WebSocketConnection.java |
Ok, I think I have a working version. Will probably push it with some other updates after some more testing. |
Currently amazon supports WebSockets for sending events on certain state changes. This includes media player play/pause/volume/etc events along with last-triggered echo events when a request is made.
This would be extremely valuable for getting real-time dynamic updates based on state changes within the Alexa echo-system and should be reasonably straight forward to implement now that we have the Auth token from a successful login.
I'm going to play around with this and report back. If successful even if we cannot decode the inner WebSocket payload, an event over WebSockets should be sufficient for us to trigger an update.
The text was updated successfully, but these errors were encountered: