Skip to content

Commit

Permalink
adapted to ytubemusic api 1.4.2 oAuth refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KoljaWindeler committed Jan 7, 2024
1 parent 5e02a93 commit a717b37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions custom_components/ytube_music_player/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ytube_music_player/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a717b37

Please sign in to comment.