Update smarttv.py
This commit is contained in:
parent
26d9754498
commit
36c5b3923c
50
smarttv.py
50
smarttv.py
@ -2,12 +2,30 @@ import serial
|
|||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# Define the serial port
|
# Serial configurations
|
||||||
SERIAL_PORT = '/dev/ttyUSB0'
|
SERIAL_PORT = '/dev/ttyUSB0'
|
||||||
|
|
||||||
# Define the baud rate
|
|
||||||
BAUD_RATE = 19200
|
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
|
# Define commands
|
||||||
SMARTTV_HELP = b'?\r'
|
SMARTTV_HELP = b'?\r'
|
||||||
SMARTTV_ON = b'on\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_HDMI = b'set input=hdmi1\r'
|
||||||
SMARTTV_SET_INPUT_OPS= b'set input=ops/hdmi2\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
|
# Function to send command to TV
|
||||||
def send_command(command):
|
def send_command(command):
|
||||||
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
|
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
|
||||||
@ -33,7 +59,7 @@ def send_command(command):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) != 2:
|
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)
|
sys.exit(1)
|
||||||
|
|
||||||
command = sys.argv[1]
|
command = sys.argv[1]
|
||||||
@ -47,7 +73,10 @@ if __name__ == "__main__":
|
|||||||
print("Volume must be between 0 and 100")
|
print("Volume must be between 0 and 100")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
volume_command = SMARTTV_SET_VOLUME % volume
|
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)
|
sys.exit(0)
|
||||||
|
|
||||||
if command == "help":
|
if command == "help":
|
||||||
@ -73,7 +102,12 @@ if __name__ == "__main__":
|
|||||||
print(send_command(SMARTTV_GET_STATE))
|
print(send_command(SMARTTV_GET_STATE))
|
||||||
|
|
||||||
elif command == "get_volume":
|
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":
|
elif command == "get_mute":
|
||||||
response = send_command(SMARTTV_GET_MUTE)
|
response = send_command(SMARTTV_GET_MUTE)
|
||||||
@ -94,6 +128,7 @@ if __name__ == "__main__":
|
|||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
elif command == "unmute":
|
elif command == "unmute":
|
||||||
|
response = send_command(SMARTTV_UNMUTE)
|
||||||
if "ute=off" in response:
|
if "ute=off" in response:
|
||||||
print('{"mute":"off"}')
|
print('{"mute":"off"}')
|
||||||
elif "ute=on" in response:
|
elif "ute=on" in response:
|
||||||
@ -117,7 +152,6 @@ if __name__ == "__main__":
|
|||||||
if "nput=ops/hdmi2" in response:
|
if "nput=ops/hdmi2" in response:
|
||||||
print("OPS")
|
print("OPS")
|
||||||
elif "nput=hdmi1" in response:
|
elif "nput=hdmi1" in response:
|
||||||
# print("HDMI")
|
|
||||||
print('{"input":"HDMI"}')
|
print('{"input":"HDMI"}')
|
||||||
else:
|
else:
|
||||||
print(send_command(SMARTTV_GET_INPUT))
|
print(send_command(SMARTTV_GET_INPUT))
|
||||||
@ -137,5 +171,5 @@ if __name__ == "__main__":
|
|||||||
print(send_command(SMARTTV_SET_INPUT_OPS))
|
print(send_command(SMARTTV_SET_INPUT_OPS))
|
||||||
|
|
||||||
else:
|
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)
|
sys.exit(1)
|
Loading…
x
Reference in New Issue
Block a user