Fix rest_command when server is unreachable (#26948)

* fix rest_command when server is unreachable

When a server doesn't exist, the connection fails immediately, rather
than waiting for a timeout. This means that the async handler is never
reached, and the request variable never filled, yet it's used in the
client error exception handler, so this one bugs out.

By using the command_config, we avoid using the potentially unassigned
request variable, avoiding this problem.

This patch makes scripts work that have a rest_command in them which
fails due to a server being offline.

* render template_url

instead of printing the template object

* fix formatting

* fix format using black

* only render url once

* blacken...
This commit is contained in:
Sebastian Kügler 2019-09-30 09:06:10 +02:00 committed by Pascal Vizeli
parent 43bd116852
commit c527e0f164

View File

@ -94,13 +94,11 @@ async def async_setup(hass, config):
template_payload.async_render(variables=service.data), "utf-8" template_payload.async_render(variables=service.data), "utf-8"
) )
request_url = template_url.async_render(variables=service.data)
try: try:
with async_timeout.timeout(timeout): with async_timeout.timeout(timeout):
request = await getattr(websession, method)( request = await getattr(websession, method)(
template_url.async_render(variables=service.data), request_url, data=payload, auth=auth, headers=headers
data=payload,
auth=auth,
headers=headers,
) )
if request.status < 400: if request.status < 400:
@ -112,7 +110,7 @@ async def async_setup(hass, config):
_LOGGER.warning("Timeout call %s.", request.url) _LOGGER.warning("Timeout call %s.", request.url)
except aiohttp.ClientError: except aiohttp.ClientError:
_LOGGER.error("Client error %s.", request.url) _LOGGER.error("Client error %s.", request_url)
# register services # register services
hass.services.async_register(DOMAIN, name, async_service_handler) hass.services.async_register(DOMAIN, name, async_service_handler)