You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/developers/websocket_api.markdown
+39-39Lines changed: 39 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Home Assistant contains a websocket API. This API can be used to stream informat
13
13
14
14
-[JavaScript](https://github.com/home-assistant/home-assistant-js-websocket) - powers the frontend
15
15
16
-
# Server states
16
+
# {% linkable_title Server states %}
17
17
18
18
1. Client connects
19
19
2. Authentication phase starts
@@ -31,20 +31,20 @@ Home Assistant contains a websocket API. This API can be used to stream informat
31
31
32
32
During the command phase, the client attaches a unique identifier to each message. The server will add this identifier to each message so that the client can link each message to it's origin.
33
33
34
-
# Message format
34
+
# {% linkable_title Message format %}
35
35
36
36
Each API message is a JSON serialized object containing a `type` key. After the authentication phase messages also must contain an `id`, an integer that contains the number of interactions.
37
37
38
38
Example of an auth message:
39
39
40
-
```json5
40
+
```json
41
41
{
42
42
"type": "auth",
43
43
"api_password": "supersecret"
44
44
}
45
45
```
46
46
47
-
```json5
47
+
```json
48
48
{
49
49
"id"5,
50
50
"type":"event",
@@ -57,29 +57,29 @@ Example of an auth message:
57
57
}
58
58
```
59
59
60
-
# Authentication phase
60
+
# {% linkable_title Authentication phase %}
61
61
62
62
When a client connects to the server, the server will test if the client is authenticated. Authentication will not be necessary if no api_password is set or if the user fulfills one of the other criteria for authentication (trusted network, password in url/header).
63
63
64
64
If no authentication is needed, the authentication phase will complete and the server will send an `auth_ok` message.
65
65
66
-
```json5
66
+
```json
67
67
{
68
68
"type": "auth_ok"
69
69
}
70
70
```
71
71
72
72
If authentication is necessary, the server sends out `auth_required`.
73
73
74
-
```json5
74
+
```json
75
75
{
76
76
"type": "auth_required"
77
77
}
78
78
```
79
79
80
80
This means that the next message from the client should be an auth message:
81
81
82
-
```json5
82
+
```json
83
83
{
84
84
"type": "auth",
85
85
"api_password": "supersecret"
@@ -88,26 +88,26 @@ This means that the next message from the client should be an auth message:
88
88
89
89
If the client supplies valid authentication, the authentication phase will complete by the server sending the `auth_ok` message:
90
90
91
-
```json5
91
+
```json
92
92
{
93
93
"type": "auth_ok"
94
94
}
95
95
```
96
96
97
97
If the data is incorrect, the server will reply with `auth_invalid` message and disconnect the session.
98
98
99
-
```json5
99
+
```json
100
100
{
101
101
"type": "auth_invalid",
102
102
"message": "Invalid password"
103
103
}
104
104
```
105
105
106
-
# Command phase
106
+
# {% linkable_title Command phase %}
107
107
108
108
During this phase the client can give commands to the server. The server will respond to each command with a `result` message indicating when the command is done and if it was successful.
109
109
110
-
```json5
110
+
```json
111
111
{
112
112
"id": 6.
113
113
"type": "result",
@@ -117,11 +117,11 @@ During this phase the client can give commands to the server. The server will re
117
117
}
118
118
```
119
119
120
-
## Subscribe to events
120
+
## {% linkable_title Subscribe to events %}
121
121
122
122
The command `subscribe_events` will subscribe your client to the event bus. You can either listen to all events or to a specific event type. If you want to listen to multiple event types, you will have to send multiple `subscribe_events` commands.
123
123
124
-
```json5
124
+
```json
125
125
{
126
126
"id": 18,
127
127
"type": "subscribe_events",
@@ -132,7 +132,7 @@ The command `subscribe_events` will subscribe your client to the event bus. You
132
132
133
133
The server will respond with a result message to indicate that the subscription is active.
134
134
135
-
```json5
135
+
```json
136
136
{
137
137
"id": 18,
138
138
"type": "result",
@@ -143,7 +143,7 @@ The server will respond with a result message to indicate that the subscription
143
143
144
144
For each event that matches, the server will send a message of type `event`. The `id` in the message will point at the original `id` of the `listen_event` command.
145
145
146
-
```json5
146
+
```json
147
147
{
148
148
"id": 18,
149
149
"type":"event",
@@ -190,11 +190,11 @@ For each event that matches, the server will send a message of type `event`. The
190
190
}
191
191
```
192
192
193
-
## Unsubscribing from events
193
+
## {% linkable_title Unsubscribing from events %}
194
194
195
195
You can unsubscribe from previously created subscription events. Pass the id of the original subscription command as value to the subscription field.
196
196
197
-
```json5
197
+
```json
198
198
{
199
199
"id": 19,
200
200
"type": "unsubscribe_events",
@@ -204,7 +204,7 @@ You can unsubscribe from previously created subscription events. Pass the id of
204
204
205
205
The server will respond with a result message to indicate that unsubscribing was successful.
206
206
207
-
```json5
207
+
```json
208
208
{
209
209
"id": 19,
210
210
"type": "result",
@@ -214,7 +214,7 @@ The server will respond with a result message to indicate that unsubscribing was
214
214
```
215
215
216
216
217
-
## Calling a service
217
+
## {% linkable_title Calling a service %}
218
218
219
219
This will call a service in Home Assistant. Right now there is no return value. The client can listen to `state_changed` events if it is interested in changed entities as a result of a service call.
220
220
@@ -233,7 +233,7 @@ This will call a service in Home Assistant. Right now there is no return value.
233
233
234
234
The server will indicate with a message indicating that the service is done executing.
235
235
236
-
```json5
236
+
```json
237
237
{
238
238
"id": 24,
239
239
"type": "result",
@@ -242,11 +242,11 @@ The server will indicate with a message indicating that the service is done exec
242
242
}
243
243
```
244
244
245
-
## Fetching states
245
+
## {% linkable_title Fetching states %}
246
246
247
247
This will get a dump of all the current states in Home Assistant.
248
248
249
-
```json5
249
+
```json
250
250
{
251
251
"id": 19,
252
252
"type": "get_states"
@@ -255,20 +255,20 @@ This will get a dump of all the current states in Home Assistant.
255
255
256
256
The server will respond with a result message containing the states.
257
257
258
-
```json5
258
+
```json
259
259
{
260
260
"id": 19,
261
261
"type": "result",
262
262
"success": true,
263
-
"result": [ … ]
263
+
"result": [ ...]
264
264
}
265
265
```
266
266
267
-
## Fetching config
267
+
## {% linkable_title Fetching config %}
268
268
269
269
This will get a dump of the current config in Home Assistant.
270
270
271
-
```json5
271
+
```json
272
272
{
273
273
"id": 19,
274
274
"type": "get_config"
@@ -277,20 +277,20 @@ This will get a dump of the current config in Home Assistant.
277
277
278
278
The server will respond with a result message containing the config.
279
279
280
-
```json5
280
+
```json
281
281
{
282
282
"id": 19,
283
283
"type": "result",
284
284
"success": true,
285
-
"result": { … }
285
+
"result": { ...}
286
286
}
287
287
```
288
288
289
-
## Fetching services
289
+
## {% linkable_title Fetching services %}
290
290
291
291
This will get a dump of the current services in Home Assistant.
292
292
293
-
```json5
293
+
```json
294
294
{
295
295
"id": 19,
296
296
"type": "get_config"
@@ -299,20 +299,20 @@ This will get a dump of the current services in Home Assistant.
299
299
300
300
The server will respond with a result message containing the services.
301
301
302
-
```json5
302
+
```json
303
303
{
304
304
"id": 19,
305
305
"type": "result",
306
306
"success": true,
307
-
"result": { … }
307
+
"result": { ...}
308
308
}
309
309
```
310
310
311
-
## Fetching panels
311
+
## {% linkable_title Fetching panels %}
312
312
313
313
This will get a dump of the current registered panels in Home Assistant.
314
314
315
-
```json5
315
+
```json
316
316
{
317
317
"id": 19,
318
318
"type": "get_panels"
@@ -321,16 +321,16 @@ This will get a dump of the current registered panels in Home Assistant.
321
321
322
322
The server will respond with a result message containing the current registered panels.
323
323
324
-
```json5
324
+
```json
325
325
{
326
326
"id": 19,
327
327
"type": "result",
328
328
"success": true,
329
-
"result": [ … ]
329
+
"result": [ ...]
330
330
}
331
331
```
332
332
333
-
# Error handling
333
+
# {% linkable_title Error handling %}
334
334
335
335
If an error occurs, the `success` key in the `result` message will be set to `false`. It will contain an `error` key containing an object with two keys: `code` and `message`.
336
336
@@ -340,7 +340,7 @@ If an error occurs, the `success` key in the `result` message will be set to `fa
340
340
| 2 | Received message is not in expected format (voluptuous validation error).
0 commit comments