Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappydinoa committed May 27, 2018
1 parent f414eb1 commit 5aa35ed
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 43 deletions.
19 changes: 14 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Usage
aquos.off() # Puts TV into standby mode
aqous.set_standbymode # Enables standby mode
aqous.set_standbymode() # Enables standby mode
aquos.play() # Play button (⏯)
Expand Down Expand Up @@ -81,8 +81,16 @@ Usage
aquos.set_input(x) # Sets TV input to input x
Note: tv\_on function will work only after you turn off tv using this
function, then un-plug and re-plug-in the tv.
aquos.get_device_name() # Gets TV device name
aquos.get_model_name() # Gets TV model name
aquos.get_software_version() # Gets TV software version
aquos.get_ip_protocol_version() # Gets TV ip protocol
Note: on function will work only after you use the setup=True argument or after
you turn off tv using this function, then un-plug and re-plug-in the tv.

Example
-------
Expand All @@ -93,8 +101,9 @@ An example program would look like:
from aquosRemote.aquos import aquosTV
aquos = aquosTV('IP.ADD.RESS.XX') # Without Auth
aquos = aquosTV('IP.ADD.RESS.XX', 'username', 'password') # With Auth
aquos = aquosTV('IP.ADD.RESS.XX') # Without auth and setup
aquos = aquosTV('IP.ADD.RESS.XX', setup=True) # With setup
aquos = aquosTV('IP.ADD.RESS.XX', 'username', 'password') # With auth
aquos.tv_on()
...
Expand Down
102 changes: 65 additions & 37 deletions aquosRemote/aquos.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
import socket
from time import sleep
from contextlib import closing


class aquosTV(object):
def __init__(self, ip, port=10002, username=None, password=None):
def __init__(self, ip, port=10002, username=None, password=None, setup=False):
self.ip = str(ip)
self.port = int(port)
self.auth = (username and password)
self.username = username
self.password = password
if not self._check_ip():
if setup:
self.on()
sleep(1)
self.set_standbymode()
self.off()
raise Exception("Port %s is not open on %s" % (self.port, self.ip))

def _check_ip(self):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.settimeout(3)
return (sock.connect_ex((self.ip, self.port)) == 0)

def format_command(self, command):
if not command.endswith("\r"):
new_command = command
number = command[4:]
if number.isdigit():
new_command = command[:4] + self.format_number(number)
new_command += "\r"
return new_command
return command

def format_number(self, number):
return "% 4d" % int(number)

def send_command(self, command, byte_size=1024, timeout=3):
command = self.format_command(command)
# print(command)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.ip, self.port))
s.settimeout(timeout)
if (self.username and self.password):
if (self.auth):
s.send(self.username + "\r" + self.password + "\r")
s.send(command.encode('utf-8'))
msg = s.recv(byte_size)
return msg
return msg.strip()
except socket.error:
raise Exception("Error connecting to %s:%s" %
(self.ip, self.port))

def remote_number(self, number):
number = self.format_number(number)
return self.send_command("RCKY" + number)

def off(self):
return self.send_command("POWR0 \r")
return self.send_command("POWR0")

def set_standbymode(self):
return self.send_command("RSPW1 \r\r")
def set_standbymode(self, mode=1):
return self.send_command("RSPW" + self.format_number(mode))

def on(self):
return self.send_command("POWR1 \r")
return self.send_command("POWR1")

def play(self):
return self.remote_number(16)
Expand All @@ -56,13 +87,13 @@ def skip_back(self):
return self.remote_number(19)

def toggle_mute(self):
return self.send_command("MUTE0 \r")
return self.send_command("MUTE0")

def mute_on(self):
return self.send_command("MUTE1 \r")
return self.send_command("MUTE1")

def mute_off(self):
return self.send_command("MUTE2 \r")
return self.send_command("MUTE2")

def volume_up(self):
return self.remote_number(33)
Expand All @@ -81,20 +112,14 @@ def volume_repeat(self, number):
self.volume_up()
x += 1
else:
return "error"
return "OK\r"
return "ERR"
return "OK"

def set_volume(self, level):
if level > 9:
sLevel = str(level) + " "
elif level <= 9:
sLevel = str(level) + " "
elif level == 100:
sLevel = str(level) + " "

if (level <= 100 and level >= 0):
return self.send_command("VOLM" + sLevel + "\r")
return "error"
if (level <= 60 and level >= 0):
level = self.format_number(level)
return self.send_command("VOLM" + level)
return "ERR"

def up(self):
return self.remote_number(41)
Expand All @@ -120,31 +145,34 @@ def netflix(self):
return self.remote_number(59)

def set_input(self, input_name):
return self.send_command("IAVD" + str(input_name) + " \r")
return self.send_command("IAVD" + str(input_name))

def toggle_input(self):
return self.send_command("ITGD \r")
return self.send_command("ITGD1")

def remote_number(self, number):
if number > 9:
number = str(number) + " "
else:
number = str(number) + " "
return self.send_command("RCKY" + number + "\r")
def get_device_name(self):
return self.send_command("TVNM1")

def _check_ip(self):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.settimeout(3)
return (sock.connect_ex((self.ip, self.port)) == 0)
def get_model_name(self):
return self.send_command("MNRD1")

def get_software_version(self):
return self.send_command("SWVN1")

def get_ip_protocol_version(self):
return self.send_command("IPPV1")


if __name__ == "__main__":
# Example
# aquos = aquosTV("Basement TV", "192.168.1.2")
from time import sleep
aquos = aquosTV("192.168.1.2")
# aquos = aquosTV("192.168.1.2")
aquos = aquosTV("192.168.1.2", setup=True)
aquos.on()
sleep(1)
# print(aquos.get_device_name())
# print(aquos.get_model_name())
# print(aquos.get_software_version())
# print(aquos.get_ip_protocol_version())
aquos.set_volume(30)
sleep(1)
aquos.off()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="aquosRemote",
version="1.3.1",
version="1.4.0",
description="A small Python module for sending Remote Control Codes to your Sharp AQUOS Smart TV",
long_description=long_description,
url="https://github.com/thehappydinoa/aquosRemote",
Expand Down

0 comments on commit 5aa35ed

Please sign in to comment.