Skip to content

Commit 2d47cd7

Browse files
committed
Merge pull request #191 from fabaff/http
HTTP binary sensor and sensor docs
2 parents 567840e + c5aaa22 commit 2d47cd7

File tree

5 files changed

+145
-5
lines changed

5 files changed

+145
-5
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
layout: component
3+
title: "HTTP Binary Sensor"
4+
description: "Instructions how to integrate HTTP binary sensors within Home Assistant."
5+
date: 2016-02-05 12:15
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
logo: http.png
11+
ha_category: Binary Sensor
12+
---
13+
14+
The URL for a binary sensor looks like the example below:
15+
16+
```bash
17+
http://IP_ADDRESS:8123/api/states/binary_sensor.DEVICE_NAME
18+
```
19+
20+
<p class='note'>
21+
You should choose a unique device name (DEVICE_NAME) to avoid clashes with other devices.
22+
</p>
23+
24+
The JSON payload must contain the new state and can have a friendly name. The friendly name is used in the frontend to name the sensor.
25+
26+
```json
27+
{"state": "on", "attributes": {"friendly_name": "Radio"}}
28+
```
29+
30+
For a quick test `curl` can be useful to "simulate" a device.
31+
32+
```bash
33+
$ curl -X POST -H "x-ha-access: YOUR_PASSWORD" \
34+
-d '{"state": "off", "attributes": {"friendly_name": "Radio"}}' \
35+
http://localhost:8123/api/states/binary_sensor.radio
36+
```
37+
38+
To check if the sensor is working, use again `curl` to retrieve the [current state](/developers/rest_api/#get-apistatesltentity_id).
39+
40+
```bash
41+
$ curl -X GET -H "x-ha-access: YOUR_PASSWORD" \
42+
http://localhost:8123/api/states/binary_sensor.radio
43+
{
44+
"attributes": {
45+
"friendly_name": "Radio"
46+
},
47+
"entity_id": "binary_sensor.radio",
48+
"last_changed": "16:45:51 05-02-2016",
49+
"last_updated": "16:45:51 05-02-2016",
50+
"state": "off"
51+
}
52+
```
53+
54+
## {% linkable_title Examples %}
55+
56+
In this section you find some real life examples of how to use this sensor. Beside `curl`.
57+
58+
### {% linkable_title Using Python request module %}
59+
60+
As already shown on the [API](/developers/rest_api/) page, it's very simple to use Python and the [Requests](http://docs.python-requests.org/en/latest/) module for the interaction with Home Assistant.
61+
62+
```python
63+
response = requests.post(
64+
'http://localhost:8123/api/states/binary_sensor.radio',
65+
headers={'x-ha-access': 'YOUR_PASSWORD', 'content-type': 'application/json'},
66+
data=json.dumps({'state': 'on', 'attributes': {'friendly_name': 'Radio'}}))
67+
print(response.text)
68+
```
69+
70+
### {% linkable_title Using `httpie` %}
71+
72+
[`httpie`](https://github.com/jkbrzt/httpie) is a user-friendly CLI HTTP client.
73+
74+
```bash
75+
$ http -v POST http://localhost:8123/api/states/binary_sensor.radio \
76+
x-ha-access:YOUR_PASSWORD state=off \
77+
attributes:='{"friendly_name": "Radio"}'
78+
```

source/_components/http.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ http:
2525
2626
Configuration variables:
2727
28-
- **api_password** (*Optional*): Protect Home Assistant with a password
28+
- **api_password** (*Optional*): Protect Home Assistant with a password.
2929
- **server_port** (*Optional*): Let you set a port to use. Defaults to 8123.
3030
- **development** (*Optional*): Disable caching and load unvulcanized assets. Useful for Frontend development.
3131
- **ssl_certificate** (*Optional*): Path to your TLS/SSL certificate to serve Home Assistant over a secure connection.
3232
- **ssl_key** (*Optional*): Path to your TLS/SSL key to serve Home Assistant over a secure connection.
3333
34+
On top of the `http` component is a [REST API](/developers/rest_api/) and a [Python API](/developers/python_api/) available.
35+
36+
The `http` platforms are not a real platform within the meaning of the terminology used around Home Assistant. Home Assistant's [REST API](/developers/rest_api/) is consuming and proceeding messages received over HTTP.
37+
38+
To use those kind of sensors in your installation no configuration in Home Assistant is needed. All configuration is done on the devices themselves. This means that you must be able to edit the target URL or endpoint and the payload. The entity will be created after the first message has arrived.
39+
40+
All [requests](/developers/rest_api/#post-apistatesltentity_id) needs to be sent to the endpoint of the device and must be **POST**.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
layout: component
3+
title: "HTTP Sensor"
4+
description: "Instructions how to integrate HTTP sensors within Home Assistant."
5+
date: 2016-02-05 12:15
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
logo: http.png
11+
ha_category: Sensor
12+
---
13+
14+
The URL for a sensor looks like the example below:
15+
16+
```bash
17+
http://IP_ADDRESS:8123/api/states/sensor.DEVICE_NAME
18+
```
19+
20+
<p class='note'>
21+
You should choose a unique device name (DEVICE_NAME) to avoid clashes with other devices.
22+
</p>
23+
24+
The JSON payload must contain the new state and should include the unit of measurement and a friendly name. The friendly name is used in the frontend to name the sensor.
25+
26+
```json
27+
{"state": "20", "attributes": {"unit_of_measurement": "°C", "friendly_name": "Bathroom Temperature"}}
28+
```
29+
30+
For a quick test `curl` can be useful to "simulate" a device.
31+
32+
```bash
33+
$ curl -XPOST -H "x-ha-access: YOUR_PASSWORD" \
34+
-d '{"state": "20", "attributes": {"unit_of_measurement": "°C", "friendly_name": "Bathroom Temp"}}' \
35+
http://localhost:8123/api/states/sensor.bathroom_temperature
36+
```
37+
38+
Use again `curl` to retrieve the [current state](/developers/rest_api/#get-apistatesltentity_id) to check if the sensor is working.
39+
40+
```bash
41+
$ curl -X GET -H "x-ha-access: YOUR_PASSWORD" \
42+
http://localhost:8123/api/states/sensor.bathroom_temperature
43+
{
44+
"attributes": {
45+
"friendly_name": "Bathroom Temp",
46+
"unit_of_measurement": "\u00b0C"
47+
},
48+
"entity_id": "sensor.bathroom_temperature",
49+
"last_changed": "09:46:17 06-02-2016",
50+
"last_updated": "09:48:46 06-02-2016",
51+
"state": "20"
52+
}
53+
```
54+
55+
For more examples please visit the [HTTP Binary Sensor](/components/binary_sensor.http/#examples) page.

source/developers/api.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ footer: true
1212
Home Assistant is offering a RESTful API and a Python API for convenient access to
1313
a Home Assistant instance over HTTP.
1414

15-
- [Rest API](/developers/rest_api/)
15+
- [RESTful API](/developers/rest_api/)
1616
- [Python API](/developers/python_api/)

source/developers/rest_api.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: page
3-
title: "Rest API"
4-
description: "Home Assistant Rest API documentation"
3+
title: "RESTful API"
4+
description: "Home Assistant RESTful API documentation"
55
date: 2014-12-21 13:27
66
sidebar: false
77
comments: false
@@ -14,7 +14,7 @@ Home Assistant runs a web server accessible on port 8123.
1414
* http://IP_ADDRESS:8123/ is an interface to control Home Assistant.
1515
* http://IP_ADDRESS:8123/api/ is a Rest API.
1616

17-
The API accepts and returns only JSON encoded objects. All API calls have to be accompanied by the header `X-HA-Access: YOUR_PASSWORD` (YOUR_PASSWORD as specified in your `configuration.yaml` file).
17+
The API accepts and returns only JSON encoded objects. All API calls have to be accompanied by the header `X-HA-Access: YOUR_PASSWORD` (YOUR_PASSWORD as specified in your `configuration.yaml` file in the [`http:` section](/components/http/)).
1818

1919
There are multiple ways to consume the Home Assistant Rest API. One is with `curl`:
2020

0 commit comments

Comments
 (0)