Bose Soundtouch control

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Monday 27 August 2018 12:25

Furiousz23 wrote:
Sunday 26 August 2018 21:06
When i type ./Bosevolume.py on the command line i get this error...
I assume you've got the correct values for volIdx and url in lines 10 & 11?

Try typing this in a browser:

Code: Select all

http://<domoticz url>:<domoticz port>/json.htm?type=devices&rid=<Domoticz idx of Bose volume>
(substituting your values, of course!).

Furiousz23
Posts: 28
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Monday 27 August 2018 12:37

The result of the command in my browser:

Code: Select all

{
   "ActTime" : 1535366127,
   "AstrTwilightEnd" : "22:56",
   "AstrTwilightStart" : "04:27",
   "CivTwilightEnd" : "21:18",
   "CivTwilightStart" : "06:05",
   "DayLength" : "13:59",
   "NautTwilightEnd" : "22:04",
   "NautTwilightStart" : "05:19",
   "ServerTime" : "2018-08-27 12:35:27",
   "SunAtSouth" : "13:05",
   "Sunrise" : "06:42",
   "Sunset" : "20:41",
   "app_version" : "4.9934",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "Set Level: 7 %",
         "Description" : "",
         "DimmerType" : "abs",
         "Favorite" : 0,
         "HardwareID" : 17,
         "HardwareName" : "Bose Soundtouch",
         "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
         "HardwareTypeVal" : 15,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "0001409D",
         "Image" : "Light",
         "IsSubDevice" : false,
         "LastUpdate" : "2018-08-27 12:24:58",
         "Level" : 7,
         "LevelInt" : 7,
         "MaxDimLevel" : 100,
         "Name" : "Bose Soundtouch volume",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Set Level: 7 %",
         "StrParam1" : "c2NyaXB0Oi8vL2hvbWUvcGkvZG9tb3RpY3ovc2NyaXB0cy9weXRob24vQm9zZXZvbHVtZS5weQ==",
         "StrParam2" : "c2NyaXB0Oi8vL2hvbWUvcGkvZG9tb3RpY3ovc2NyaXB0cy9weXRob24vQm9zZXZvbHVtZS5weQ==",
         "SubType" : "Switch",
         "SwitchType" : "Dimmer",
         "SwitchTypeVal" : 7,
         "Timers" : "false",
         "Type" : "Light/Switch",
         "TypeImg" : "dimmer",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "77"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
This is my Bosevolume.py script:

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.1.5'
volIdx = '77'
url = 'http://192.168.1.36:8080/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: 10 %'
			device.set_volume(volume)
	oldStatus = status
	
	sleep(0.2)

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Monday 27 August 2018 18:20

This might be due to issues with the requests module in python. Try this version instead, using urllib:

Code: Select all

#!/usr/bin/env python

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

bose = '<bose url>'
volIdx = '<Domoticz idx for volume control>'
url = '<domoticz url>:<port>/json.htm?type=devices&rid=' + volIdx
oldStatus = ''

device = soundtouch_device(bose)
device.power_on()

def domoticzread(var):
	req = urllib2.Request(url)
	resp = urllib2.urlopen(req)
	response = resp.read()
	jsonData = json.loads(response)
	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)

Furiousz23
Posts: 28
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Tuesday 28 August 2018 0:08

I`ve got the volume slider working with the new script, but had to add the adress of the Bose to the Domoticz whitelist. But unfortunatly the selector switch stopped working now....Domoticz doesn`t give any errors.

Running ./Bose.py on the command line:

Code: Select all

pi@raspberrypi:~/domoticz/scripts/python $ ./Bose.py
Traceback (most recent call last):
  File "./Bose.py", line 17, in <module>
    arg = int(sys.argv[1])
IndexError: list index out of range
However, on the command line the power.on, off and select.source commands are working fine and the Bose soundbar responds immediately

Code: Select all

pi@raspberrypi:~/domoticz/scripts/python $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from libsoundtouch import soundtouch_device
>>> device = soundtouch_device('192.168.1.5')
>>> device.power_off()
>>> device.power_on()

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Tuesday 28 August 2018 8:41

If you run Bose.py from the command line, you need to give it an argument (0 - 9), e.g.,

Code: Select all

./Bose.py 1
Don’t know why selector not working though.

Furiousz23
Posts: 28
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Wednesday 29 August 2018 20:29

Don`t ask how.....but it was working for a few seconds

Deleted some lines from the script (The select URL part), saved the script. Chmod 0777 and Chmod +x the file....and it was working :o.
But it doesn`t anymore, this thing i driving me nuts :shock: . The strage thing is, after a reboot of the Pi the selector works again, but only one time

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Thursday 30 August 2018 9:51

I’m out of ideas - sorry! :(

Furiousz23
Posts: 28
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Thursday 30 August 2018 16:44

No problem, thanx for the help!!

User avatar
najtram
Posts: 42
Joined: Wednesday 26 April 2017 11:50
Target OS: Raspberry Pi
Domoticz version: v4.9700
Location: Netherlands
Contact:

Re: Bose Soundtouch control

Post by najtram » Sunday 02 September 2018 23:09

Tried multiple ways.

- selector works from terminal.
- selector gives error in log from domoticz.

From this post (http://www.domoticz.com/forum/viewtopic.php?t=16206) it seams to be the issue that it is an python2 script instead of python3.

Hopefully this helps to find solutions.
Domoticz on Raspberry Pi 3 with USB RFLink Gateway at 433 Mhz for several KAKU switches. Bluetooth Soma Smart Shades. USB Opentherm Gateway connected to Remeha iSense with Remeha Tzerra boiler. ITHO ecofan RFT (ESP8266+CC1101). Almost running homebridge.

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Monday 03 September 2018 0:25

Yes, it's a python 2 script, and it works OK for me.

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Monday 03 September 2018 10:54

@Furiousz23,

Out of interest, what happens if you use an on / off switch, and assign two of the selector switch actions to the switch on / off actions? e.g.,

Code: Select all

On: script:///<path to python script>.Bose.py 2
Also, have you checked that the script has the same owner as Domoticz (e.g., pi:pi)?

Furiousz23
Posts: 28
Joined: Saturday 30 December 2017 21:01
Target OS: Raspberry Pi
Domoticz version: 4.10199
Location: Nederland
Contact:

Re: Bose Soundtouch control

Post by Furiousz23 » Monday 03 September 2018 14:10

Thing is drving me crazy :twisted: . I got it working for a few seconds.

Deleted the old selector script and created 5 new scripts. Each script with only one action

Power off:

Code: Select all

#!/usr/bin/env python

from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
device = soundtouch_device('192.168.1.5')
device.power_off()
AUX:

Code: Select all

#!/usr/bin/env python

from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
device = soundtouch_device('192.168.1.5')
device.select_source_aux()
Preset:

Code: Select all

#!/usr/bin/env python

from libsoundtouch import soundtouch_device
from libsoundtouch.utils import Source, Type
device = soundtouch_device('192.168.1.5')
presets = device.presets()
device.select_preset(presets[0])
After that i did some browsing and after 5 minutes i returened to Domoticz and the selector stopped working but without any errors.....

User avatar
najtram
Posts: 42
Joined: Wednesday 26 April 2017 11:50
Target OS: Raspberry Pi
Domoticz version: v4.9700
Location: Netherlands
Contact:

Re: Bose Soundtouch control

Post by najtram » Monday 17 September 2018 17:06

Ok, this took me a lot of time in the meanwhile. I'm so happy if it's going to work. I got a almost running situation, but just not that last step.

What worked was entering presets from command line terminal. Works fine! What doesn't work is entering this script in a switch in domoticz. Somehow I understood, while browsing the forum, that Domoticz is using Python3 instead of Python2. (not sure if this is correct but I needed to continue testing)
So here is what I did:

- Python2 bose.py 2 = WORKING from commandline, NOT in domoticz.
- Python3 bose.py 3 = NOT working from commandline
- Translated script to Python3 script with http://www.pythonconverter.com/
- Python3 bose.py 3 = NOT working from commandline gives error for soundtouch libs.
- Installed pip3
- Loaded soundtouch libs in pip3
- Python3 bose.py 3 = WORKING from commandline
- So with this working Python3 situation, I returned to Domoticz, almost sure that this was the solution.
- BUT back in Domoticz NOT working, giving 256 error in the log.

So for Python2 AND for Python3 it works from my commandline, but not from Domoticz

Used notation in Domoticz:
script:///home/pi/domoticz/scripts/python/bose.py 1
and
script://python/bose.py 6

Script is owned by pi and chmod with +x

Result in log:
Error: Error executing script command (/home/pi/domoticz/scripts/python/bose.py). returned: 256

Thanks for thinking along! Hope someone has a solution! :shock:
Last edited by najtram on Tuesday 18 September 2018 9:29, edited 1 time in total.
Domoticz on Raspberry Pi 3 with USB RFLink Gateway at 433 Mhz for several KAKU switches. Bluetooth Soma Smart Shades. USB Opentherm Gateway connected to Remeha iSense with Remeha Tzerra boiler. ITHO ecofan RFT (ESP8266+CC1101). Almost running homebridge.

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Monday 17 September 2018 20:35

I’m sorry this seems to be causing so much trouble! :cry:

I’m on v3.8153, so don’t know if this makes a difference. Also, I’m definitely running this as a Python 2 script.

Have you tried my earlier suggestion of running this as a simple on / off switch, to see if selector is the problem? (set up on & off actions).

I’m away at the moment, so can’t test this further.

User avatar
najtram
Posts: 42
Joined: Wednesday 26 April 2017 11:50
Target OS: Raspberry Pi
Domoticz version: v4.9700
Location: Netherlands
Contact:

Re: Bose Soundtouch control

Post by najtram » Tuesday 18 September 2018 19:40

You don't have to apology! I'm glad you take the time to think along, and thankfull I've gotten this far already!

I've tried in a on/off switch. Same result/error.
Domoticz on Raspberry Pi 3 with USB RFLink Gateway at 433 Mhz for several KAKU switches. Bluetooth Soma Smart Shades. USB Opentherm Gateway connected to Remeha iSense with Remeha Tzerra boiler. ITHO ecofan RFT (ESP8266+CC1101). Almost running homebridge.

User avatar
najtram
Posts: 42
Joined: Wednesday 26 April 2017 11:50
Target OS: Raspberry Pi
Domoticz version: v4.9700
Location: Netherlands
Contact:

Re: Bose Soundtouch control

Post by najtram » Tuesday 18 September 2018 20:13

I've got it working!!! :lol: :D

I'll post solution later
Domoticz on Raspberry Pi 3 with USB RFLink Gateway at 433 Mhz for several KAKU switches. Bluetooth Soma Smart Shades. USB Opentherm Gateway connected to Remeha iSense with Remeha Tzerra boiler. ITHO ecofan RFT (ESP8266+CC1101). Almost running homebridge.

User avatar
najtram
Posts: 42
Joined: Wednesday 26 April 2017 11:50
Target OS: Raspberry Pi
Domoticz version: v4.9700
Location: Netherlands
Contact:

Re: Bose Soundtouch control

Post by najtram » Tuesday 18 September 2018 20:20

I did multiple things so i'm not sure what solved it exactly but here's what I did:

I found a similair issue on this site: https://easydomoticz.com/forum/viewtopi ... 2&start=20

There they did a pip reinstall or something like that, so I did:
sudo -H pip install libsoundtouch
sudo -H pip3 install libsoundtouch

I added this on top of the python bose.py:

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-
Created a bose_sh.sh with the following content:

Code: Select all

!/bin/bash

python3 /home/pi/domoticz/scripts/python/bose.py $1
And put this in domoticz switch:

Code: Select all

script:///bin/bash /home/pi/domoticz/scripts/python/bose_sh.sh 4
Domoticz on Raspberry Pi 3 with USB RFLink Gateway at 433 Mhz for several KAKU switches. Bluetooth Soma Smart Shades. USB Opentherm Gateway connected to Remeha iSense with Remeha Tzerra boiler. ITHO ecofan RFT (ESP8266+CC1101). Almost running homebridge.

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » Friday 05 October 2018 16:05

I've just got round to upgrading to v4.9700 of Domoticz, and have hit the same problem - I seem to recall that recent versions of Domoticz only like python3.x, whereas my script was running happily under python2.7.

I can confirm that the bash solution described by @najtram works for me too - thanks! :D :D :D

MikeF
Posts: 228
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

Re: Bose Soundtouch control

Post by MikeF » 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.
Last edited by MikeF on Monday 12 November 2018 12:08, edited 1 time in total.

hubd
Posts: 9
Joined: Friday 25 September 2015 14:58
Target OS: Raspberry Pi
Domoticz version: latest
Contact:

Re: Bose Soundtouch control

Post by hubd » Sunday 11 November 2018 22: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

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests