diff --git a/smarttv.py b/smarttv.py index 21dd4a7..a46091a 100644 --- a/smarttv.py +++ b/smarttv.py @@ -2,9 +2,13 @@ import serial import time import sys +# Define the serial port SERIAL_PORT = '/dev/ttyUSB0' + +# Define the baud rate BAUD_RATE = 19200 +# Define commands SMARTTV_HELP = b'?\r' SMARTTV_ON = b'on\r' SMARTTV_OFF = b'off\r' @@ -19,12 +23,13 @@ SMARTTV_GET_INPUT = b'get input\r' SMARTTV_SET_INPUT_HDMI = b'set input=hdmi1\r' SMARTTV_SET_INPUT_OPS= b'set input=ops/hdmi2\r' +# Function to send command to TV def send_command(command): with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser: ser.write(command) - time.sleep(0.1) - reply = ser.read(100) - return reply.decode('utf-8') + time.sleep(0.1) # Wait for command to be sent + reply = ser.read(100) # Read up to 100 bytes of reply + return reply.decode('utf-8') # Decode bytes to string if __name__ == "__main__": if len(sys.argv) != 2: @@ -32,15 +37,37 @@ if __name__ == "__main__": sys.exit(1) command = sys.argv[1] + if "=" in command: + command_parts = command.split("=") + if len(command_parts) != 2 or command_parts[0] != "set_volume": + print("Usage: python3 smarttv.py set_volume=volume") + sys.exit(1) + volume = int(command_parts[1]) + if not 0 <= volume <= 100: + print("Volume must be between 0 and 100") + sys.exit(1) + volume_command = SMARTTV_SET_VOLUME % volume + print(send_command(volume_command)) + sys.exit(0) if command == "help": print(send_command(SMARTTV_HELP)) elif command == "power_on": - print(send_command(SMARTTV_ON)) + response = send_command(SMARTTV_ON) + if "owerstate=on" in response: + print('{"power_state":"on"}') + else: + print(send_command(SMARTTV_ON)) elif command == "power_off": - print(send_command(SMARTTV_OFF)) + response = send_command(SMARTTV_OFF) + if "owerstate=off" in response: + print('{"power_state":"off"}') + elif "powerstate=standby" in response: + print('{"power_state":"off"}') + else: + print(send_command(SMARTTV_OFF)) elif command == "get_state": print(send_command(SMARTTV_GET_STATE)) @@ -48,40 +75,67 @@ if __name__ == "__main__": elif command == "get_volume": print(send_command(SMARTTV_GET_VOLUME)) - elif command == "set_volume": - if len(sys.argv) != 3: - print("Usage: python3 smarttv.py set_volume volume") - sys.exit(1) - - volume = int(sys.argv[2]) - if not 0 <= volume <= 100: - print("Volume must be between 0 and 100") - sys.exit(1) - - volume_command = SMARTTV_SET_VOLUME % volume - print(send_command(volume_command)) - elif command == "get_mute": - print(send_command(SMARTTV_GET_MUTE)) + response = send_command(SMARTTV_GET_MUTE) + if "ute=off" in response: + print('{"mute":"off"}') + elif "ute=on" in response: + print('{"mute":"on"}') + else: + print(response) elif command == "mute": - print(send_command(SMARTTV_MUTE)) - + response = send_command(SMARTTV_MUTE) + if "ute=off" in response: + print('{"mute":"off"}') + elif "ute=on" in response: + print('{"mute":"on"}') + else: + print(response) + elif command == "unmute": - print(send_command(SMARTTV_UNMUTE)) + if "ute=off" in response: + print('{"mute":"off"}') + elif "ute=on" in response: + print('{"mute":"on"}') + else: + print(response) elif command == "get_powerstate": - print(send_command(SMARTTV_GET_POWERSTATE)) - + response = send_command(SMARTTV_GET_POWERSTATE) + if "owerstate=on" in response: + print('{"power_state":"on"}') + elif "owerstate=off" in response: + print('{"power_state":"off"}') + elif "owerstate=standby" in response: + print('{"power_state":"off"}') + else: + print(response) + elif command == "get_input": - print(send_command(SMARTTV_GET_INPUT)) - + response = send_command(SMARTTV_GET_INPUT) + if "nput=ops/hdmi2" in response: + print("OPS") + elif "nput=hdmi1" in response: + # print("HDMI") + print('{"input":"HDMI"}') + else: + print(send_command(SMARTTV_GET_INPUT)) + elif command == "set_input_hdmi": - print(send_command(SMARTTV_SET_INPUT_HDMI)) - + response = send_command(SMARTTV_SET_INPUT_HDMI) + if "nput=hdmi1" in response: + print("HDMI") + else: + print(send_command(SMARTTV_SET_INPUT_HDMI)) + elif command == "set_input_ops": - print(send_command(SMARTTV_SET_INPUT_OPS)) + response = send_command(SMARTTV_SET_INPUT_OPS) + if "nput=ops/hdmi2" in response: + print("OPS") + else: + print(send_command(SMARTTV_SET_INPUT_OPS)) else: print("Invalid command. Usage: python3 smarttv.py [power_on|power_off|get_volume|set_volume=volume]") - sys.exit(1) + sys.exit(1) \ No newline at end of file