In configuration.yaml add
#################################################################
## Customize
#################################################################
customize:
script.powerofftv:
hidden: true
script.powerontv:
hidden: true
**
Shell commands calling tv.py for LG TV
**
Exposes service shell_command.wol_bd_br
#################################################################
## Shell commands
#################################################################
shell_command:
change_to_tv: python3 /home/pi/.homeassistant/tv.py 15
power_off_tv: python3 /home/pi/.homeassistant/tv.py 8
wol_bd_br: wakeonlan EE:BB:BB:EE:00:FF
**
State TV
**
input_boolean:
television:
name: Bedroom
initial: off
icon: mdi:television
group:
Television:
- input_boolean.television
**
Automations Television
**
#################################################################
## Automations
#################################################################
automation:
- alias: 'turn on tv'
trigger:
platform: state
entity_id: input_boolean.television
state: 'on'
action:
service: script.powerontv
- alias: 'turn off tv'
trigger:
platform: state
entity_id: input_boolean.television
state: 'off'
action:
service: script.powerofftv
**
Scripts
**
#################################################################
## Scripts
#################################################################
script:
# Turns on the TV and then changes input 20 seconds later
powerontv:
alias: poweron_tv
sequence:
- alias: 'turn on tv'
service: shell_command.wol_bd_br
- delay:
seconds: 20
- alias: 'change input to tv'
service: shell_command.change_to_tv
powerofftv:
alias: poweroff_tv
sequence:
- alias: 'turn off tv'
service: shell_command.power_off_tv
In the tv.py add your pairing key instead of DDDDDD copy and paste the entire script into a file named tv.py and your good to go. Of course restart HA
This is the tv.py script.
#!/usr/bin/env python3
import http.client
from tkinter import *
import xml.etree.ElementTree as etree
import socket
import re
import sys
lgtv = {}
dialogMsg =""
headers = {"Content-Type": "application/atom+xml"}
lgtv["pairingKey"] = "DDDDDD"
def getip():
strngtoXmit = 'M-SEARCH * HTTP/1.1' + '\r\n' + \
'HOST: 239.255.255.250:1900' + '\r\n' + \
'MAN: "ssdp:discover"' + '\r\n' + \
'MX: 2' + '\r\n' + \
'ST: urn:schemas-upnp-org:device:MediaRenderer:1' + '\r\n' + '\r\n'
bytestoXmit = strngtoXmit.encode()
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.settimeout(3)
found = False
gotstr = 'notyet'
i = 0
ipaddress = None
sock.sendto( bytestoXmit, ('239.255.255.250', 1900 ) )
while not found and i <= 5 and gotstr == 'notyet':
try:
gotbytes, addressport = sock.recvfrom(512)
gotstr = gotbytes.decode()
except:
i += 1
sock.sendto( bytestoXmit, ( '239.255.255.250', 1900 ) )
if re.search('LG', gotstr):
ipaddress, _ = addressport
found = True
else:
gotstr = 'notyet'
i += 1
sock.close()
if not found : sys.exit("Lg TV not found")
return ipaddress
def displayKey():
conn = http.client.HTTPConnection( lgtv["ipaddress"], port=8080)
reqKey = "<?xml version=\"1.0\" encoding=\"utf-8\"?><auth><type>AuthKeyReq</type></auth>"
conn.request("POST", "/hdcp/api/auth", reqKey, headers=headers)
httpResponse = conn.getresponse()
if httpResponse.reason != "OK" : sys.exit("Network error")
return httpResponse.reason
def getSessionid():
conn = http.client.HTTPConnection( lgtv["ipaddress"], port=8080)
pairCmd = "<?xml version=\"1.0\" encoding=\"utf-8\"?><auth><type>AuthReq</type><value>" \
+ lgtv["pairingKey"] + "</value></auth>"
conn.request("POST", "/hdcp/api/auth", pairCmd, headers=headers)
httpResponse = conn.getresponse()
if httpResponse.reason != "OK" : return httpResponse.reason
tree = etree.XML(httpResponse.read())
return tree.find('session').text
def getPairingKey():
displayKey()
root = Tk()
root.withdraw()
dialogMsg = "Please enter the pairing key\nyou see on your TV screen\n"
d = MyDialog(root, dialogMsg)
root.wait_window(d.top)
lgtv["pairingKey"] = result
d.top.destroy()
def handleCommand(cmdcode):
conn = http.client.HTTPConnection( lgtv["ipaddress"], port=8080)
cmdText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><command><session>" \
+ lgtv["session"] \
+ "</session><type>HandleKeyInput</type><value>" \
+ cmdcode \
+ "</value></command>"
conn.request("POST", "/hdcp/api/dtv_wifirc", cmdText, headers=headers)
httpResponse = conn.getresponse()
#main()
lgtv["ipaddress"] = getip()
theSessionid = getSessionid()
while theSessionid == "Unauthorized" :
getPairingKey()
theSessionid = getSessionid()
if len(theSessionid) < 8 : sys.exit("Could not get Session Id: " + theSessionid)
lgtv["session"] = theSessionid
#displayKey()
result = str(sys.argv[1])
handleCommand(result)