Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Netatmo bound to HASS OAuth."""
2 
3 from collections.abc import Iterable
4 from typing import cast
5 
6 from aiohttp import ClientSession
7 import pyatmo
8 
9 from homeassistant.components import cloud
10 from homeassistant.helpers import config_entry_oauth2_flow
11 
12 from .const import API_SCOPES_EXCLUDED_FROM_CLOUD
13 
14 
15 def get_api_scopes(auth_implementation: str) -> Iterable[str]:
16  """Return the Netatmo API scopes based on the auth implementation."""
17 
18  if auth_implementation == cloud.DOMAIN:
19  return set(
20  {
21  scope
22  for scope in pyatmo.const.ALL_SCOPES
23  if scope not in API_SCOPES_EXCLUDED_FROM_CLOUD
24  }
25  )
26  return sorted(pyatmo.const.ALL_SCOPES)
27 
28 
29 class AsyncConfigEntryNetatmoAuth(pyatmo.AbstractAsyncAuth):
30  """Provide Netatmo authentication tied to an OAuth2 based config entry."""
31 
32  def __init__(
33  self,
34  websession: ClientSession,
35  oauth_session: config_entry_oauth2_flow.OAuth2Session,
36  ) -> None:
37  """Initialize the auth."""
38  super().__init__(websession)
39  self._oauth_session_oauth_session = oauth_session
40 
41  async def async_get_access_token(self) -> str:
42  """Return a valid access token for Netatmo API."""
43  await self._oauth_session_oauth_session.async_ensure_token_valid()
44  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:36
Iterable[str] get_api_scopes(str auth_implementation)
Definition: api.py:15