Allow templates in rest_command headers (#26099)

This commit is contained in:
Pedro Lamas authored and Paulus Schoutsen committed 2019-11-25 19:39:56 -08:00
1 parent cc255da038
commit db0008e62c
2 files changed
+50 -8

No files matched your search

@@ -37,7 +37,7 @@ COMMAND_SCHEMA = vol.Schema(
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.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.template}),
vol.Inclusive(CONF_USERNAME, "authentication"): cv.string,
vol.Inclusive(CONF_PASSWORD, "authentication"): cv.string,
vol.Optional(CONF_PAYLOAD): cv.template,
@@ -75,15 +75,15 @@ async def async_setup(hass, config):
template_payload = command_config[CONF_PAYLOAD]
template_payload.hass = hass
headers = None
template_headers = None
if CONF_HEADERS in command_config:
headers = command_config[CONF_HEADERS]
template_headers = command_config[CONF_HEADERS]
for template_header in template_headers.values():
template_header.hass = hass
content_type = None
if CONF_CONTENT_TYPE in command_config:
content_type = command_config[CONF_CONTENT_TYPE]
if headers is None:
headers = {}
headers[hdrs.CONTENT_TYPE] = content_type
async def async_service_handler(service):
"""Execute a shell command service."""
@@ -94,6 +94,20 @@ async def async_setup(hass, config):
)
request_url = template_url.async_render(variables=service.data)
headers = None
if template_headers:
headers = {}
for header_name, template_header in template_headers.items():
headers[header_name] = template_header.async_render(
variables=service.data
)
if content_type:
if headers is None:
headers = {}
headers[hdrs.CONTENT_TYPE] = content_type
try:
async with getattr(websession, method)(
request_url,