Shortcuts for Samsung Smart TV (since 2016) with Python
Hey guys,
I tried to figure out how I could control my Samsung Smart TV using Siri/Shortcuts without having any sort of Homebridge/Apple TV/Homepod etc. I figured that all the remote control apps from the App Store use the "WebSocket" Protocoll to send Commands from a device to control the TV. Based on this Information I tried to figure how I could possibly create WebSocket Calls using the Shortcuts App. Scriptable could have been a Solution, but unfortunately Apples JavaScriptCore (Scriptable is based on) seems to not support WebSockets yet? Instead I tried Python, beacuse apperently we can run Python Scripts on iOS (this could be a big Deal for all Sorts of Scripts?). The Construtctor Method throws a ReferenceError: new WebSocket()
Based on this GitHub Project I wrote the following Pythong Script using the App Store App "Pyto - Python 3", which mutes/unmutes the TVs audio:
import websocket
import json
import time
KEY_INTERVAL = 0.2
def send_key(key, repeat=1):
for n in range(repeat):
url = 'ws://TV IP:TV PORT/api/v2/channels/samsung.remote.control'
connection = websocket.create_connection(url)
payload = json.dumps({
'method': 'ms.remote.control',
'params': {
'Cmd': 'Click',
'DataOfCmd': key,
'Option': 'false',
'TypeOfRemote': 'SendRemoteKey'
}
})
connection.send(payload)
time.sleep(KEY_INTERVAL)
send_key("KEY_MUTE")
A list of the Keys, which can be used to control the TV can be found in the GitHub Project Source. Now you just need to find out the IP Address of your TV, which might be displayed somewhere in the TV Settings. I could find it unsing the TV Remote Control App "myTifi". The port might be a default Setting for every TV. In my case (Samsung Q7F) the Port is 8001.
Edit: it is important that you set a static IP Address in the Network Settings of your Smart TV. Otherwise it is possible, that the TV will receive a different IP address, which means that your Scripts will not be able to send the Commands.
After you created the Python Script you can click on the Settings Icon in the bottom right corner to create a Shortcut for this Script. I created multiple Scripts, where every script sends a different Key (mute, power, volume up/down...). It might be possible to have one Script and pass the key to send from the shortcut itself as an Argument to the Python Script. Using multiple Scripts was they fast and easy (and dirty?) way for me.
A Con of this Method could be the cost of the Pyto App, which seems to be 10,99 € for the full version. I am not sure whether the Lite Version (3,49 € ) lets you create Shortcuts. At the Moment I am using the 3 Days free trial.
Let me know what you think about this Method.
Posted Dec 29, 2020, 5:18 PM UTC · edited