141 lines
4.4 KiB
Python
141 lines
4.4 KiB
Python
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'
|
|
SMARTTV_GET_MUTE = b'get mute\r'
|
|
SMARTTV_MUTE = b'set mute=on\r'
|
|
SMARTTV_UNMUTE = b'set mute=off\r'
|
|
SMARTTV_GET_STATE = b'get powerstate\r'
|
|
SMARTTV_GET_VOLUME = b'get volume\r'
|
|
SMARTTV_SET_VOLUME = b'set volume=%d\r'
|
|
SMARTTV_GET_POWERSTATE = b'get powerstate\r'
|
|
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) # 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:
|
|
print("Usage: python3 smarttv.py [power_on|power_off|get_volume|set_volume=volume]")
|
|
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":
|
|
response = send_command(SMARTTV_ON)
|
|
if "owerstate=on" in response:
|
|
print('{"power_state":"on"}')
|
|
else:
|
|
print(send_command(SMARTTV_ON))
|
|
|
|
elif command == "power_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))
|
|
|
|
elif command == "get_volume":
|
|
print(send_command(SMARTTV_GET_VOLUME))
|
|
|
|
elif command == "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":
|
|
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":
|
|
if "ute=off" in response:
|
|
print('{"mute":"off"}')
|
|
elif "ute=on" in response:
|
|
print('{"mute":"on"}')
|
|
else:
|
|
print(response)
|
|
|
|
elif command == "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":
|
|
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":
|
|
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":
|
|
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) |