Ver Fonte

add direct HA logic

Karl Hudgell há 6 meses atrás
pai
commit
7634331bc1
1 ficheiros alterados com 117 adições e 0 exclusões
  1. 117 0
      lib/home_assistant.py

+ 117 - 0
lib/home_assistant.py

@@ -0,0 +1,117 @@
+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")