-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Roku initialization and discovery
- Loading branch information
Nick Miller
committed
Mar 18, 2016
1 parent
972ca13
commit f3e5e7b
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|