From 23737e0225911911f171ba3e673092a21faa88c3 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sat, 23 Nov 2019 11:02:52 +0100 Subject: [PATCH] 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 --- .../components/rest_command/__init__.py | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/rest_command/__init__.py b/homeassistant/components/rest_command/__init__.py index 223dc7da7cc..10f37b6ac4c 100644 --- a/homeassistant/components/rest_command/__init__.py +++ b/homeassistant/components/rest_command/__init__.py @@ -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 request.status < 400: - _LOGGER.info("Success call %s.", request.url) - else: - _LOGGER.warning("Error %d on call %s.", request.status, request.url) + 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, + ) 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)