59 lines
1.8 KiB
Python
Raw Normal View History

2024-09-19 11:00:35 +01:00
import sys
2024-09-20 11:13:04 +01:00
from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
2024-10-04 13:45:39 +01:00
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()
2024-09-19 11:00:07 +01:00
2024-09-20 17:44:22 +01:00
client = create_client()
2024-09-19 11:00:07 +01:00
create_config(client)
2024-10-04 13:45:39 +01:00
use_mqtt = os.environ['use_mqtt'].lower() == 'true'
2024-09-19 11:00:35 +01:00
# Check if any arguments are passed
if len(sys.argv) > 1:
# Get the JSON argument from the command line
2024-09-20 12:41:25 +01:00
input_str = sys.argv[1]
2024-09-20 07:49:08 +01:00
2024-09-20 12:52:23 +01:00
# Convert the JSON string into a dictionary
disc_data = input_str.split(":")
if disc_data[0] == "ytmusic":
disc_object = {
2024-09-20 14:13:21 +01:00
"name":disc_data[1],
2024-09-20 12:52:23 +01:00
"type":disc_data[2],
"id":disc_data[3]
}
# Check if disc is the same as last inserted
if (disc_object == check_current_disc(client)):
2024-09-20 12:52:23 +01:00
# If disc is the same, PLAY
control_player(client, "PLAY")
else:
# Pass the parsed data to the update_disc function
2024-10-04 13:45:39 +01:00
if use_mqtt:
control_player(client, "PLAY")
update_disc(client, disc_data)
else:
load_disc(disc_object)
2024-09-20 12:52:23 +01:00
else:
if input_str == "EJECT":
# PAUSE the current playing item
2024-10-04 13:45:39 +01:00
if use_mqtt:
control_player(client, "PAUSE")
else:
pause_media()
2024-09-20 12:52:23 +01:00
elif input_str == "PLAY":
# PLAY the current playing item
2024-10-04 13:45:39 +01:00
if use_mqtt:
control_player(client, "PLAY")
else:
play_media_again()
2024-09-20 12:52:23 +01:00
elif input_str == "PAUSE":
# PAUSE the current playing item
2024-10-04 13:45:39 +01:00
if use_mqtt:
control_player(client, "PAUSE")
else:
pause_media()
2024-09-20 12:52:23 +01:00
else:
print(f"Can't process {input_str}")