-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsamsung-smart-tv-remote.py
executable file
·63 lines (53 loc) · 1.79 KB
/
samsung-smart-tv-remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import configparser
import pika
import json
import time
from samsungtvws import SamsungTVWS
# Load configuration
config = configparser.ConfigParser()
config.read('config.ini')
# Setup CloudAMQP connection
params = pika.URLParameters(config['CloudAMQP']['url'])
params.socket_timeout = 5
connection = pika.BlockingConnection(params) # Connect to CloudAMQP
channel = connection.channel() # Start a channel
# Samsung TV configuration
config_remote = {
"host": config['SamsungSmartTV']['host'],
"port": int(config['SamsungSmartTV']['port']),
"timeout": 0,
}
# Function to change the TV channel
def change_channel(channel_number):
with SamsungTVWS(host=config_remote['host'], port=config_remote['port']) as remote:
for digit in str(channel_number):
print(f"Sending digit: {digit}")
remote.send_key(f"KEY_{digit}")
time.sleep(0.5)
remote.send_key("KEY_ENTER")
print(f"Channel changed to {channel_number}")
# Function to turn off the TV
def turn_off_tv():
with SamsungTVWS(host=config_remote['host'], port=config_remote['port']) as remote:
print("Shutting down the TV")
remote.send_key("KEY_POWER")
# Callback function for incoming messages
def callback(ch, method, properties, body):
message = json.loads(body)
command = message.get('command')
value = message.get('value')
if command == "CHANGE_CHANNEL":
change_channel(value)
elif command == "TURN_OFF":
turn_off_tv()
else:
print(f"No command implemented for: {command}")
# Set up subscription to the queue
channel.basic_consume(
queue=config['CloudAMQP']['queue'],
on_message_callback=callback,
auto_ack=True
)
print("Waiting for commands...")
channel.start_consuming() # Start consuming (blocks)
connection.close()