Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Husqvarna Automower bound to Home Assistant OAuth."""
2 
3 import logging
4 from typing import cast
5 
6 from aioautomower.auth import AbstractAuth
7 from aioautomower.const import API_BASE_URL
8 from aiohttp import ClientSession
9 
10 from homeassistant.const import CONF_ACCESS_TOKEN
11 from homeassistant.helpers import config_entry_oauth2_flow
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 
16 class AsyncConfigEntryAuth(AbstractAuth):
17  """Provide Husqvarna Automower authentication tied to an OAuth2 based config entry."""
18 
19  def __init__(
20  self,
21  websession: ClientSession,
22  oauth_session: config_entry_oauth2_flow.OAuth2Session,
23  ) -> None:
24  """Initialize Husqvarna Automower auth."""
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  return cast(str, self._oauth_session_oauth_session.token["access_token"])
32 
33 
34 class AsyncConfigFlowAuth(AbstractAuth):
35  """Provide Automower AbstractAuth for the config flow."""
36 
37  def __init__(self, websession: ClientSession, token: dict) -> None:
38  """Initialize Husqvarna Automower auth."""
39  super().__init__(websession, API_BASE_URL)
40  self.token: dict = token
41 
42  async def async_get_access_token(self) -> str:
43  """Return a valid access token."""
44  return cast(str, self.token[CONF_ACCESS_TOKEN])
None __init__(self, ClientSession websession, config_entry_oauth2_flow.OAuth2Session oauth_session)
Definition: api.py:23
None __init__(self, ClientSession websession, dict token)
Definition: api.py:37