# File: tv_power.py
# Title: Sharp AQUOS power button script
#
# Brief:
# Connects to the TV over LAN and toggles it on/off.
#
# TV must first be set up to connect to wi-fi and enable remote access (via on-screen menu),
# and have LAN turn-on enabled (by sending "RSPW2 " over putty or hyperterm).
# I use Intelliremote to kick off this script with my media center remote.
#
# Author: Chris Hammond (https://www.facebook.com/Chris.A.Hammond)
# Version: Jan 6, 2014
#
# Note: Other commands can be found on page 45 of this PDF:
# http://files.sharpusa.com/Downloads/ForHome/HomeEntertainment/LCDTVs/Manuals/tel_man_2013_fullline.pdf
#
# Tips: Bitcoin 1P15hKDPDqw5TkXWsWgZcg4QEH3WEcxMGM
# Dogecoin D7sqgTGt6ERQMuj7T5S3VfvSRrMHvCSSQQ
import socket
# Got this from my router's status page:
tv_hostname = "unknown9444449D0E9D"
# Got this from TV's setup menu:
tv_port = 10002
print "Connecting to TV..."
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_ip = socket.gethostbyname(tv_hostname)
s.connect((remote_ip,tv_port))
print "Connected."
# Get current power state:
s.sendall("POWR? \r")
ans = s.recv(10).rstrip()
if ans != '1' and ans != '0':
quit( "Failed! TV responded '%s' instead of '1' or '0'." % (ans,) )
print "Current power state: %s" % (ans,)
# Send opposite power state:
cmd = "POWR%d \r" % (ans == '0')
print "Sending '%s'..." % (cmd.rstrip(),)
s.sendall(cmd)
ans = s.recv(10).rstrip()
if ans != 'OK':
quit( "Failed! TV responded '%s' instead of 'OK'." % (ans,) )
print "Success!"