forked from akhan23wgu/amazon-alexa-lg-tv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
alexa-tv.py
108 lines (96 loc) · 4.24 KB
/
alexa-tv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
""" fauxmo_minimal.py - Fabricate.IO
This is a demo python file showing what can be done with the debounce_handler.
The handler prints True when you say "Alexa, device on" and False when you say
"Alexa, device off".
If you have two or more Echos, it only handles the one that hears you more clearly.
You can have an Echo per room and not worry about your handlers triggering for
those other rooms.
The IP of the triggering Echo is also passed into the act() function, so you can
do different things based on which Echo triggered the handler.
"""
import fauxmo
import logging
import time
import os
from debounce_handler import debounce_handler
logging.basicConfig(level=logging.DEBUG)
class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
TRIGGERS = {"tv": 52000, "plex":52001, "volume": 52002, "netflix": 52003, "playback": 52004, "prime video":52005, "hdmi1":52006, "hdmi2":52007, "hdmi3":52008, "vudu":52009}
def act(self, client_address, state, name):
print "State", state, "on ", name, "from client @", client_address
if name == "tv" and state == True:
os.system("python lgtv.py on")
print "Magic packet sent to turn on TV!"
elif name == "tv" and state == False:
os.system("python lgtv.py off")
print "TV turned off!"
elif name == "hdmi1" and state == True:
os.system("python lgtv.py setInput HDMI_1")
print "Input set to HDMI 1"
elif name == "hdmi2" and state == True:
os.system("python lgtv.py setInput HDMI_2")
print "Input set to HDMI 2"
elif name == "hdmi3" and state == True:
os.system("python lgtv.py setInput HDMI_3")
print "Input set to HDMI 3"
elif name == "plex" and state == True:
os.system("python lgtv.py startApp cdp-30")
print "Launched Plex"
elif name == "plex" and state == False:
os.system("python lgtv.py closeApp cdp-30")
print "Closed Plex"
elif name == "prime video" and state == True:
os.system("python lgtv.py startApp amazon.html")
print "Launched Amazon Prime Video"
elif name == "prime video" and state == False:
os.system("python lgtv.py closeApp amazon.html")
print "Closed Amazon Prime Video"
elif name == "vudu" and state == True:
os.system("python lgtv.py startApp vudu")
print "Launched Vudu"
elif name == "vudu" and state == False:
os.system("python lgtv.py closeApp vudu")
print "Closed Vudu"
elif name == "netflix" and state == True:
os.system("python lgtv.py startApp netflix")
print "Launched Netflix"
elif name == "netflix" and state == False:
os.system("python lgtv.py closeApp netflix")
print "Closed Netflix"
elif name == "volume" and state == True:
os.system("python lgtv.py setVolume 44")
print "Volume set to FOURTYFOUR"
elif name == "volume" and state == False:
os.system("python lgtv.py setVolume 0")
print "Volume set to ZERO"
elif name == "playback" and state == True:
os.system("python lgtv.py inputMediaPlay")
print "Playback set to RESUME"
elif name == "playback" and state == False:
os.system("python lgtv.py inputMediaPause")
print "Playback set to PAUSE"
return True
if __name__ == "__main__":
# Startup the fauxmo server
fauxmo.DEBUG = True
p = fauxmo.poller()
u = fauxmo.upnp_broadcast_responder()
u.init_socket()
p.add(u)
# Register the device callback as a fauxmo handler
d = device_handler()
for trig, port in d.TRIGGERS.items():
fauxmo.fauxmo(trig, u, p, None, port, d)
# Loop and poll for incoming Echo requests
logging.debug("Entering fauxmo polling loop")
while True:
try:
# Allow time for a ctrl-c to stop the process
p.poll(100)
time.sleep(0.1)
except Exception, e:
logging.critical("Critical exception: " + str(e))
break