Skip to content

Commit

Permalink
Added Roku initialization and discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Miller committed Mar 18, 2016
1 parent 972ca13 commit f3e5e7b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
38 changes: 37 additions & 1 deletion rokucli/cli.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
import argparse
from roku import Roku
from discover import discover_roku

class RokuCLI():
""" Command-line interpreter for processing user input and relaying
commands to Roku """
def __init__(self):
pass

def parseargs(self):
parser = argparse.ArgumentParser(
description='Command-line control of Roku devices')
parser.add_argument(
'ipaddr',
nargs='?',
help=('IP address of Roku to connect to. By default, will ' +
'automatically detect Roku within LAN.'))
return parser.parse_args()

def run(self):
ipaddr = self.parseargs().ipaddr

# If IP not specified, use Roku discovery and let user choose
if ipaddr:
roku = Roku(ipaddr)
else:
roku = discover_roku()

# Main interactive loop
print('Running')
while True:
roku.home()
break


def main():
print("Roku main")
RokuCLI().run()
37 changes: 37 additions & 0 deletions rokucli/discover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from roku import Roku

def discover_roku():
""" Search LAN for available Roku devices. Returns a Roku object. """

print("Searching for Roku devices within LAN ...")
rokus = Roku.discover()
if not rokus:
print("Unable to discover Roku devices. " +
"Try again, or manually specify the IP address with " +
"\'roku <ipaddr>\' (e.g. roku 192.168.1.130)")
return None

print("Found the following Roku devices:")
for i,r in enumerate(rokus):
print("[" + str(i+1) + "] " + str(r.host) + ":" + str(r.port))
print("")

if len(rokus) == 1:
print("Selecting Roku 1 by default")
return rokus[0]
else:
print("Multiple Rokus found. Select the index of the Roku to control:")

while True:
try:
query = "Select (1 to " + str(len(rokus)) + ") > "
sel = int(raw_input(query)) - 1
if sel >= len(rokus):
raise ValueError
else:
break
except ValueError:
print("Invalid selection")

return rokus[sel]

0 comments on commit f3e5e7b

Please sign in to comment.