soco.events module

Classes to handle Sonos UPnP Events and Subscriptions.

The Subscription class from this module will be used in soco.services unless config.EVENTS_MODULE is set to point to soco.events_twisted, in which case soco.events_twisted.Subscription will be used. See the Example in soco.events_twisted.

Example

Run this code, and change your volume, tracks etc:

from __future__ import print_function
try:
    from queue import Empty
except:  # Py2.7
    from Queue import Empty

import logging
logging.basicConfig()
import soco
from pprint import pprint
from soco.events import event_listener
# pick a device at random and use it to get
# the group coordinator
device = soco.discover().pop().group.coordinator
print (device.player_name)
sub = device.renderingControl.subscribe()
sub2 = device.avTransport.subscribe()

while True:
    try:
        event = sub.events.get(timeout=0.5)
        pprint (event.variables)
    except Empty:
        pass
    try:
        event = sub2.events.get(timeout=0.5)
        pprint (event.variables)
    except Empty:
        pass

    except KeyboardInterrupt:
        sub.unsubscribe()
        sub2.unsubscribe()
        event_listener.stop()
        break

soco.events_base module

Base classes used by soco.events and soco.events_twisted.

soco.events_twisted module

Classes to handle Sonos UPnP Events and Subscriptions.

The Subscription class from this module will be used in soco.services if config.EVENTS_MODULE is set to point to this module.

Example

Run this code, and change your volume, tracks etc:

from __future__ import print_function
import logging
logging.basicConfig()
import soco
from pprint import pprint

from soco import events_twisted
soco.config.EVENTS_MODULE = events_twisted
from twisted.internet import reactor

def print_event(event):
    try:
        pprint (event.variables)
    except Exception as e:
        pprint ('There was an error in print_event:', e)

def main():
    # pick a device at random and use it to get
    # the group coordinator
    device = soco.discover().pop().group.coordinator
    print (device.player_name)
    sub = device.renderingControl.subscribe().subscription
    sub2 = device.avTransport.subscribe().subscription
    sub.callback = print_event
    sub2.callback = print_event

    def before_shutdown():
        sub.unsubscribe()
        sub2.unsubscribe()
        events_twisted.event_listener.stop()

    reactor.addSystemEventTrigger(
        'before', 'shutdown', before_shutdown)

if __name__=='__main__':
    reactor.callWhenRunning(main)
    reactor.run()