123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- from homeassistant_api import Client
- from dotenv import load_dotenv
- import os
- load_dotenv()
- # # play track
- # client.get_domain("media_player").play_media(entity_id="media_player.ytube_music_player", media_content_id="LM", media_content_type="playlist")
- # # pause
- # client.get_domain("media_player").media_pause(entity_id="media_player.ytube_music_player")
- # # sleep(5)
- # # # play
- # client.get_domain("media_player").media_play(entity_id="media_player.ytube_music_player")
- # # # stop
- # client.get_domain("media_player").media_stop(entity_id="media_player.ytube_music_player")
- # # skip
- # client.get_domain("media_player").media_next_track(entity_id="media_player.ytube_music_player")
- # # radio
- # client.get_domain("media_player").play_media(entity_id="media_player.ytube_music_player", media_content_id="{{state_attr("media_player.ytube_music_player","videoId") }}", media_content_type="vid_channel")
- # # shuffle
- # {'name': 'Daft Punk Random Access Memories', 'type': 'album', 'id': 'MPREb_K8qWMWVqXGi'}
- def create_client() -> Client:
- url = os.environ["ha_url"] + "/api"
- token = os.environ["ha_token"]
- return Client(url, token)
- def load_disc(disc_object):
- """
- Play media on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.play_media(
- entity_id="media_player.ytube_music_player",
- media_content_id=disc_object['id'],
- media_content_type=disc_object['type'],
- )
- def pause_media():
- """
- Pause media on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.media_pause(entity_id="media_player.ytube_music_player")
- def play_media_again():
- """
- Play paused or stopped media on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.media_play(entity_id="media_player.ytube_music_player")
- def stop_media():
- """
- Stop media on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.media_stop(entity_id="media_player.ytube_music_player")
- def next_track():
- """
- Skip to the next track on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.media_next_track(entity_id="media_player.ytube_music_player")
- def set_shuffle():
- """
- Enable or disable shuffle on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- # Use toggle_set instead of just set, because get_shuffle returns None if no current setting
- media_player.toggle_set(entity_id="media_player.ytube_music_player", attribute="shuffle", value=True)
- def play_radio():
- """
- Play radio or vid channel on a media player.
- Args:
- client (homeassistant_api.Client): Home Assistant API client.
- """
- media_player = create_client().get_domain("media_player")
- media_player.play_media(
- entity_id="media_player.ytube_music_player",
- media_content_id='{{state_attr("media_player.ytube_music_player","videoId") }}',
- media_content_type="vid_channel",
- )
- # load_disc("MPREb_GkjTjE1eOn4", "album")
|