Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for yolink bound to Home Assistant OAuth."""
2 
3 from aiohttp import ClientSession
4 from yolink.auth_mgr import YoLinkAuthMgr
5 
6 from homeassistant.core import HomeAssistant
7 from homeassistant.helpers import config_entry_oauth2_flow
8 
9 
10 class ConfigEntryAuth(YoLinkAuthMgr):
11  """Provide yolink authentication tied to an OAuth2 based config entry."""
12 
13  def __init__(
14  self,
15  hass: HomeAssistant,
16  websession: ClientSession,
17  oauth2Session: config_entry_oauth2_flow.OAuth2Session,
18  ) -> None:
19  """Initialize yolink Auth."""
20  self.hasshass = hass
21  self.oauth_sessionoauth_session = oauth2Session
22  super().__init__(websession)
23 
24  def access_token(self) -> str:
25  """Return the access token."""
26  return self.oauth_sessionoauth_session.token["access_token"]
27 
28  async def check_and_refresh_token(self) -> str:
29  """Check the token."""
30  await self.oauth_sessionoauth_session.async_ensure_token_valid()
31  return self.access_tokenaccess_token()