Skip to content

Instantly share code, notes, and snippets.

@guyzyl
Last active March 27, 2024 16:55
Show Gist options
  • Save guyzyl/36a99294d1471a11fdc934b7f10f51ae to your computer and use it in GitHub Desktop.
Save guyzyl/36a99294d1471a11fdc934b7f10f51ae to your computer and use it in GitHub Desktop.
A small server that locates all Sonos devices on the network, synchronizes their volume, and groups them with the device that is playing music
from typing import Optional, List
from flask import Flask
from soco import discover, SoCo
app = Flask(__name__)
@app.route("/normalize")
def normalize_volume(devices: Optional[List[SoCo]]=None) -> str:
devices = devices or list(discover())
min_volume = min([d.volume for d in devices])
for d in devices:
d.volume = min_volume
return ""
@app.route("/group")
def group_to_playing(devices: Optional[List[SoCo]]=None) -> str:
devices = devices or list(discover())
master_device = None
for d in devices:
if d.get_current_track_info()["title"]:
master_device = d
if master_device is None:
return
for d in devices:
if d == master_device:
continue
d.join(master_device)
return ""
@app.route("/")
def normalize_and_group() -> str:
devices = list(discover())
normalize_volume(devices)
group_to_playing(devices)
return ""
if __name__ == "__main__":
app.run(host="0.0.0.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment