2024-09-19 11:00:35 +01:00
|
|
|
import sys
|
2024-10-04 13:45:39 +01:00
|
|
|
import os
|
2024-10-04 16:35:45 +01:00
|
|
|
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
|
|
|
|
from dotenv import load_dotenv
|
2024-10-04 13:45:39 +01:00
|
|
|
|
|
|
|
load_dotenv()
|
2024-09-19 11:00:07 +01:00
|
|
|
|
2024-10-04 16:35:45 +01:00
|
|
|
use_mqtt = os.environ["use_mqtt"].lower() == "true"
|
|
|
|
if use_mqtt:
|
|
|
|
client = create_client()
|
|
|
|
create_config(client)
|
2024-09-19 11:00:07 +01:00
|
|
|
|
2024-10-04 16:35:45 +01:00
|
|
|
# Debug: Print environment variable
|
|
|
|
print(f"Using MQTT: {use_mqtt}")
|
2024-10-04 13:45:39 +01:00
|
|
|
|
2024-09-20 07:49:08 +01:00
|
|
|
|
2024-10-04 16:35:45 +01:00
|
|
|
# Helper function to handle disc operations
|
|
|
|
def handle_disc(disc_data):
|
|
|
|
print(f"Handling disc: {disc_data}")
|
|
|
|
disc_object = {"name": disc_data[1], "type": disc_data[2], "id": disc_data[3]}
|
|
|
|
if use_mqtt:
|
|
|
|
if disc_object == check_current_disc(client):
|
|
|
|
print("Disc is the same as the last one, playing.")
|
2024-09-20 12:52:23 +01:00
|
|
|
control_player(client, "PLAY")
|
2024-10-04 16:35:45 +01:00
|
|
|
if use_mqtt:
|
|
|
|
print("Updating disc via MQTT and playing.")
|
|
|
|
control_player(client, "PLAY")
|
|
|
|
update_disc(client, disc_data)
|
|
|
|
else:
|
|
|
|
print(f"Loading {disc_object['name']} via Home Assistant.")
|
|
|
|
load_disc(disc_object)
|
|
|
|
|
|
|
|
|
|
|
|
# Command handlers for media control
|
|
|
|
def handle_play():
|
|
|
|
print("Handling PLAY command.")
|
|
|
|
if use_mqtt:
|
|
|
|
control_player(client, "PLAY")
|
|
|
|
else:
|
|
|
|
play_media_again()
|
|
|
|
|
|
|
|
|
|
|
|
def handle_pause():
|
|
|
|
print("Handling PAUSE command.")
|
|
|
|
if use_mqtt:
|
|
|
|
control_player(client, "PAUSE")
|
|
|
|
else:
|
|
|
|
pause_media()
|
|
|
|
|
|
|
|
|
|
|
|
# Dictionary for command dispatching
|
|
|
|
command_handlers = {"EJECT": handle_pause, "PLAY": handle_play, "PAUSE": handle_pause}
|
|
|
|
|
|
|
|
|
|
|
|
# Main function to process input
|
|
|
|
def process_input(input_str):
|
|
|
|
print(f"Processing input: {input_str}")
|
|
|
|
|
|
|
|
if ":" in input_str:
|
|
|
|
# Handle disc input
|
|
|
|
disc_data = input_str.split(":")
|
|
|
|
if disc_data[0] == "ytmusic":
|
|
|
|
handle_disc(disc_data)
|
2024-09-20 12:52:23 +01:00
|
|
|
else:
|
2024-10-04 16:35:45 +01:00
|
|
|
print(f"Unknown disc type: {disc_data[0]}")
|
2024-09-20 12:52:23 +01:00
|
|
|
else:
|
2024-10-04 16:35:45 +01:00
|
|
|
# Handle control commands
|
|
|
|
command_handler = command_handlers.get(input_str)
|
|
|
|
if command_handler:
|
|
|
|
command_handler()
|
2024-09-20 12:52:23 +01:00
|
|
|
else:
|
2024-10-04 16:35:45 +01:00
|
|
|
print(f"Can't process {input_str}")
|
|
|
|
|
|
|
|
|
|
|
|
# Check if any arguments are passed
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
input_str = sys.argv[1]
|
|
|
|
print(f"Received argument: {input_str}")
|
|
|
|
process_input(input_str)
|
|
|
|
else:
|
|
|
|
print("No arguments passed.")
|