59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import sys
|
|
from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
|
|
from lib.home_assistant import load_disc, pause_media,play_media_again,play_radio,stop_media
|
|
from dotenv import load_dotenv
|
|
import os
|
|
|
|
load_dotenv()
|
|
|
|
client = create_client()
|
|
|
|
create_config(client)
|
|
|
|
use_mqtt = os.environ['use_mqtt'].lower() == 'true'
|
|
|
|
# Check if any arguments are passed
|
|
if len(sys.argv) > 1:
|
|
# Get the JSON argument from the command line
|
|
input_str = sys.argv[1]
|
|
|
|
# Convert the JSON string into a dictionary
|
|
disc_data = input_str.split(":")
|
|
if disc_data[0] == "ytmusic":
|
|
disc_object = {
|
|
"name":disc_data[1],
|
|
"type":disc_data[2],
|
|
"id":disc_data[3]
|
|
}
|
|
# Check if disc is the same as last inserted
|
|
if (disc_object == check_current_disc(client)):
|
|
# If disc is the same, PLAY
|
|
control_player(client, "PLAY")
|
|
else:
|
|
# Pass the parsed data to the update_disc function
|
|
if use_mqtt:
|
|
control_player(client, "PLAY")
|
|
update_disc(client, disc_data)
|
|
else:
|
|
load_disc(disc_object)
|
|
else:
|
|
if input_str == "EJECT":
|
|
# PAUSE the current playing item
|
|
if use_mqtt:
|
|
control_player(client, "PAUSE")
|
|
else:
|
|
pause_media()
|
|
elif input_str == "PLAY":
|
|
# PLAY the current playing item
|
|
if use_mqtt:
|
|
control_player(client, "PLAY")
|
|
else:
|
|
play_media_again()
|
|
elif input_str == "PAUSE":
|
|
# PAUSE the current playing item
|
|
if use_mqtt:
|
|
control_player(client, "PAUSE")
|
|
else:
|
|
pause_media()
|
|
else:
|
|
print(f"Can't process {input_str}") |