diff --git a/smarttv.py b/smarttv.py index a46091a..9b16b91 100644 --- a/smarttv.py +++ b/smarttv.py @@ -2,12 +2,30 @@ import serial import time import sys -# Define the serial port +# Serial configurations SERIAL_PORT = '/dev/ttyUSB0' - -# Define the baud rate BAUD_RATE = 19200 +USAGE_INFO = "Usage: python3 smarttv.py [power_on|power_off|get_state|get_volume|set_volume=volume|get_mute|mute|unmute|get_input|set_input_hdmi|set_input_ops]" + +USAGE_INF = """ +Usage: python3 smarttv.py [command] + +Commands: + power_on - Turn the smart TV on + power_off - Turn the smart TV off + get_state - Get the power state of the smart TV + get_volume - Get the current volume level of the smart TV + set_volume=volume - Set the volume level of the smart TV (0-100) + get_mute - Check if the smart TV is muted + mute - Mute the smart TV + unmute - Unmute the smart TV + get_input - Get the current input source of the smart TV + set_input_hdmi - Set the input source to HDMI + set_input_ops - Set the input source to OPS/HDMI2 + help - Display help information +""" + # Define commands SMARTTV_HELP = b'?\r' SMARTTV_ON = b'on\r' @@ -23,6 +41,14 @@ 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' +## MQTT +MQTT_IP = "locahost" +MQTT_PORT = 1883 +MQTT_USERNAME = "username" +MQTT_PASSWORD = "password" +MQTT_TOPIC_CMD = "smarttv/cmd" +MQTT_TOPIC_STATUS = "smarttv/status" + # Function to send command to TV def send_command(command): with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser: @@ -33,7 +59,7 @@ def send_command(command): if __name__ == "__main__": if len(sys.argv) != 2: - print("Usage: python3 smarttv.py [power_on|power_off|get_volume|set_volume=volume]") + print(USAGE_INFO) sys.exit(1) command = sys.argv[1] @@ -47,7 +73,10 @@ if __name__ == "__main__": print("Volume must be between 0 and 100") sys.exit(1) volume_command = SMARTTV_SET_VOLUME % volume - print(send_command(volume_command)) + response = send_command(volume_command) + if "olume=" in response: + volume_level = response.split('=')[1].rstrip('>').strip() + print('{"volume":' + volume_level + '}') sys.exit(0) if command == "help": @@ -73,7 +102,12 @@ if __name__ == "__main__": print(send_command(SMARTTV_GET_STATE)) elif command == "get_volume": - print(send_command(SMARTTV_GET_VOLUME)) + response = send_command(SMARTTV_GET_VOLUME) + if "olume=" in response: + volume_level = response.split('=')[1].rstrip('>').strip() + print('{"volume":' + volume_level + '}') + else: + print(response) elif command == "get_mute": response = send_command(SMARTTV_GET_MUTE) @@ -94,6 +128,7 @@ if __name__ == "__main__": print(response) elif command == "unmute": + response = send_command(SMARTTV_UNMUTE) if "ute=off" in response: print('{"mute":"off"}') elif "ute=on" in response: @@ -117,7 +152,6 @@ if __name__ == "__main__": 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)) @@ -137,5 +171,5 @@ if __name__ == "__main__": print(send_command(SMARTTV_SET_INPUT_OPS)) else: - print("Invalid command. Usage: python3 smarttv.py [power_on|power_off|get_volume|set_volume=volume]") + print("Invalid command.", USAGE_INFO) sys.exit(1) \ No newline at end of file