DEV Community

Cover image for I Created a Python Script To Control my ROKU TV
Humberto David Esquerra
Humberto David Esquerra

Posted on

I Created a Python Script To Control my ROKU TV

Preface

For those that don’t know what a Roku Tv is, it’s a Tv that comes with software that can play different popular streaming services like Netflix, Hulu, Disney+, services like that. You can also download an App onto your phone that can control the volume, channels, video outputs, and movement on screen. Although I did find the app very useful, I would often find myself taking my hands off my laptop to use the app on my phone to interact with it. So, like a software developer would, I created a CLI (command line interface) that can interreact with the TV. That way it would be easier for me use the device. In this article I really want to talk about the process I took to go from knowing nothing about the product, to being able to manipulate it through the command line.

Oh, and also, I will only be talking about how I discovered how to work with the ROKU device and the logical parts that went into the script, the GUI and CLI part will be saved for a different article.

First, I had to figure out how it works.

At first, I had no idea how the Roku platform worked, So I investigated with a simple google search for things like, how does Roku work? how does Roku interact with the app? Is there a Roku developer page? After a few searches I finally found this page

https://developer.roku.com/docs/developer-program/debugging/external-control-api.md

This is one the developer pages talking about ECP which is their protocol for allowing a device to be controlled over a LAN network. Upon further investigation I discovered that these devices can be controlled be making an empty POST request to the correct socket (socket meaning IP address with port number) with a specific URL route. For example, let’s say your devices socket is 199.111.255.9:1234 to make the device increase its volume you would make a post request to that socket with the URL ending VolumeUp. Here’s an example of me making a Roku device increase its volume, navigate down, and another one navigation home. I will be using the Linux command curl to achieve this.

curl -d '' "http://199.111.255.9:1234/keypress/VolumeUp"
curl -d '' "http://199.111.255.9:1234/keypress/Down"
curl -d '' "http://199.111.255.9:1234/keypress/Home"
Enter fullscreen mode Exit fullscreen mode

As you can see using curl with the -d flag it’s really easy to control a Roku device. Also notice that the post request has nothing in the body.

It works now time to build the CLI!

Now that we understand how to control our ROKU Tv over our network it’s time to make it works in a python script. That way I don’t have to constantly use the curl command alone to work with the TV.

First, I wanted to come up with the logic to get everything working. I wanted to create a proof of concept, so I started by creating the commands and making sure they work. I decided to create methods in a python class and later binding that class to a variable for easy access. I also wanted to use try, except statements for easy troubleshooting. All in all, I ended up coming up with this.

Image description

I needed to import the OS command as CURL relies on the Linux Operating System. I then initialized the class to a variable, used the method, and it worked! From here the entire process became simple.

Simple! Now I’ll add more commands and class attributes.

Now that we’ve proven our proof of concept everything became simple ill just add more commands for what I want the ROKU device to do like movement and volume. I also wanted to add properties to the class that would reference the IP address of the device and the port as well. All in all, that looked like this. Nice and neat I’d say.

Image description

Time to wrap everything up.

Now that most of the logic is complete its time to bind the class to a variable and make it run. First, I created a variable called my_device and assigned it to the class, while giving the arguments for the Ip address and port number like this.

my_device = Roku ('112.111.647.9', '1234')
Enter fullscreen mode Exit fullscreen mode

Like I said I wanted to just go over the discovery of working with the technology and how I got it all to work with python. If you’re interested in seeing the entire source code, with GUIand CLI I’ve attached the link down below.

Thank you for reading my article!

The source code to this project contains an actual CLI and a GUI that I’ve created. I just wanted to go over the barebones project and the brunt of the logic here.

Also, you can check it out here at Davidfree2/roku_tv

And as always thanks for reading! I hope you found this article enjoyable and learned something in the process. If there’s something that you feel like I’ve left out then comment down below, or message me directly. Also leave a follow if you found this article informative! :)

What I’m up to lately.

My names David and I’ve been working on software web development for about 2 years now. Mostly free lance work. I know Python, JavaScript, Bash, CSS, HTML, PostgreSQL, MongoDB, and many other technologies. If you want to get in touch check me out here at davidesquerra.com. At this website you can text me directly or email me. And like I said above thanks for reading :).

Top comments (1)

Collapse
 
sectasy0 profile image
sectasy

Good article, but my question is: Why you send http request through curl while you can send that request from requests python package?