-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
/
Copy pathapi.py
38 lines (26 loc) · 1.27 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""API for Volvo bound to Home Assistant OAuth."""
from typing import cast
from aiohttp import ClientSession
from volvocarsapi.auth import AccessTokenManager
from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session
class VolvoAuth(AccessTokenManager):
"""Provide Volvo authentication tied to an OAuth2 based config entry."""
def __init__(self, websession: ClientSession, oauth_session: OAuth2Session) -> None:
"""Initialize Volvo auth."""
super().__init__(websession)
self._oauth_session = oauth_session
async def async_get_access_token(self) -> str:
"""Return a valid access token."""
await self._oauth_session.async_ensure_token_valid()
return cast(str, self._oauth_session.token["access_token"])
class ConfigFlowVolvoAuth(AccessTokenManager):
"""Provide Volvo authentication before a ConfigEntry exists.
This implementation directly provides the token without supporting refresh.
"""
def __init__(self, websession: ClientSession, token: str) -> None:
"""Initialize ConfigFlowVolvoAuth."""
super().__init__(websession)
self._token = token
async def async_get_access_token(self) -> str:
"""Return the token for the Volvo API."""
return self._token