forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add websocket API (home-assistant#4582)
* Add websocket API * Add identifiers to interactions * Allow unsubscribing event listeners * Add support for fetching data * Clean up handling code websockets api * Lint * Add Home Assistant version to auth messages * Py.test be less verbose in tox
- Loading branch information
Showing
7 changed files
with
831 additions
and
14 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
homeassistant/components/frontend/www_static/websocket_test.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>WebSocket debug</title> | ||
<style> | ||
.controls { | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
|
||
.controls textarea { | ||
height: 160px; | ||
min-width: 400px; | ||
margin-right: 24px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class='controls'> | ||
<textarea id="messageinput"> | ||
{ | ||
"id": 1, "type": "subscribe_events", "event_type": "state_changed" | ||
} | ||
</textarea> | ||
<pre> | ||
Examples: | ||
{ | ||
"id": 2, "type": "subscribe_events", "event_type": "state_changed" | ||
} | ||
|
||
{ | ||
"id": 3, "type": "call_service", "domain": "light", "service": "turn_off" | ||
} | ||
|
||
{ | ||
"id": 4, "type": "unsubscribe_events", "subscription": 2 | ||
} | ||
|
||
{ | ||
"id": 5, "type": "get_states" | ||
} | ||
|
||
{ | ||
"id": 6, "type": "get_config" | ||
} | ||
|
||
{ | ||
"id": 7, "type": "get_services" | ||
} | ||
|
||
{ | ||
"id": 8, "type": "get_panels" | ||
} | ||
</pre> | ||
</div> | ||
<div> | ||
<button type="button" onclick="openSocket();" >Open</button> | ||
<button type="button" onclick="send();" >Send</button> | ||
<button type="button" onclick="closeSocket();" >Close</button> | ||
</div> | ||
<!-- Server responses get written here --> | ||
<pre id="messages"></pre> | ||
|
||
<!-- Script to utilise the WebSocket --> | ||
<script type="text/javascript"> | ||
var webSocket; | ||
var messages = document.getElementById("messages"); | ||
|
||
function openSocket(){ | ||
var isOpen = false; | ||
// Ensures only one connection is open at a time | ||
if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){ | ||
writeResponse("WebSocket is already opened."); | ||
return; | ||
} | ||
// Create a new instance of the websocket | ||
webSocket = new WebSocket("ws://localhost:8123/api/websocket"); | ||
|
||
/** | ||
* Binds functions to the listeners for the websocket. | ||
*/ | ||
webSocket.onopen = function(event){ | ||
if (!isOpen) { | ||
isOpen = true; | ||
writeResponse('Connection opened'); | ||
} | ||
// For reasons I can't determine, onopen gets called twice | ||
// and the first time event.data is undefined. | ||
// Leave a comment if you know the answer. | ||
if(event.data === undefined) | ||
return; | ||
|
||
writeResponse(event.data); | ||
}; | ||
|
||
webSocket.onmessage = function(event){ | ||
writeResponse(event.data); | ||
}; | ||
|
||
webSocket.onclose = function(event){ | ||
writeResponse("Connection closed"); | ||
}; | ||
} | ||
|
||
/** | ||
* Sends the value of the text input to the server | ||
*/ | ||
function send(){ | ||
var text = document.getElementById("messageinput").value; | ||
webSocket.send(text); | ||
} | ||
|
||
function closeSocket(){ | ||
webSocket.close(); | ||
} | ||
|
||
function writeResponse(text){ | ||
messages.innerHTML += "\n" + text; | ||
} | ||
|
||
openSocket(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.