Skip to content

Instantly share code, notes, and snippets.

@dim-geo
Forked from fliphess/wol.py
Last active March 18, 2021 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dim-geo/f032161327e17a19d469a77440e8a2bc to your computer and use it in GitHub Desktop.
Save dim-geo/f032161327e17a19d469a77440e8a2bc to your computer and use it in GitHub Desktop.
Wake up post 2016 Philips TV's using Wake on Lan (on Wifi, WoWLAN)
#!/usr/bin/env python3
import struct
import re
import socket
import sys
import subprocess
#input mac
MAC = ''
def command(argument):
#print(argument)
try:
data=subprocess.run(argument.split(),check=True,capture_output=True,encoding="utf-8")
return data
except:
raise
def wake_on_lan(mac):
if len(mac) == 12:
pass
elif len(mac) == 12 + 5:
mac = mac.replace(mac[2], '')
else:
raise ValueError('Incorrect MAC address format')
data = ''.join(['FFFFFFFFFFFF', mac * 16])
# Split up the hex values and pack.
send_data = b''
for i in range(0, len(data), 2):
send_data = b''.join([send_data, struct.pack('B', int(data[i: i + 2], 16))])
# Broadcast it to the LAN.
for _ in range(15):
sock = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)
#maybe you need to send the packet from ip that belogns to the same subnet as the tv
#sock.bind(('192.168.x.x',0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#You must use this broadcast IP
sock.sendto(send_data, ('255.255.255.255', 9))
#After WOL, you need to issue remote command to wake the TV, via pylips.
#So the flowto wake the TV, should be:
#wol until ping works, then check powerstate
#if powerstate is ok, then power it on
if __name__ == '__main__':
# mac = get_mac_address(sys.argv[1])
#print(mac)
flag=True
while flag:
#print(flag)
try:
#modify IP to your TV
print(command('ping -c1 192.168.x.x').stdout)
#check state
data=command('python3 /root/pylips/pylips.py --command powerstate')
print(data.stdout)
if "On" in data.stdout:
flag=False
continue
if "Standby" not in data.stdout:
continue
data=command('python3 /root/pylips/pylips.py --command standby')
print(data)
if 'Ok' in data.stdout:
flag=False
except:
for _ in range(20):
wake_on_lan(MAC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment