Skip to content

Commit

Permalink
Made host parameter optional and added example to run the script for …
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Schneider committed Aug 14, 2014
1 parent 03427bc commit 567cb35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ A PC, with Python 3.x installed, connected to the same network as your Lg TV.

## Usage

See the available options with: `./lgcommander.py --help`
See the available options with: `./lgcommander.py --help` or `python3 lgcommander.py --help`

usage: lgcommander.py [-h] [-V] [-p PORT] [-P {roap,hdcp}] [-k PAIRING_KEY]
[-c COMMAND]
ip_address
usage: lgcommander.py [-h] [-V] [-H HOST] [-p PORT] [-P {roap,hdcp}]
[-k PAIRING_KEY] [-c COMMAND]

Control your Smart Lg TV with your PC

positional arguments:
ip_address IP address or FQDN of device. Use the special value
"scan" for a mulicast request for TVs in your LAN.

optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-H HOST, --host HOST IP address or FQDN of device. Use the special value
"scan" for a mulicast request for TVs in your LAN.
-p PORT, --port PORT TCP port (default is 8080)
-P {roap,hdcp}, --protocol {roap,hdcp}
Protocol to use. Currently ROSP and HDCP are
Expand Down
30 changes: 16 additions & 14 deletions lgcommander.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ class LgRemote:

def __init__(
self,
ip_address=None,
host=None,
port=8080,
protocol='hdcp'
):

self.port = port
self.ip_address = ip_address
if not ip_address:
self.host = host
if not self.host:
self.getip()
self._protocol = protocol
self._pairing_key = None
self._session_id = None

def getip(self):
if self.ip_address:
return self.ip_address
if self.host:
return self.host
strngtoXmit = 'M-SEARCH * HTTP/1.1' + '\r\n' + \
'HOST: 239.255.255.250:1900' + '\r\n' + \
'MAN: "ssdp:discover"' + '\r\n' + \
Expand All @@ -87,19 +87,19 @@ def getip(self):
sock.sendto(bytestoXmit, ('239.255.255.250', 1900))
if re.search('LG', gotstr):
logging.debug("Found device: %s", addressport)
self.ip_address, _ = addressport
self.host, _ = addressport
found = True
else:
gotstr = 'notyet'
i += 1
sock.close()
if not found:
raise socket.error("Lg TV not found.")
logging.info("Using device: %s", self.ip_address)
return self.ip_address
logging.info("Using device: %s", self.host)
return self.host

def display_key_on_screen(self):
conn = http.client.HTTPConnection(self.ip_address, port=self.port)
conn = http.client.HTTPConnection(self.host, port=self.port)
req_key_xml_string = self._xml_version_string + '<auth><type>AuthKeyReq</type></auth>'
logging.debug("Request device to show key on screen.")
conn.request(
Expand All @@ -122,7 +122,7 @@ def get_session_id(self, paring_key):
pair_cmd_xml_string = self._xml_version_string \
+ '<auth><type>AuthReq</type><value>' \
+ self._pairing_key + "</value></auth>"
conn = http.client.HTTPConnection(self.ip_address, port=self.port)
conn = http.client.HTTPConnection(self.host, port=self.port)
conn.request(
'POST',
"/{}/api/auth".format(self._protocol),
Expand Down Expand Up @@ -159,7 +159,7 @@ def handle_key_input(self, cmdcode):
+ "</session><type>HandleKeyInput</type><value>" \
+ cmdcode \
+ "</value></command>"
conn = http.client.HTTPConnection(self.ip_address, port=self.port)
conn = http.client.HTTPConnection(self.host, port=self.port)
conn.request(
"POST",
command_url_for_protocol[self._protocol],
Expand Down Expand Up @@ -192,7 +192,9 @@ def get_pairing_key_from_user(lg_remote):
version='%(prog)s {version}'.format(version=__version__)
)
args.add_argument(
'ip_address',
'-H',
'--host',
default='scan',
help=u"IP address or FQDN of device."
+ " Use the special value \"scan\" for a mulicast request for TVs in your LAN."
)
Expand Down Expand Up @@ -229,10 +231,10 @@ def get_pairing_key_from_user(lg_remote):
# level=logging.INFO,
)

logging.debug(None if user_parms.ip_address == 'scan' else user_parms.ip_address)
logging.debug(None if user_parms.host == 'scan' else user_parms.host)
try:
lg_remote = LgRemote(
ip_address=None if user_parms.ip_address == 'scan' else user_parms.ip_address,
host=None if user_parms.host == 'scan' else user_parms.host,
protocol=user_parms.protocol)
except socket.error as error:
raise SystemExit(error)
Expand Down

0 comments on commit 567cb35

Please sign in to comment.