Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for myUplink bound to Home Assistant OAuth."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from aiohttp import ClientSession
8 from myuplink.auth_abstract import AbstractAuth
9 
10 from homeassistant.helpers import config_entry_oauth2_flow
11 
12 from .const import API_ENDPOINT
13 
14 
15 class AsyncConfigEntryAuth(AbstractAuth):
16  """Provide myUplink authentication tied to an OAuth2 based config entry."""
17 
18  def __init__(
19  self,
20  websession: ClientSession,
21  oauth_session: config_entry_oauth2_flow.OAuth2Session,
22  ) -> None:
23  """Initialize myUplink auth."""
24  super().__init__(websession, API_ENDPOINT)
25  self._oauth_session_oauth_session = oauth_session
26 
27  async def async_get_access_token(self) -> str:
28  """Return a valid access token."""
29  await self._oauth_session_oauth_session.async_ensure_token_valid()
30 
31  return cast(str, self._oauth_session_oauth_session.token["access_token"])