From 813be23edfd68e01be3d117655979b965dda3a0c Mon Sep 17 00:00:00 2001 From: Chris Browet Date: Tue, 2 Nov 2021 18:47:40 +0100 Subject: [PATCH] Add REST sensor/binary_sensor/switch templated headers & params (#20058) --- source/_integrations/binary_sensor.rest.markdown | 14 ++++++++++++-- source/_integrations/rest.markdown | 4 ++-- source/_integrations/sensor.rest.markdown | 10 ++++++++-- source/_integrations/switch.rest.markdown | 9 ++++++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/source/_integrations/binary_sensor.rest.markdown b/source/_integrations/binary_sensor.rest.markdown index 2575ee740de..6e74ad12d7d 100644 --- a/source/_integrations/binary_sensor.rest.markdown +++ b/source/_integrations/binary_sensor.rest.markdown @@ -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. diff --git a/source/_integrations/rest.markdown b/source/_integrations/rest.markdown index 0e399f5d6e4..7e1dde07cd4 100644 --- a/source/_integrations/rest.markdown +++ b/source/_integrations/rest.markdown @@ -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 diff --git a/source/_integrations/sensor.rest.markdown b/source/_integrations/sensor.rest.markdown index 4ff4d2e04b5..4fc669e761f 100644 --- a/source/_integrations/sensor.rest.markdown +++ b/source/_integrations/sensor.rest.markdown @@ -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 diff --git a/source/_integrations/switch.rest.markdown b/source/_integrations/switch.rest.markdown index 87784976f55..1c85b923508 100644 --- a/source/_integrations/switch.rest.markdown +++ b/source/_integrations/switch.rest.markdown @@ -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 %}`.