-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_stream.py
68 lines (47 loc) · 1.75 KB
/
play_stream.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
import datetime
import soco
from soco.snapshot import Snapshot
import sys
import requests
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
target_volume = 30
sonos_player_name = "Master Bedroom"
class UndiscoverablePlayerError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def discover_target_sonos_player(target_name):
for zone in soco.discover():
if zone.player_name == target_name:
return zone
raise UndiscoverablePlayerError(target_name)
def button_pressed_npr():
try:
player = discover_target_sonos_player(sonos_player_name)
except UndiscoverablePlayerError as e:
print "Can't find the target Sonos player "+e.value
sys.exit(0)
sonos = soco.SoCo(player.ip_address)
sonos.volume = target_volume
print "Playing KQED on the " + player.player_name + " player at " + player.ip_address
# The title is needed, oddly...
sonos.play_uri('x-sonosapi-stream:s34804?sid=254&flags=32', title='KQED')
# Just in case...
sonos.volume = target_volume
def udp_filter(pkt):
options = pkt[DHCP].options
for option in options:
if isinstance(option, tuple):
if 'requested_addr' in option:
# we've found the IP address, which means its the second and final UDP request, so we can trigger our action
mac_to_action[pkt.src]()
break
mac_to_action = {'74:c2:46:4a:52:af' : button_pressed_npr}
mac_id_list = list(mac_to_action.keys())
print "Listening for Dash button presses..."
sniff(prn=udp_filter, store=0, filter="udp", lfilter=lambda d: d.src in mac_id_list)
if __name__ == "__main__":
main()