Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Electric Kiwi 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 electrickiwi_api import AbstractAuth
9 
10 from homeassistant.helpers import config_entry_oauth2_flow
11 
12 from .const import API_BASE_URL
13 
14 
15 class AsyncConfigEntryAuth(AbstractAuth):
16  """Provide Electric Kiwi 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 Electric Kiwi auth."""
24  # add host when ready for production "https://api.electrickiwi.co.nz" defaults to dev
25  super().__init__(websession, API_BASE_URL)
26  self._oauth_session_oauth_session = oauth_session
27 
28  async def async_get_access_token(self) -> str:
29  """Return a valid access token."""
30  await self._oauth_session_oauth_session.async_ensure_token_valid()
31 
32  return cast(str, self._oauth_session_oauth_session.token["access_token"])
None __init__(self, ClientSession websession, config_entry_oauth2_flow.OAuth2Session oauth_session)
Definition: api.py:22