forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
48 lines (39 loc) · 1.58 KB
/
api.py
File metadata and controls
48 lines (39 loc) · 1.58 KB
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
39
40
41
42
43
44
45
46
47
48
"""API helpers for the Eve Online integration."""
from __future__ import annotations
from typing import cast
from aiohttp import ClientError, ClientSession
from eveonline.auth import AbstractAuth
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
OAuth2TokenRequestReauthError,
OAuth2TokenRequestTransientError,
)
from homeassistant.helpers.config_entry_oauth2_flow import OAuth2Session
from homeassistant.helpers.update_coordinator import UpdateFailed
from .const import DOMAIN
class AsyncConfigEntryAuth(AbstractAuth):
"""Provide Eve Online authentication tied to an OAuth2 based config entry."""
def __init__(
self,
websession: ClientSession,
oauth_session: OAuth2Session,
) -> None:
"""Initialize Eve Online auth."""
super().__init__(websession)
self._oauth_session = oauth_session
async def async_get_access_token(self) -> str:
"""Return a valid access token."""
try:
await self._oauth_session.async_ensure_token_valid()
except OAuth2TokenRequestReauthError as err:
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="authentication_failed",
) from err
except (OAuth2TokenRequestTransientError, ClientError) as err:
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="token_refresh_failed",
translation_placeholders={"error": str(err)},
) from err
return cast(str, self._oauth_session.token["access_token"])