Skip to content

square-spade/samsung_multiroom

 
 

Repository files navigation

Samsung Multiroom (WIP)

Control Samsung Multiroom speakers.

Installation

pip install samsung_multiroom

Example speaker control

Initialise

from samsung_multiroom import SamsungMultiroomSpeaker

# initialise (replace with your speaker's ip address)
speaker = SamsungMultiroomSpeaker('192.168.1.129')

# get speaker name
speaker.get_name()

Basic functions

# get/set volume
volume = speaker.get_volume()
print(volume)

speaker.set_volume(10)

# switch source to connect with your samsung tv
speaker.set_source('soundshare')

# mute/unmute
speaker.mute()
speaker.unmute()

Audio source browsers

# browse dlna device called nas
browser = speaker.service('dlna').browser
# or shorter
browser = speaker.browser('dlna')
browser = browser.browse('/nas/Music/By Folder/Air/Moon Safari/CD 1')

for item in browser:
    print(item.object_type, item.object_id, item.artist, '-',  item.name)


# browse TuneIn radios
browser = speaker.service('tunein').browser
browser = browser.browse('/Trending/')

for item in browser:
    print(item.object_type, item.object_id, item.name)

App integrations

# check available services
names = speaker.get_services_names()
print(names)

# authenticate (unless you've done it already via mobile app)
speaker.service('Deezer').login('your email', 'your password')

browser = speaker.service('Deezer').browser
browser = browser.browse('/Browse/Rock/Artists/Queen')

for item in browser:
    print(item.object_type, item.object_id, item.name)

Player functions

# create playlist from browser items (see above) and play
speaker.player.play(browser)

# pause/resume
speaker.player.pause()
speaker.player.resume()

# repeat mode
from samsung_multiroom import REPEAT_ALL, REPEAT_ONE, REPEAT_OFF
speaker.player.repeat(REPEAT_ALL)

# get track info
track = speaker.player.get_current_track()
print(track)

Equalizer functions

# get preset names
presets = speaker.equalizer.get_presets_names()
print(presets)

# set predefined equalizer settings
speaker.equalizer.set('Pop')

# set adhoc settings
speaker.equalizer.set([4,3,2,1,2,3,0]) # <-6, 6>

# overwrite current preset
speaker.equalizer.save()

# ... or save as a new preset
speaker.equalizer.save('Experimental')

Clock functions

# set alarm
browser = speaker.service('tunein').browser
browser = browser.browse('/Trending/')

speaker.clock.alarm.slot(0).set(
    time='17:28',
    weekdays=[0,1,5], # Mon, Tue, Fri
    playlist=browser,
)

# enable/disable alarm 0
speaker.clock.alarm.slot(0).enable()
speaker.clock.alarm.slot(0).disable()

# sleep after 30 seconds
speaker.clock.timer.start(300)

remaining_time = speaker.clock.timer.get_remaining_time()
print(remaining_time)

Speaker discovery

from samsung_multiroom import SamsungSpeakerDiscovery
speakers = SamsungSpeakerDiscovery().discover() # takes some time

for s in speakers:
    print(s.get_name(), '@', s.ip_address)

Speaker grouping

# (after speaker discovery)
main_speaker = speakers[0]
rest_speakers = speakers[1:]

speaker_group = main_speaker.group('My first group', rest_speakers)

# now use speaker group like a speaker
speaker_group.set_volume(10)

browser = speaker_group.service('dlna').browser
browser = browser.browse('/nas/Music/By Folder/Air/Moon Safari/CD 1')

speaker_group.player.play(browser)

Events (preview)

You can monitor events emitted by the speaker without polling. Full list of supported events can be found in samsung_multiroom/event/type/.

import asyncio

from samsung_multiroom import SamsungMultiroomSpeaker


# listener will be passed an Event object (see samsung_multiroom/event/type/)
def listener(event):
    print(event.name, event)


async def main():
    speaker = SamsungMultiroomSpeaker('192.168.1.129')
    event_loop = speaker.event_loop
    event_loop.add_listener('*', listener)

    await event_loop.loop()


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
# listen to all events
event_loop.add_listener('*', listener)

# listen to events within a namespace
event_loop.add_listener('speaker.service.*', listener)

# listen to a single event
event_loop.add_listener('speaker.service.changed', listener)

License

MIT License

Copyright (c) 2018 Krystian Galutowski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Samsung multiroom speaker API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.5%
  • Makefile 0.5%