diff --git a/hassio/api/proxy.py b/hassio/api/proxy.py index 24c4b72aa..da81bf242 100644 --- a/hassio/api/proxy.py +++ b/hassio/api/proxy.py @@ -57,7 +57,7 @@ class APIProxy(CoreSysAttributes): yield resp return - except HomeAssistantAuthError: + except HomeAssistantAPIError: _LOGGER.error("Authenticate error on API for request %s", path) except aiohttp.ClientError as err: _LOGGER.error("Client error on API %s request %s", path, err) @@ -151,7 +151,7 @@ class APIProxy(CoreSysAttributes): _LOGGER.error( "Failed authentication to Home-Assistant websocket: %s", data) - except (RuntimeError, HomeAssistantAPIError) as err: + except (RuntimeError, HomeAssistantAuthError, ValueError) as err: _LOGGER.error("Client error on websocket API %s.", err) raise HTTPBadGateway() diff --git a/hassio/exceptions.py b/hassio/exceptions.py index cd9499ba3..9cd18e146 100644 --- a/hassio/exceptions.py +++ b/hassio/exceptions.py @@ -1,7 +1,4 @@ """Core Exceptions.""" -import asyncio - -import aiohttp class HassioError(Exception): @@ -26,14 +23,13 @@ class HomeAssistantUpdateError(HomeAssistantError): pass -class HomeAssistantAuthError(HomeAssistantError): - """Home Assistant Auth API exception.""" +class HomeAssistantAPIError(HomeAssistantError): + """Home Assistant API exception.""" pass -class HomeAssistantAPIError( - HomeAssistantAuthError, asyncio.TimeoutError, aiohttp.ClientError): - """Home Assistant API exception.""" +class HomeAssistantAuthError(HomeAssistantAPIError): + """Home Assistant Auth API exception.""" pass diff --git a/hassio/homeassistant.py b/hassio/homeassistant.py index 3eb3bde4f..832a2d04b 100644 --- a/hassio/homeassistant.py +++ b/hassio/homeassistant.py @@ -355,7 +355,7 @@ class HomeAssistant(JsonConfig, CoreSysAttributes): return with suppress(asyncio.TimeoutError, aiohttp.ClientError): - async with self.sys_websession_ssl.get( + async with self.sys_websession_ssl.post( f"{self.api_url}/auth/token", timeout=30, data={ @@ -363,15 +363,14 @@ class HomeAssistant(JsonConfig, CoreSysAttributes): "refresh_token": self.refresh_token } ) as resp: - if resp.status != 200: - _LOGGER.error("Authenticate problem with HomeAssistant!") - raise HomeAssistantAuthError() - tokens = await resp.json() - self.access_token = tokens['access_token'] - return + if resp.status == 200: + _LOGGER.info("Updated HomeAssistant API token") + tokens = await resp.json() + self.access_token = tokens['access_token'] + return _LOGGER.error("Can't update HomeAssistant access token!") - raise HomeAssistantAPIError() + raise HomeAssistantAuthError() @asynccontextmanager async def make_request(self, method, path, json=None, content_type=None, @@ -394,15 +393,20 @@ class HomeAssistant(JsonConfig, CoreSysAttributes): await self.ensure_access_token() headers[hdrs.AUTHORIZATION] = f'Bearer {self.access_token}' - async with getattr(self.sys_websession_ssl, method)( - url, data=data, timeout=timeout, json=json, headers=headers - ) as resp: - # Access token expired - if resp.status == 401 and self.refresh_token: - self.access_token = None - continue - yield resp - return + try: + async with getattr(self.sys_websession_ssl, method)( + url, data=data, timeout=timeout, json=json, + headers=headers + ) as resp: + # Access token expired + if resp.status == 401 and self.refresh_token: + self.access_token = None + continue + yield resp + return + except (asyncio.TimeoutError, aiohttp.ClientError) as err: + _LOGGER.error("Error on call %s: %s", url, err) + break raise HomeAssistantAPIError() diff --git a/requirements.txt b/requirements.txt index f1df28229..e22f09920 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ attr==0.3.1 async_timeout==3.0.0 aiohttp==3.3.2 -docker==3.4.0 +docker==3.4.1 colorlog==3.1.2 voluptuous==0.11.1 gitpython==2.1.10 diff --git a/tox.ini b/tox.ini index e2aff7381..23310edf9 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ envlist = lint [testenv] deps = flake8==3.5.0 - pylint==2.0.0 + pylint==2.1.1 -r{toxinidir}/requirements.txt [testenv:lint]