Add REST sensor/binary_sensor/switch templated headers & params (#20058)

This commit is contained in:
Chris Browet 2021-11-02 18:47:40 +01:00 committed by GitHub
parent 87963b7694
commit 813be23edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 7 deletions

View File

@ -124,7 +124,11 @@ password:
headers:
description: The headers for the requests.
required: false
type: [list, string]
type: [list, template]
params:
description: The query params for the requests.
required: false
type: [list, template]
{% endconfiguration %}
## Examples
@ -153,7 +157,9 @@ binary_sensor:
### Accessing an HTTP authentication protected endpoint
The REST sensor supports HTTP authentication and customized headers.
The REST sensor supports HTTP authentication and template-enabled customized headers.
{% raw %}
```yaml
binary_sensor:
@ -165,8 +171,12 @@ binary_sensor:
headers:
User-Agent: Home Assistant
Content-Type: application/json
X-Custom-Header: '{{ states("input_text.the_custom_header") }}'
```
{% endraw %}
The headers will contain all relevant details. This will also give
you the ability to access endpoints that are protected by tokens.

View File

@ -151,11 +151,11 @@ password:
headers:
description: The headers for the requests.
required: false
type: [string, list]
type: [list, template]
params:
description: The query params for the requests.
required: false
type: [string, list]
type: [list, template]
sensor:
description: A list of [RESTful Sensor](/integrations/sensor.rest) to create from the shared data. All configuration settings that the supported by [RESTful Sensor](/integrations/sensor.rest#configuration-variables) not listed above can be used here.
required: false

View File

@ -41,6 +41,12 @@ or a template based request:
sensor:
- platform: rest
resource_template: http://IP_ADDRESS/{{ now().strftime('%Y-%m-%d') }}
headers:
Authorization: >
Bearer {{ states("input_text.my_access_token") }}
params:
start_date: >
{{ (now() - timedelta(days = 1)).strftime('%Y-%m-%d') }}
```
{% endraw %}
@ -109,11 +115,11 @@ password:
headers:
description: The headers for the requests.
required: false
type: [string, list]
type: [template, list]
params:
description: The query params for the requests.
required: false
type: [string, list]
type: [template, list]
json_attributes:
description: A list of keys to extract values from a JSON dictionary result and then set as sensor attributes. If the endpoint returns XML with the "text/xml", "application/xml" or "application/xhtml+xml" content type, it will automatically be converted to JSON according to this [specification](https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html)
required: false

View File

@ -74,7 +74,11 @@ password:
headers:
description: The headers for the request.
required: false
type: [string, list]
type: [list, template]
params:
description: The query params for the requests.
required: false
type: [list, template]
verify_ssl:
description: Verify the SSL certificate of the endpoint.
required: false
@ -97,6 +101,7 @@ This example shows a switch that uses a [template](/topics/templating/) to allow
```
{% raw %}
```yaml
switch:
- platform: rest
@ -106,8 +111,10 @@ switch:
is_on_template: "{{ value_json.is_active }}"
headers:
Content-Type: application/json
X-Custom-Header: '{{ states("input_text.the_custom_header") }}'
verify_ssl: true
```
{% endraw %}
`body_on` and `body_off` can also depend on the state of the system. For example, to enable a remote temperature sensor tracking on a radio thermostat, one has to send the current value of the remote temperature sensor. This can be achieved by using the template `{% raw %}'{"rem_temp":{{states('sensor.bedroom_temp')}}}'{% endraw %}`.