Home Assistant Unofficial Reference
2024.12.1
api.py
Go to the documentation of this file.
1
"""API for YouTube bound to Home Assistant OAuth."""
2
3
from
youtubeaio.types
import
AuthScope
4
from
youtubeaio.youtube
import
YouTube
5
6
from
homeassistant.const
import
CONF_ACCESS_TOKEN
7
from
homeassistant.core
import
HomeAssistant
8
from
homeassistant.helpers
import
config_entry_oauth2_flow
9
from
homeassistant.helpers.aiohttp_client
import
async_get_clientsession
10
11
12
class
AsyncConfigEntryAuth
:
13
"""Provide Google authentication tied to an OAuth2 based config entry."""
14
15
youtube: YouTube |
None
=
None
16
17
def
__init__
(
18
self,
19
hass: HomeAssistant,
20
oauth2_session: config_entry_oauth2_flow.OAuth2Session,
21
) ->
None
:
22
"""Initialize YouTube Auth."""
23
self.
oauth_session
oauth_session = oauth2_session
24
self.
hass
hass = hass
25
26
@property
27
def
access_token
(self) -> str:
28
"""Return the access token."""
29
return
self.
oauth_session
oauth_session.token[CONF_ACCESS_TOKEN]
# type: ignore[no-any-return]
30
31
async
def
check_and_refresh_token
(self) -> str:
32
"""Check the token."""
33
await self.
oauth_session
oauth_session.async_ensure_token_valid()
34
return
self.
access_token
access_token
35
36
async
def
get_resource
(self) -> YouTube:
37
"""Create resource."""
38
token = await self.
check_and_refresh_token
check_and_refresh_token()
39
if
self.
youtube
youtube
is
None
:
40
self.
youtube
youtube = YouTube(session=
async_get_clientsession
(self.
hass
hass))
41
await self.
youtube
youtube.set_user_authentication(token, [AuthScope.READ_ONLY])
42
return
self.
youtube
youtube
homeassistant.components.youtube.api.AsyncConfigEntryAuth
Definition:
api.py:12
homeassistant.components.youtube.api.AsyncConfigEntryAuth.__init__
None __init__(self, HomeAssistant hass, config_entry_oauth2_flow.OAuth2Session oauth2_session)
Definition:
api.py:21
homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource
YouTube get_resource(self)
Definition:
api.py:36
homeassistant.components.youtube.api.AsyncConfigEntryAuth.oauth_session
oauth_session
Definition:
api.py:23
homeassistant.components.youtube.api.AsyncConfigEntryAuth.access_token
str access_token(self)
Definition:
api.py:27
homeassistant.components.youtube.api.AsyncConfigEntryAuth.youtube
youtube
Definition:
api.py:40
homeassistant.components.youtube.api.AsyncConfigEntryAuth.check_and_refresh_token
str check_and_refresh_token(self)
Definition:
api.py:31
homeassistant.components.youtube.api.AsyncConfigEntryAuth.hass
hass
Definition:
api.py:24
homeassistant.const
Definition:
const.py:1
homeassistant.core
Definition:
core.py:1
homeassistant.helpers.aiohttp_client
Definition:
aiohttp_client.py:1
homeassistant.helpers.aiohttp_client.async_get_clientsession
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
Definition:
aiohttp_client.py:92
homeassistant.helpers
Definition:
__init__.py:1
core
homeassistant
components
youtube
api.py
Generated by
1.9.1