Lua Example for chrome audio cast integration

Post Reply
zicht
Posts: 202
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 3.8xxx
Location: NL
Contact:

Lua Example for chrome audio cast integration

Post by zicht » Wednesday 25 April 2018 19:36

Hi,

I have spend some time in the chrome audio & LMS api to integrate it into domoticz.
The goal is to implement some kind of control, as the current integration has no remote control buttons.
The below is quick and dirty solution. Maybe it can inspire others.

i was inspired by :
https://www.domoticz.com/wiki/Logitech_Media_Server

Warning : the below is not a plug and play script, some lua skills needed.

I use chrome cast audio with EDIFIER computer speakers, sound quality is depending on what you invest.
Quite a cheap sonos alternative.

Prequisitions :
- One or more chrome audio cast device
- LMS (logitec media server - old Squeezebox server running. --> ad as hardware in domotic this LMS server
- Chromecast plugin activated in config of LMS/Plugins
- Dummy Switchs named Volume to be able to adjust volume
- Dummy Switchs named Voorkeuzezender to be able switch radio station (=favorite)-

for basic radio player and local music playing : you do not need to have an squeezebox account !
LMS is free for local private use.

Actual script i use :

Code: Select all

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
-- LMS
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
-- mac adresses listed in LMS !
-- Single speaker
cmmnd1="cc:cc:09:f5:xx:xx" -- slaapk achter
cmmnd2="cc:cc:09:f5:xx:xx" -- zolder
cmmnd3="cc:cc:09:f5:xx:xx" -- werkk
cmmnd4="cc:cc:09:f5:xx:xx" -- woonk
cmmnd5="cc:cc:09:f5:xx:xx" -- extra
--Groups of speakers
cmmnd6="cc:cc:3c:24:xx:xx" -- thuisgroep
cmmnd7="cc:cc:0d:b1:xx:xx" -- beneden
cmmnd8="cc:cc:be:a9:xx:xx" -- boven
---
-- Check if the status is stopped, if stopped it will be ignored in the rest of the script
if otherdevices['Woonkamerr']~="Stopped" then wnk=true else wnk = false end
if otherdevices['Werkkamer']~="Stopped" then wrk=true else wrk=false end
if otherdevices['Slaapk 1e achter']~="Stopped" then Sl=true else Sl=false end
if otherdevices['Slaapk Zolder']~="Stopped" then SlZ=true else Slz=false end
if otherdevices['Beneden']~="Stopped" then Ben=true else Ben=false end
if otherdevices['Boven']~="Stopped" then Bov=true else Bov=false end
if otherdevices['Thuisgroep']~="Stopped" then Th=true else Th=false end
if otherdevices['ChromecastAudio1201']~="Stopped" then Xtr=true else Xtr=false end

----
-- volume adjustment done
if devicechanged['Volume']=="Off" then
	  commnd="http://192.168.XXX.XXX:90XX/status.suffix?p0=pause"          --- CHANGE IP AND PORT OF LMS SERVER
	  print("Radio : "..commnd)
    if wnk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd4 } end
	if wrk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd3 } end
	if Sl then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd1 } end
	if SlZ then ccommandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd2 } end
	if Ben then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd7 } end
	if Bov then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd8 } end
	if Th then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd6 } end
	if Xtr then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd5 } end
	commandArray["Voorkeurzender"]="Off"
elseif devicechanged['Volume'] then
	  vol=otherdevices['Volume']
	  x=string.len(vol)-2   y=x-2  v=string.sub(vol,y,x) 
	  nData=v nData=nData:gsub('%W',' ') nData=nData:match("^%s*(.-)%s*$") nData=nData:gsub("%s+", " ") v=nData
	  commnd="http://192.168.XXX.XXX:90XX/status.suffix?p0=mixer&p1=volume&p2="..v		   --- CHANGE IP AND PORT OF LMS SERVER
	  print("Radio : "..commnd)
		
    if wnk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd4 } end
	if wrk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd3 } end
	if Sl then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd1 } end
	if SlZ then ccommandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd2 } end
	if Ben then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd7 } end
	if Bov then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd8 } end
	if Th then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd6 } end
	if Xtr then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd5 } end
end

-- Changed radio station
if devicechanged['Voorkeurzender']=="Off" then
	  commnd="http://192.168.XXX.XXX:90XX/status.suffix?p0=stop"                                                           --- CHANGE IP AND PORT FOR LMS
 	  print("Radio : "..commnd)
    if wnk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd4 } end
	if wrk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd3 } end
	if Sl then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd1 } end
	if SlZ then ccommandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd2 } end
	if Ben then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd7 } end
	if Bov then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd8 } end
	if Th then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd6 } end
	if Xtr then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd5 } end
end

if devicechanged['Voorkeurzender']~="Off" then
      zend=otherdevices['Voorkeurzender']
	  x=string.len(zend) 
	  vl=string.sub(zend,0,x) v2=string.gsub(zend,"F","")
      commnd="http://192.168.xxx.xxx:90xx/plugins/Favorites/index.html?index="..v2          --- CHANGE IP AND PORT

	if v2=="0" then v="http://opml.radiotime.com/Tune.ashx?id=s252020&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371" end
	if v2=="1" then v="http://opml.radiotime.com/Tune.ashx?id=s6712&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371" end
	if v2=="2" then v="http://opml.radiotime.com/Tune.ashx?id=s75166&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371"  end
	if v2=="3" then v="http://opml.radiotime.com/Tune.ashx?id=s253879&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371" end
	if v2=="4" then v="http://opml.radiotime.com/Tune.ashx?id=s296300&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371" end
	if v2=="5" then v="http://opml.radiotime.com/Tune.ashx?id=s67814&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=fa1099791b4280d537f7566de8df7371" end
	if v2=="6" then v="http://opml.radiotime.com/Tune.ashx?id=s87683&formats=aac,ogg,mp3,wmpro,wma,wmvoice&partnerId=16&serial=6d811c25445615ce59f4aaa4d2aa90f1" end
	if v~= nil then commnd="http://192.168.XXX.XXX:90XX/status.suffix?p0=playlist&p1=play&p2="..v end    -- CHANGE IP AND PORT FOR LMS
	if v== nil then commnd="http://192.168.XXX.XXX:90XX/status.suffix?p0=stop" end				    -- CHANGE IP AND PORT FOR LMS
	print("Radio : "..commnd)

    if wnk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd4 } end
	if wrk then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd3 } end
	if Sl then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd1 } end
	if SlZ then ccommandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd2 } end
	if Ben then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd7 } end
	if Bov then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd8 } end
	if Th then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd6 } end
	if Xtr then commandArray[#commandArray + 1]= {['OpenURL'] = commnd.."&player="..cmmnd5 } end
end
i believe it is quite simple to understand.
If you initiate a dummy switch domoticz will interact with LMS server to initiate the action (done by LMS)
It works also on a group of speakers in the chrome home app.

Question : It wil only work on players with another status then stopped, so how to start ?
Answer : put a player on and off : the status will change from stopped to on--> initiate the favorite, it will be set to the player with on status.

https://en.wikipedia.org/wiki/Logitech_Media_Server
http://tutoriels.domotique-store.fr/con ... -http.html
https://www.mysqueezebox.com/download

(ps. I have limted time for support, sorry for that... )
Rpi & Win x64. Using : cam's,Nest,RFXCom,Kaku,LaCrosse,RFY,IsAlive,Dummy,Wether Underground, standard Lua, Curl on windows,Domoticz Android App & Tasker,

"The only source of knowledge is experience (A.Einstein)"

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests