Bose Soundtouch control

Moderator: leecollings

MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF »

This looks as if something is broken in your version of libsoundtouch. Try running 'sudo apt-get update', and then remove / install libsoundtouch, to see if this helps.
jurgen
Posts: 30
Joined: Friday 02 October 2015 19:09
Target OS: NAS (Synology & others)
Domoticz version: stable
Contact:

Re: Bose Soundtouch control

Post by jurgen »

Hi all,

Last week i tried to install libsoundtouch on synology and tried to controle my bose soundtouch with domoticz. But i had some problems with python version 2 and 3 and i was not able to get everything working. After some troubleshooting with python3 and install libsoundtouch with pip for python 3 it was working for me, so i would like to share it with other people.

In my case:

DSM 6.2.1-23824 Update 1
Python 3.5.1

steps i did to install everything:

ssh to synology

Check which version is installed (when python 3 is not installed, install it with the package manager in synology webinterface)

sudo -i
ls /volume1/@appstore/py3k/usr/local/bin
python3 python3.5 python3.5m

Install PIP for python3 to install libsoundtouch
cd /volume1/@appstore/py3k/usr/local/bin#
./python3 -m ensurepip

Ignoring indexes: https://pypi.python.org/simple
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-7.1.2 setuptools-18.2


Install libsoundtouch with PIP
root@DS411:/volume1/@appstore/py3k/usr/local/bin# ./python3 -m pip install libsoundtouch
Collecting libsoundtouch
Downloading https://files.pythonhosted.org/packages ... ne-any.whl
Collecting websocket-client>=0.40.0 (from libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (200kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 200kB 534kB/s
Collecting zeroconf>=0.19.1 (from libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl
Collecting enum-compat>=0.0.2 (from libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... 0.2.tar.gz
Collecting requests<3,>=2 (from libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (57kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 61kB 1.2MB/s
Collecting six (from websocket-client>=0.40.0->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl
Collecting ifaddr (from zeroconf>=0.19.1->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... -0.1.4.zip
Collecting certifi>=2017.4.17 (from requests<3,>=2->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (146kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 147kB 653kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests<3,>=2->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (133kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 135kB 516kB/s
Collecting urllib3<1.25,>=1.21.1 (from requests<3,>=2->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (118kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 118kB 760kB/s
Collecting idna<2.8,>=2.5 (from requests<3,>=2->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl (58kB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 61kB 1.2MB/s
Collecting ipaddress (from ifaddr->zeroconf>=0.19.1->libsoundtouch)
Downloading https://files.pythonhosted.org/packages ... ne-any.whl
Installing collected packages: six, websocket-client, ipaddress, ifaddr, zeroconf, enum-compat, certifi, chardet, urllib3, idna, requests, libsoundtouch
Running setup.py install for ifaddr
Running setup.py install for enum-compat
Successfully installed certifi-2018.10.15 chardet-3.0.4 enum-compat-0.0.2 idna-2.7 ifaddr-0.1.4 ipaddress-1.0.22 libsoundtouch-0.8.0 requests-2.20.1 six-1.11.0 urllib3-1.24.1 websocket-client-0.54.0 zeroconf-0.21.3
You are using pip version 7.1.2, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

When everything is installed, try with python3 if you are able to control bose soundtouch using the command line.
Use the following commands but edit the bose IP address to your speaker.

root@DS411:/python3

>>> from libsoundtouch import soundtouch_device
>>> from libsoundtouch.utils import Source, Type
>>> from time import sleep
>>> bose = '192.168.x.x'
>>> device = soundtouch_device(bose)
>>> device.power_on()
>>> print(device.config.name)
Soundtouch 20
>>> device.play()
>>> quit()

If you were able to control your speaker then proceed with the next steps.

mkdir /volume1/@appstore/domoticz/var/scripts/bose
cd /volume1/@appstore/domoticz/var/scripts/bose
nano presets.py

Code: Select all

#!/usr/bin/env python3

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

bose = '192.168.x.x'

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

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

if arg == 0: # power off
        device.power_off()

if 1 <= arg <= 6: # presets 1-6
        device.select_preset(presets[arg-1])
save and exit

change permissions to execute
chmod a+x presets.py

try if this script is working, this will play preset 1 or button 1 on the speaker:
python3 presets.py 1

if the script is working that we can proceed to configure domoticz webinterface to interact with the python script

In domoticz go to settings ->hardware
add Dummy hardware
create virtual swith (selector)
go to devices and make sure that the device is added to switches
edit the switch
Make sure you add something like this (depents on your situation)
bose knoppen.JPG
bose knoppen.JPG (200.9 KiB) Viewed 2832 times
If add something like this then you are able to select the presets like this.
eindresultaat.JPG
eindresultaat.JPG (46.34 KiB) Viewed 2832 times

If you would like to controle the volume of the speaker with domoticz proceed with the next steps.

cd /volume1/@appstore/domoticz/var/scripts/bose
nano volume.py

Add the following script:
bose IP
Dimmer IDX to control the volume
NAS IP for the url to interact with domoticz

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 = '192.168.x.x'
volIdx = 'Dimmer IDX like 103'
url = 'http://192.168.x.x:8084/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)
Change the permissions
chmod a+x volume.py

Run the script in background.
python3 /usr/local/domoticz/var/scripts/bose/volume.py &

go to domoticz:
settings ->hardware add virtual switch under the dummy switch which created earlier.
Make sure that the switch is added and change the type to dimmer
dimmer.JPG
dimmer.JPG (191.89 KiB) Viewed 2832 times
When everything is working fine then you need to make sure that the volume.py is started when the NAS is rebooted or booted. Create a startup script

cd /usr/local/etc/rc.d
nano bose-volume.sh
python3 /usr/local/domoticz/var/scripts/bose/volume.py &

chmod a+x bose-volume.sh

Done!

@MikeF credits for you for the initial scripts in your first post.
ghulleman
Posts: 9
Joined: Sunday 16 June 2019 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands / Hilversum
Contact:

Re: Bose Soundtouch control

Post by ghulleman »

hubd wrote: Sunday 11 November 2018 21:25 Hi,

I tried to connect to Soundtouch. I get this error:

File "bose.py", line 7, in <module>
from libsoundtouch import soundtouch_device
File "/usr/local/lib/python2.7/dist-packages/libsoundtouch/__init__.py", line 9, in <module>
from zeroconf import Zeroconf, ServiceBrowser
File "/usr/local/lib/python2.7/dist-packages/zeroconf.py", line 175
def current_time_millis() -> float:
^
SyntaxError: invalid syntax

My Python-version is:
Python 2.7.9
My Python 3-version is:
Python 3.4.2

Latest libsoundtouch, installed with pip

I am on a Raspberry PI 3 running Linux version 4.9.41-v7+ (dc4@dc4-XPS13-9333)
(gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611) )
#1023 SMP Tue Aug 8 16:00:15 BST 2017

Hope you can help

Hubd
For those who got this error: check the python version (terminal, python -V). If version 2.7.x, zeroconf might be a high version (terminal, pip show zeroconf). Version 0.20 and higher does not support python 2.7. If you want to use python 2.7, uninstall zerconf (pip uninstall zeroconf) and install version 0.19.1 (pip install zeroconf==0.19.1)

Source: https://openplc.discussion.community/po ... x-10110417
PhJack
Posts: 4
Joined: Thursday 26 July 2018 22:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Bose Soundtouch control

Post by PhJack »

Furiousz23 wrote: Friday 24 August 2018 22:43
PhJack wrote: Wednesday 22 August 2018 22:47 Hi Furiousz23,
can you tell me please what is the current firmware of your Bose soundtouch ? Is the cal of the function "device.select_source_aux()" working for you ?

Hi

Unfortunately the selector switch didn't work after all, only the volume slider worked. In the Domoticz log there where no errors, but the sound touch didn't respond to the Domoticz selector commands. At the moment I just made a fresh install on my Raspberry, so I will try to get the Bose script working again.
I read the documentation of the Bose API and I found a different way to activate AUX as libsoundtouch was no more able to do it (probably because of a new firmware)
You juste need a shell command with the right IP of your BOSE:

Code: Select all

#!/bin/bash
curl -s -o /dev/null --request POST --header "Content-Type: application/xml" --data '<ContentItem source="PRODUCT" sourceAccount="TV"></ContentItem>' http://IP OF THE BOSE:8090/select
It is because the aux is no more named AUX but TV instead. I think libsoundtouch is out of date on this point.
PhJack
Posts: 4
Joined: Thursday 26 July 2018 22:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Bose Soundtouch control

Post by PhJack »

MikeF wrote: Friday 05 October 2018 23:51 I've come up with an alternative solution for controlling Bose volume: instead of using a python script which runs continuously:

Code: Select all

while 1:
	...
	sleep(0.2)
and reads the json from the device to check for a change of volume, I've created a simple lua script which passes the volume value to an equally simple python script.

Lua script:

Code: Select all

-- Set Bose volume

-- change next line to match name of your Bose volume device in Domoticz
local bose = 'Bose volume'

-- change next line to match location of your python script; keep space at end
local py_script = 'python3 /home/pi/devices/bose_vol.py '

commandArray = {}

if devicechanged[bose] then
	boseVolume = otherdevices_svalues[bose]
	cmd = py_script .. boseVolume
	os.execute(cmd)
end

return commandArray
Python script:

Code: Select all

#!/usr/bin/env python3

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

volume = int(sys.argv[1])

# change next line to match url of your Bose
bose = '192.168.0.69'

device = soundtouch_device(bose)
device.power_on()

device.set_volume(volume)
I believe this is a more responsive approach, and better integrated with Domoticz.

Edit: I left out the second import from libsoundtouch in the last script - now added.
A way better solution than the python script in background ! Bravo and thank you :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest