Use ClientTimeout for hassio send_command (#47957)
This commit is contained in:
parent
28c80c1133
commit
8b3dccb1b4
1 changed files with 18 additions and 14 deletions
|
@ -4,7 +4,6 @@ import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import async_timeout
|
|
||||||
|
|
||||||
from homeassistant.components.http import (
|
from homeassistant.components.http import (
|
||||||
CONF_SERVER_HOST,
|
CONF_SERVER_HOST,
|
||||||
|
@ -52,7 +51,12 @@ def api_data(funct):
|
||||||
class HassIO:
|
class HassIO:
|
||||||
"""Small API wrapper for Hass.io."""
|
"""Small API wrapper for Hass.io."""
|
||||||
|
|
||||||
def __init__(self, loop, websession, ip):
|
def __init__(
|
||||||
|
self,
|
||||||
|
loop: asyncio.AbstractEventLoop,
|
||||||
|
websession: aiohttp.ClientSession,
|
||||||
|
ip: str,
|
||||||
|
) -> None:
|
||||||
"""Initialize Hass.io API."""
|
"""Initialize Hass.io API."""
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.websession = websession
|
self.websession = websession
|
||||||
|
@ -187,20 +191,20 @@ class HassIO:
|
||||||
This method is a coroutine.
|
This method is a coroutine.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
with async_timeout.timeout(timeout):
|
request = await self.websession.request(
|
||||||
request = await self.websession.request(
|
method,
|
||||||
method,
|
f"http://{self._ip}{command}",
|
||||||
f"http://{self._ip}{command}",
|
json=payload,
|
||||||
json=payload,
|
headers={X_HASSIO: os.environ.get("HASSIO_TOKEN", "")},
|
||||||
headers={X_HASSIO: os.environ.get("HASSIO_TOKEN", "")},
|
timeout=aiohttp.ClientTimeout(total=timeout),
|
||||||
)
|
)
|
||||||
|
|
||||||
if request.status not in (HTTP_OK, HTTP_BAD_REQUEST):
|
if request.status not in (HTTP_OK, HTTP_BAD_REQUEST):
|
||||||
_LOGGER.error("%s return code %d", command, request.status)
|
_LOGGER.error("%s return code %d", command, request.status)
|
||||||
raise HassioAPIError()
|
raise HassioAPIError()
|
||||||
|
|
||||||
answer = await request.json()
|
answer = await request.json()
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
_LOGGER.error("Timeout on %s request", command)
|
_LOGGER.error("Timeout on %s request", command)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue