Add custom header support for rest_command (#12646)

* Add support for specifying custom headers for rest_command.
* Added headers configuration to behave similarly to the rest sensor.
* Replaced test_rest_command_content_type which only validated the
  configuration with test_rest_command_headers which tests several
  combinations of parameters that affect the request headers.
This commit is contained in:
James Marsh 2018-02-28 06:16:31 +00:00 committed by Johann Kellerman
parent f5c633415d
commit f6c504610b
3 changed files with 82 additions and 15 deletions

View file

@ -14,7 +14,7 @@ import voluptuous as vol
from homeassistant.const import (
CONF_TIMEOUT, CONF_USERNAME, CONF_PASSWORD, CONF_URL, CONF_PAYLOAD,
CONF_METHOD)
CONF_METHOD, CONF_HEADERS)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
@ -38,6 +38,7 @@ COMMAND_SCHEMA = vol.Schema({
vol.Required(CONF_URL): cv.template,
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
vol.All(vol.Lower, vol.In(SUPPORT_REST_METHODS)),
vol.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.string}),
vol.Inclusive(CONF_USERNAME, 'authentication'): cv.string,
vol.Inclusive(CONF_PASSWORD, 'authentication'): cv.string,
vol.Optional(CONF_PAYLOAD): cv.template,
@ -77,9 +78,14 @@ def async_setup(hass, config):
template_payload.hass = hass
headers = None
if CONF_HEADERS in command_config:
headers = command_config[CONF_HEADERS]
if CONF_CONTENT_TYPE in command_config:
content_type = command_config[CONF_CONTENT_TYPE]
headers = {hdrs.CONTENT_TYPE: content_type}
if headers is None:
headers = {}
headers[hdrs.CONTENT_TYPE] = content_type
@asyncio.coroutine
def async_service_handler(service):