KoljaWindeler / ytube_music_player

YouTube music player for homeassistant
346 stars 53 forks source link

The player waits for ~10 secs and fails to play the song on media player #356

Open ghj7211 opened 3 months ago

ghj7211 commented 3 months ago

Describe the bug When I try to play a track with ytube_media_player, it connects to my google home mini (i hear it being connected), awaits for about 10 seconds, and then fails to play it. When I try playing a playlist, each titles fail to play and immediately move to the next track until the end of the list.

How to reproduce This is what i get when I directly execute service from developer tools: (edit: i put in a wrong media_content_id so corrected it. now i got the id directly from {{state_attr('sensor.ytube_music_player_extra', 'search')[0].id }} )

action: media_player.play_media
data:
  media_content_id: "svO1L1uIxjM"
  entity_id: media_player.ytube_music_player
  media_content_type: music
- Retry with: (2)
- Retry with: (1)

- Failed to get URL with YouTube methode

HTTP Error 400: Bad Request

[S] async_check_api [E]
[S] async_turn_off_media_player [E]

[E] async_get_url
- self decoded url return 403 status code, attempt 60/60
[S] async_get_url_pytube
- get track failed to return URL, turning off
[E] play_media

STOP command requested but no session is active.
Error executing service: <ServiceCall media_player.media_stop (c:01J5B790VN5D9RMSTP89QE4GAH): entity_id=['media_player.keunbang']>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 100, in wrapper
    return_value = func(self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 570, in media_stop
    media_controller.stop()
  File "/usr/local/lib/python3.12/site-packages/pychromecast/controllers/media.py", line 642, in stop
    response_handler.wait_response()
  File "/usr/local/lib/python3.12/site-packages/pychromecast/response_handler.py", line 57, in wait_response
    raise RequestFailed(self._request)
pychromecast.error.RequestFailed: Failed to execute stop.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 2783, in _run_service_call_catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 2806, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 999, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1071, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 830, in async_media_stop
    await self.hass.async_add_executor_job(self.media_stop)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 102, in wrapper
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: CastMediaPlayerEntity.media_stop Failed: Failed to execute stop.

I also tried playing a random track from dashboard by selecting a track from ytube_media_player media source with the default card. This is the log message:

- Retry with: (3)
- Retry with: (2)
- Retry with: (1)

- Failed to get URL with YouTube methode

HTTP Error 400: Bad Request

* [S] async_sync_player ... media_player.keunbang: idle -> idle ... not playing [E]
* [S] _tracks_to_attribute
* [S] async_update_extra_sensor [E]
* [S] async_check_api [E]
* [S] async_turn_off_media_player [E]

* [E] async_get_url
* - self decoded url return 403 status code, attempt 60/60
* [S] async_get_url_pytube
* - get track failed to return URL, turning off
* [E] play_media

STOP command requested but no session is active.
Error executing service: <ServiceCall media_player.media_stop (c:01J5B7HF8RCATNGJXPTGGBPQJ1): entity_id=['media_player.keunbang']>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 100, in wrapper
    return_value = func(self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 570, in media_stop
    media_controller.stop()
  File "/usr/local/lib/python3.12/site-packages/pychromecast/controllers/media.py", line 642, in stop
    response_handler.wait_response()
  File "/usr/local/lib/python3.12/site-packages/pychromecast/response_handler.py", line 57, in wait_response
    raise RequestFailed(self._request)
pychromecast.error.RequestFailed: Failed to execute stop.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 2783, in _run_service_call_catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 2806, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 999, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1071, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 830, in async_media_stop
    await self.hass.async_add_executor_job(self.media_stop)
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 102, in wrapper
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: CastMediaPlayerEntity.media_stop Failed: Failed to execute stop.

Version ytube_media_player 20240810.01 (I also tried 20240709.01 and 20240704.01, which did not work as well)

Core
2024.8.1
Supervisor
2024.08.0

Additional Info I tried removing it from hacs, integrations, and json auth from config/.storage, and then reboot with reinstall. It used to work until a month ago. It had worked flawlessly. Since then I haven’t been using the feature so i cannot tell from when or why it had stopped working. I strongly suspect that this is recent pytube issue, but i'd like to know if any others have this same problem. Maybe It could just be a problem on my side. FYI, I currently have the extra sensor enabled in advanced settings.

Thanks in advance.

ghj7211 commented 3 months ago

Ok, so i found the fix. The issue was due to recent change in youtube music. (link) I found out that @KoljaWindeler already released a temporary fix for this in 20240810.01, but somehow it did not work out on my side. My workaround here was to use 'pytubefix' library. Though not reliable, the original pytube library itself seems to be a dead repo, so the change should not sound drastic. I'll leave a fork for the patched version: https://github.com/KoljaWindeler/ytube_music_player/compare/main...ghj7211:ytube_music_player:main

LeeHanYeong commented 2 months ago

Ok, so i found the fix. The issue was due to recent change in youtube music. (link) I found out that @KoljaWindeler already released a temporary fix for this in 20240810.01, but somehow it did not work out on my side. My workaround here was to use 'pytubefix' library. Though not reliable, the original pytube library itself seems to be a dead repo, so the change should not sound drastic. I'll leave a fork for the patched version: main...ghj7211:ytube_music_player:main

I'm using the code by modifying it like your commit. Thank you. I hope this commit gets merged soon.