Handle access token expiration (#671)

This commit is contained in:
Paulus Schoutsen 2018-08-28 12:14:40 +02:00 committed by Pascal Vizeli
parent b1e8722ead
commit 1a208a20b6

View File

@ -1,6 +1,7 @@
"""HomeAssistant control object.""" """HomeAssistant control object."""
import asyncio import asyncio
from contextlib import asynccontextmanager, suppress from contextlib import asynccontextmanager, suppress
from datetime import datetime, timedelta
import logging import logging
import os import os
import re import re
@ -46,6 +47,7 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
self._error_state = False self._error_state = False
# We don't persist access tokens. Instead we fetch new ones when needed # We don't persist access tokens. Instead we fetch new ones when needed
self.access_token = None self.access_token = None
self.access_token_expires = None
async def load(self): async def load(self):
"""Prepare HomeAssistant object.""" """Prepare HomeAssistant object."""
@ -352,7 +354,8 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
async def ensure_access_token(self): async def ensure_access_token(self):
"""Ensures there is an access token.""" """Ensures there is an access token."""
if self.access_token is not None: if (self.access_token is not None and
self.access_token_expires < datetime.utcnow()):
return return
with suppress(asyncio.TimeoutError, aiohttp.ClientError): with suppress(asyncio.TimeoutError, aiohttp.ClientError):
@ -364,14 +367,15 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
"refresh_token": self.refresh_token "refresh_token": self.refresh_token
} }
) as resp: ) as resp:
if resp.status == 200: if resp.status != 200:
_LOGGER.error("Can't update HomeAssistant access token!")
raise HomeAssistantAuthError()
_LOGGER.info("Updated HomeAssistant API token") _LOGGER.info("Updated HomeAssistant API token")
tokens = await resp.json() tokens = await resp.json()
self.access_token = tokens['access_token'] self.access_token = tokens['access_token']
return self.access_token_expires = \
datetime.utcnow() + timedelta(seconds=tokens['expires_in'])
_LOGGER.error("Can't update HomeAssistant access token!")
raise HomeAssistantAuthError()
@asynccontextmanager @asynccontextmanager
async def make_request(self, method, path, json=None, content_type=None, async def make_request(self, method, path, json=None, content_type=None,