From a717b370db0a5d59ee5ea7657ee5752b45be1948 Mon Sep 17 00:00:00 2001 From: Kolja Windeler Date: Sun, 7 Jan 2024 16:36:14 +0100 Subject: [PATCH] adapted to ytubemusic api 1.4.2 oAuth refactoring --- custom_components/ytube_music_player/config_flow.py | 11 +++++++---- custom_components/ytube_music_player/const.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/custom_components/ytube_music_player/config_flow.py b/custom_components/ytube_music_player/config_flow.py index 4914ff8..d63f18f 100644 --- a/custom_components/ytube_music_player/config_flow.py +++ b/custom_components/ytube_music_player/config_flow.py @@ -11,7 +11,8 @@ from homeassistant.helpers.storage import STORAGE_DIR from ytmusicapi import YTMusic import requests -from ytmusicapi.auth.oauth import YTMusicOAuth +from ytmusicapi.auth.oauth import OAuthCredentials, RefreshingToken + import traceback import asyncio @@ -41,7 +42,7 @@ async def async_step_user(self, user_input=None): # pylint: disable=unused-arg user_input[CONF_NAME] = DOMAIN session = requests.Session() - self.oauth = YTMusicOAuth(session) + self.oauth = OAuthCredentials("","",session,"") self.code = await self.hass.async_add_executor_job(self.oauth.get_code) user_input[CONF_CODE] = self.code return self.async_show_form(step_id="oauth", data_schema=vol.Schema(await async_create_form(self.hass,user_input,1)), errors=self._errors) @@ -51,7 +52,9 @@ async def async_step_user(self, user_input=None): # pylint: disable=unused-arg async def async_step_oauth(self, user_input=None): # pylint: disable=unused-argument self._errors = {} try: - self.token = await self.hass.async_add_executor_job(lambda: self.oauth.get_token_from_code(self.code["device_code"])) + self.token = await self.hass.async_add_executor_job(lambda: self.oauth.token_from_code(self.code["device_code"])) + self.refresh_token = RefreshingToken(credentials=self.oauth, **self.token) + self.refresh_token.update(self.refresh_token.as_dict()) except: self._errors["base"] = ERROR_GENERIC user_input[CONF_CODE] = self.code @@ -66,7 +69,7 @@ async def async_step_finish(self,user_input=None): self._errors = {} if user_input is not None: self.data = user_input - await self.hass.async_add_executor_job(lambda: self.oauth.dump_token(self.token,self.data[CONF_HEADER_PATH])) + await self.hass.async_add_executor_job(lambda: self.refresh_token.store_token(self.data[CONF_HEADER_PATH])) if(self.data[CONF_ADVANCE_CONFIG]): return self.async_show_form(step_id="adv_finish", data_schema=vol.Schema(await async_create_form(self.hass,user_input,3)), errors=self._errors) else: diff --git a/custom_components/ytube_music_player/const.py b/custom_components/ytube_music_player/const.py index 7b4c9da..e49c1d5 100644 --- a/custom_components/ytube_music_player/const.py +++ b/custom_components/ytube_music_player/const.py @@ -243,7 +243,7 @@ async def async_try_login(hass, path, brand_id): _LOGGER.debug("- using brand ID: "+brand_id) api = await hass.async_add_executor_job(YTMusic,path,brand_id) else: - _LOGGER.debug("- login without brand ID") + _LOGGER.debug("- login without brand ID and credential at path "+path) api = await hass.async_add_executor_job(YTMusic,path) except KeyError as err: _LOGGER.debug("- Key exception")