Created
July 20, 2018 19:09
-
-
Save Scarysize/303afcdb29fda8071847b684c7750c56 to your computer and use it in GitHub Desktop.
Toggle Sonos speaker between two volume settings using an Amazon dash button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import soco | |
from scapy.all import * | |
# MAC address of your Amazon dash button | |
DASH_MAC_ADDRESS = '' | |
# Name of your Sonos speaker | |
SPEAKER_NAME = '' | |
# Volume values to toggle between | |
TV_VOLUME = 23 | |
MUSIC_VOLUME = 5 | |
def get_speaker(): | |
for speaker in soco.discover(): | |
if speaker.player_name == SPEAKER_NAME: | |
return speaker | |
return None | |
def is_tv_volume(volume): | |
dist_tv = abs(volume - TV_VOLUME) | |
dist_music = abs(volume - MUSIC_VOLUME) | |
return dist_tv < dist_music | |
def detect_button(pkt): | |
if pkt.haslayer(DHCP) and pkt[Ether].src == DASH_MAC_ADDRESS: | |
print("Button Press Detected") | |
speaker = get_speaker() | |
if is_tv_volume(speaker.volume): | |
print('switching to music') | |
speaker.volume = MUSIC_VOLUME | |
else: | |
print('switching to tv') | |
speaker.volume = TV_VOLUME | |
sniff(prn=detect_button, filter="(udp and (port 67 or 68))", store=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment