Command_line Switch To Control Connect/Disconnect Speakers, set volume and Play/Spotify

Firstly, apologies, this is my first post, I don’t know any of the formatting script yet and also this is quite a long post.
I have read a lot of posts and recognise that a common issue is lack of background info, so I have tried to be as thorough with my explanation as I can, based on my knowledge of HA and coding.

Let me begin.
I currently have HA and HA Bridge running on my Pi.
I also have Airfoil (airplay multi streaming software) and its API installed on my iMac.

In HA Bridge I have devices setup which allow me to connect/disconnect airplay speakers via the Airfoil API:

Using http POST:
http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/connect
http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/disconnect

Set their volume:

Using http POST:
http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/volume
Body Args:
${intensity.percent}

Play/Pause Spotify:

Using http GET:
http ://192.168.1.2:8080/spotify/play

Then I can also group http calls to connect/disconnect multiple speakers, which is great and Alexa finds these devices as emulated hue bulbs.

This works great, but I would prefer to be able to control these speakers, groups, volume and Spotify from Home Assistant.

If I set the HA Bridge port to 80, then HA finds the HA Bridge devices.
The problem with this is that HA then shares the devices again and then Alexa finds duplicated of each device :frowning:

So, I have added the customize element “emulated_hue_hidden: true” for each speaker (emulated light hue device) in HA customize.yaml, this prevents Alexa from finding multiple instances of the same named device on my network - great!

So you may be asking, why do I want to deviate away from this setup, if it works… well, ultimately, it would be nice to have all the devices setup and fully controllable in HA, rather than integrate the HA Bridge.
Its also fun to learn a bit more about HA’s capabilities.

So I then came across command_line switch component, which I have configured to connect/disconnect a speaker, using the following:

  • platform: command_line
    switches:
    lounge_speaker_switch:
    command_on: “/usr/bin/curl -X POST http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/connect”
    command_off: “/usr/bin/curl -X POST http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/disconnect”

What i’m missing is the method of retrieving the status to set the command_status of the switch
After calling the connector or disconnected command it posts the following args in the body:

{“id”:“6CADF8024C05@Kitchen-2-Cone”,“connected”:"true}
{“id”:“6CADF8024C05@Kitchen-2-Cone”,“connected”:“false”}

Can I use this args to set the status? If so, how?
Alternately, but calling the following:

[GET] http ://192.168.1.2:8080/speakers/

I get the status and stats of all my speakers:

[{“connected”:“false”,“volume”:0.855453312397,“name”:“Computer”,“id”:“com.rogueamoeba.airfoil.LocalSpeaker”},{“connected”:“false”,“volume”:0.75,“name”:“Full House”,“id”:“com.rogueamoeba.group.3566A3CD-78D9-43C2-856C-BD435554E33F”},{“connected”:“false”,“volume”:0.75,“name”:“Downstairs”,“id”:“com.rogueamoeba.group.158CC8AE-E802-4A68-9133-5B8C7C648628”},{“connected”:“false”,“volume”:0.75,“name”:“Kitchen”,“id”:“com.rogueamoeba.group.767E1254-03DF-47E9-AC05-78C1C4CD35C6”},{“connected”:“false”,“volume”:0.783333301544,“name”:“Master-Bedroom-Cone”,“id”:“6CADF8EF26CD@Master-Bedroom-Cone”},{“connected”:“false”,“volume”:0.814705908298,“name”:“Dining-Cone”,“id”:“6CADF80339D7@Dining-Cone”},{“connected”:“false”,“volume”:0.832352936268,“name”:“Lounge Airport”,“id”:“581FAAE5B24C@Lounge Airport”},{“connected”:“false”,“volume”:0.823529422283,“name”:“Kitchen-Cone”,“id”:“6CADF8034145@Kitchen-Cone”},{“connected”:“false”,“volume”:0.823529422283,“name”:“Lounge-Cone”,“id”:“6CADF8026F7B@Lounge-Cone”},{“connected”:“false”,“volume”:0.814705908298,“name”:“Kitchen-2-Cone”,“id”:“6CADF8024C05@Kitchen-2-Cone”}

Can these args then be used to set the status by pin pointing the connected value for a particular id?

command_state: “/usr/bin/curl -X GET http ://192.168.1.2:8080/speakers”

Further to this request, I would also like to retrieve, use and set the volume of each speaker.
As mentioned earlier, I can set the volume of a speaker using:

POST:
http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/volume
Body Args:
{percent}

So to do this I would need a slider component.
Now, the beauty of the HA Bridge emulated device, when found by HA, is that it thinks it’s a light and so expands and presents a brightness slider, so I can set the volume.
However, as far as I know, the command_line switch component cannot have a “brightness” slider, so if I wanted to associate the volume with the command_line switch, then this would have to be a separate component, which is then positioned/grouped with the speaker’s command_line switch.

Is it possible to create a command_line light component, with on/off/brightness controls?

Following any form of resolution I would then like to add control for the Spotify component, so that I can turn on a speaker/group of speakers, set the source of the Spotify component (to my computer, as I use the same Spotify account on other devices) and then set it to play.

Simples?!

You can install jq on most Linux platforms to easily pull the return value. For example, in your command_on, you could try:

command_on: "/usr/bin/curl -X POST http ://192.168.1.2:8080/speakers/6CADF8026F7B@Lounge-Cone/connect | jq '.connected' "

Which would return:

"true" or "false" #based of course on what is sent back in the JSON body

Thanks fanuch,

With your suggestion of jq and a bit of Googling I was able to construct this:

command_state: “/usr/bin/curl -X GET http://192.168.1.2:8080/speakers | jq --raw-output ‘. | select(.id==“6CADF8026F7B@Lounge-Cone”) | .connected’”

Which returns true or false depending on whether it is connected or not - but only in putty ssh.

The issue is with the double quotes that are required around the speaker name “6CADF8026F7B@Lounge-Cone”, but this is comflicting with the double quotes around the whole expression.

I have tried "6CADF8026F7B@Lounge-Cone", but this doesn’t work.
What is the correct way of structuring this expression?