Skip to content

Commit

Permalink
Rename rest_command request to response, add exc_info for exceptions (h…
Browse files Browse the repository at this point in the history
…ome-assistant#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
  • Loading branch information
Misiu authored and pvizeli committed Nov 23, 2019
1 parent 0bd01ba commit 23737e0
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions homeassistant/components/rest_command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

import aiohttp
from aiohttp import hdrs
import async_timeout
import voluptuous as vol

from homeassistant.const import (
CONF_TIMEOUT,
CONF_USERNAME,
CONF_HEADERS,
CONF_METHOD,
CONF_PASSWORD,
CONF_URL,
CONF_PAYLOAD,
CONF_METHOD,
CONF_HEADERS,
CONF_TIMEOUT,
CONF_URL,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand Down Expand Up @@ -96,21 +95,32 @@ async def async_service_handler(service):

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
)

if request.status < 400:
_LOGGER.info("Success call %s.", request.url)
else:
_LOGGER.warning("Error %d on call %s.", request.status, request.url)
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,
)

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)
Expand Down

0 comments on commit 23737e0

Please sign in to comment.