Bose Soundtouch control

Post Reply
MikeF
Posts: 187
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: UK
Contact:

Bose Soundtouch control

Post by MikeF » Saturday 24 February 2018 13:31

I have a Bose Soundtouch 20 Series II (Airplay version), and I recently came across this python library https://github.com/CharlesBlonde/libsoundtouch for accessing and controlling it.

I have created two simple (i.e., can be improved!) python scripts: selector and volume control.

Selector
This script uses a Domoticz selector switch to select from the 6 stored presets; I have extended it to add two streaming URLs, as well as the Soundtouch's AUX input:
Image
(I'm using 10 levels - 0 to 90 - but this can be extended / reduced as required.)

The code (see later) takes a single argument (0 to 9), corresponding to the above levels, and the selector actions have been set up as follows:
Image
Obviously, change this to correspond to the names for your presets!

Code:
Spoiler: show

Code: Select all

#!/usr/bin/env python

from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
import sys

bose = '<your Soundtouch wifi address>'
# put your stream URLs here			
url1 = 'http://178.33.232.106:8034/stream' 			
url2 = 'http://direct.fipradio.fr/live/fip-webradio2.mp3'

device = soundtouch_device(bose)
device.power_on()
presets = device.presets()

# read arg
arg = int(sys.argv[1])
print arg

if arg == 0: # power off
	device.power_off()
	
if 1 <= arg <= 6: # presets 1-6
	device.select_preset(presets[arg-1])

if arg == 7: # HTTP URL (not HTTPS)
	device.play_url(url1)

if arg == 8: # HTTP URL (not HTTPS)
	device.play_url(url2)
	
if arg == 9: # AUX
	device.select_source_aux()
Save this with the name and location specified in the selector actions (I used /home/pi/devices/bose.py).

Volume control
Set up a dummy switch in Domoticz, and change it to type 'dimmer' (0 - 100%).
Image

This uses the following python script:
Spoiler: show

Code: Select all

#!/usr/bin/env python

from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
from time import sleep
import requests
import json

bose = '<your Soundtouch wifi address>'
volIdx = '<Domoticz idx for dimmer switch>'
url = 'http://<domoticz url:port>/json.htm?type=devices&rid=' + volIdx
oldStatus = ''

device = soundtouch_device(bose)
device.power_on()

def domoticzread(var):
	response = requests.get(url)
	jsonData = json.loads(response.text)
	result = jsonData['result'][0][var]
	return result

while 1:
	volume = domoticzread('Level')
	status = domoticzread('Status')
	
	if status != oldStatus: 
		#print volume, status
		if status == 'Off':
			device.set_volume(0)
		else:
			# status = 'On' or 'Set Level: <x> %'
			device.set_volume(volume)
	oldStatus = status
	
	sleep(0.2)
This needs to be run in background, e.g.,

Code: Select all

python /home/pi/devices/bose_volume.py &
or started on boot in crontab.

(In both scripts, insert your own values in angle brackets: <...>)

I'm interested in any comments / suggestions for improvement.
Last edited by MikeF on Friday 17 August 2018 0:43, edited 1 time in total.

PhJack
Posts: 1
Joined: Thursday 26 July 2018 22:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Bose Soundtouch control

Post by PhJack » Thursday 26 July 2018 22:08

Hi,
thank you for you post and sorry for my english approximate.
I used a script inspired by yours to turn on,select aux or turn off my bose soundbar from domoticz on a bananapi.
Howewer, it's been a few days that the "select aux" is not working anymore. I wonder if it's not due to an upgrade of firmware. Have you notice the same problem ?
I was realy happy having found this method thanks to your post since my attempt to duplicate the IR telecommand of the bose was a failure ...

Thank you for your help

MikeF
Posts: 187
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Saturday 28 July 2018 18:09

Selecting AUX with my script works OK for me. I’m using the libsoundtouch library, and calling device.select_source_aux() in my Python script.

I haven’t upgraded the firmware on my Bose - I’m reluctant to do so, as the model I bought (on eBay) is a Series II, which has AirPlay support, and I don’t want to lose that.

Furiousz23
Posts: 5
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Thursday 16 August 2018 18:37

I want to integrate my Bose Soundtouch into Domoticz, but cant figure out how to use this plugin. Can anybody point me in the right direction?

MikeF
Posts: 187
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Friday 17 August 2018 0:34

First of all, this is not a python plugin - I'm not familiar with the python plugin architecture - but a pair of python scripts which I knocked up quickly (sort of proof of concept).

1. Selector
The first one makes use of a selector switch in Domoticz (have a look at the Domoticz wiki if you're not au fait with this). It has 10 levels:
0 - Off
10 - 60: these match your Soundtouch presets
70 - 80: you can add 2 streaming URLs for additional radio stations here (these are url1 and url2 in bose.py)
90: selects AUX input.

You don't need 7 - 9, or you could add more levels.

Copy and save this as bose.py in your preferred location - I used the folder /home/pi/devices/ on my Raspberry Pi. You'll need to install the libsoundtouch python library for this to run - see the link at the start of my first post.

Edit the selector levels (click on Edit on the selector switch you've created), and enter the level names as you want them to appear in the selector dropdown (and choose selector style: select menu). You also need to add the selector actions for the levels you've chosen. The format for each action is as follows:

Code: Select all

script:///<path to saved bose.py> <n>
where <n> is a consecutive number from 0 - 9 (in my case, as I have 10 levels: 0 - 90). E.g., for level 10 in my system:

Code: Select all

script:///home/pi/devices/bose.py 1
2. Volume control
This is pretty much as I described it in my initial post. Again, copy and save this as bose_volume.py in your preferred location.


These scripts could certainly be improved (e.g., as a plugin), but as I said, this was primarily a proof of concept, and they work for me!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests