Rename rest_command request to response, add exc_info for exceptions (#28521)

* rename request to response, isort and black fixes

* Log exception details

* Add status code to success log, reformat log

* new syntax for response

* changed info to debug
pull/29060/head
Tomasz 2019-11-23 11:02:52 +01:00 committed by Pascal Vizeli
parent 0bd01ba495
commit 23737e0225
1 changed files with 27 additions and 17 deletions

View File

@ -4,17 +4,16 @@ import logging
import aiohttp
from aiohttp import hdrs
import async_timeout
import voluptuous as vol
from homeassistant.const import (
CONF_TIMEOUT,
CONF_USERNAME,
CONF_PASSWORD,
CONF_URL,
CONF_PAYLOAD,
CONF_METHOD,
CONF_HEADERS,
CONF_METHOD,
CONF_PASSWORD,
CONF_PAYLOAD,
CONF_TIMEOUT,
CONF_URL,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -96,21 +95,32 @@ async def async_setup(hass, config):
request_url = template_url.async_render(variables=service.data)
try:
with async_timeout.timeout(timeout):
request = await getattr(websession, method)(
request_url, data=payload, auth=auth, headers=headers
async with getattr(websession, method)(
request_url,
data=payload,
auth=auth,
headers=headers,
timeout=timeout,
) as response:
if response.status < 400:
_LOGGER.debug(
"Success. Url: %s. Status code: %d.",
response.url,
response.status,
)
else:
_LOGGER.warning(
"Error. Url: %s. Status code %d.",
response.url,
response.status,
)
if request.status < 400:
_LOGGER.info("Success call %s.", request.url)
else:
_LOGGER.warning("Error %d on call %s.", request.status, request.url)
except asyncio.TimeoutError:
_LOGGER.warning("Timeout call %s.", request.url)
_LOGGER.warning("Timeout call %s.", response.url, exc_info=1)
except aiohttp.ClientError:
_LOGGER.error("Client error %s.", request_url)
_LOGGER.error("Client error %s.", request_url, exc_info=1)
# register services
hass.services.async_register(DOMAIN, name, async_service_handler)