Sure. This is what I did. And it works for pre-WebOS models.
Particular script I use is for models released in 2011. Also have script for 2012 models.
In configuration.yaml add
[color=#0080FF][b]
script.powerofftv:
hidden: true
script.powerontv:
hidden: true
#################################################################
Shell commands calling tv.py for LG TV
#################################################################
Exposes service shell_command.wol_bd_br
shell_command:
wol_bd_br: wakeonlan EE:BB:BB:EE:00:FF
change_to_tv: python3 /home/pi/.homeassistant/tv.py 15
power_off_tv: python3 /home/pi/.homeassistant/tv.py 8
State TV
input_boolean:
television:
name: Bedroom
initial: off
icon: mdi:television
group:
Television:
- input_boolean.television
#################################################################
Automations Television
#################################################################
automation 3:
-
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
from: ‘on’
to: ‘off’
action:
service: script.powerofftv
#################################################################
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
Turns TV off
powerofftv:
alias: poweroff_tv
sequence:
- alias: ‘turn off tv’
service: shell_command.power_off_tv[/b][/color]
In the tv.py add your pairing key and your good to go. Of course restart HASS
This is the tv.py script.
[color=#0080FF]#!/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\"?>AuthKeyReq”
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\"?>AuthReq”
+ lgtv[“pairingKey”] + “”
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\"?>”
+ lgtv[“session”]
+ “HandleKeyInput”
+ cmdcode
+ “”
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)
[/color]
Until someone works the script into a component I’ll keep using this and add shell commands for volume , channel changing, etc.
Turning TV on and off via HASS was what I needed.