2020-01-11 11:20:00 +00:00
|
|
|
"""API for Netatmo bound to HASS OAuth."""
|
2021-07-21 21:36:57 +00:00
|
|
|
from typing import cast
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
from aiohttp import ClientSession
|
2020-01-11 11:20:00 +00:00
|
|
|
import pyatmo
|
|
|
|
|
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow
|
|
|
|
|
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
class AsyncConfigEntryNetatmoAuth(pyatmo.AbstractAsyncAuth):
|
2020-01-11 11:20:00 +00:00
|
|
|
"""Provide Netatmo authentication tied to an OAuth2 based config entry."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-20 12:59:19 +00:00
|
|
|
websession: ClientSession,
|
|
|
|
oauth_session: config_entry_oauth2_flow.OAuth2Session,
|
2021-05-20 12:06:44 +00:00
|
|
|
) -> None:
|
2021-05-20 12:59:19 +00:00
|
|
|
"""Initialize the auth."""
|
|
|
|
super().__init__(websession)
|
|
|
|
self._oauth_session = oauth_session
|
2020-01-11 11:20:00 +00:00
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_get_access_token(self) -> str:
|
2021-05-20 12:59:19 +00:00
|
|
|
"""Return a valid access token for Netatmo API."""
|
|
|
|
if not self._oauth_session.valid_token:
|
|
|
|
await self._oauth_session.async_ensure_token_valid()
|
2021-07-21 21:36:57 +00:00
|
|
|
return cast(str, self._oauth_session.token["access_token"])
|