Itunes component: add shuffle support (which is supported by underlying server layer)

Yes, obviously iTunes needs to be running… Try to start it up and then see if you can see the itunes-api server.

Ok, now, every time I start the mac it starts up itunes and itunes server.
This is my config:

  • platform: itunes
    name: iTunes
    host: http://192.168.178.75
    #… if i write only host: 192.168.178.75 she don’t work
    port: 8181

the tunes, however, remains always offline. I do not see the play button

I have to add some other rtiga to see my playlists?

If it’s working, you can go to http://192.168.178.75:8181/playlists and it will show you a list of all your playlists in JSON format. Then you know itunes-api is working and you have a problem on the homeassistant config side.

My config is here if you want to see how I did in on hass side.:

I wouldn’t try doing playlists first from hass, they are not easy. Just see if you can get play/next, etc working, then we will work on playlist buttons (I have them in my config).

Try doing just this, it’s all I needed to get 8 airplay speakers and my iTunes library to show up with all features:

media_player:

  - platform: itunes
    host: 192.168.178.75

Yeah that doesn’t look good. Probably failed to connect to iTunes… You will want to make sure it starts after iTunes starts… It’s good to start it with script/server so you can see the log output and what happens immediately. Once you have it working with script/server, then you can try to have it autostart.

Forget about forever for now, I wasn’t able to get it to work… Just run script/server and see what happens. Make sure iTunes is running.

Ok. Tell me at least which version operating system you are on

I’m on 10.11 (El Capitan). I hope it didn’t break in later versions as I’m about to upgrade.

I solved.
I had so many problems because I was trying an installation on lion, now I’ve updated to High sierra it’s all working out great. However it was not so simple. I will explain my procedure hoping that it will serve someone.
First you need to install node for osx
after, execute the command in the terminal:
sudo / usr / local / bin / npm install forever -g

then copy the itunes api master folder into documents and from the terminal:
cd Documents / itunes-api-master
./script/server
./script/install

1 Like

Wow I didn’t expect anyone would be on an older version than me ;-). Glad you got it going.

Now that I’ve configured itunes, could you explain how to create a playlist?

Well, you create the playlists in iTunes like you normally would. To access those playlists from HomeAssistant, I have configured mine with some buttons that you can press to choose the playlist you want in the lovelace UI.

You can read about it here:

I recommend you do it the way I showed my second post from that link rather than the first.

I think there also might way to do it by embedding an input select into the mini-media-player card but I haven’t gotten around to trying that method yet…

Getting proper on/off status is much harder… It’s on my list of things to do but is never high enough priority to get to. You are welcome to try. With pushbuttons like I use in my example, it’s much less noticeable, because when you press the button, the playlist starts and that’s it.

Hi, I was able to get a playlist played/stop. I also configured the shuffle.
Now I would like to understand how to get the status update? If I activate the shuffle function from itunes, I would like it to also activate in HA.
My configuration is:


customize

switch.shuffle:
icon: mdi:shuffle
assumed_state: false

switch.anastacia:
icon: mdi:library-music
assumed_state: false

:slight_smile: That’s why this thread was started… There’s no easy way that I can tell to make shuffle work properly with status,etc. They need to fix it in the component… You can go vote for the feature request:

Hey, let me ask you, do you have it starting automatically on boot with forever? I got a new machine running on 10.14, but I still get the same permission errors when running script/install as I got on my old machine and it won’t start on boot. Does this work for you?

scusa per il ritardo

Per Yosemite usa questo comando

sudo / usr / local / bin / npm installa -g per sempre

Dopo aver inserito
cd Documents / itunes-api-master

Così sarà:
cd Documents / itunes-api-master
sudo / usr / local / bin / npm installs -g per sempre
./script/bootstrap.
cd Documents / itunes-api-master
./script/install

In caso di permesso Deneid

script / server
sudo script / installazione sudo

Per verificare l’operazione
LOCALIP: 8181

yes, he starting automatically on boot with forever

Alright, so I finally got around to making a shuffle that properly reports the state of the shuffle on the media player and turns it on/off when toggled. If you change the state from outside hass, it takes 5-10 seconds to sync, that’s just how long it takes itunes-api to do it. A better way to do this might be with a RESTful switch, but I got tired of banging my head against that wall and did it this way.

Add the following to configuration.yaml:

the commands that turn shuffle off and on:

rest_command:
  shuffle_on: 
    url: "http://[YOUR_ITUNES-API_SERVER_IP]:8181/shuffle"
    method: put
    content_type: "application/x-www-form-urlencoded"
    payload: "mode=songs"
  shuffle_off: 
    url: "http://[YOUR_ITUNES-API_SERVER_IP]:8181/shuffle"
    method: put
    content_type: "application/x-www-form-urlencoded"
    payload: "mode=off"

the input boolean you will use as a switch in your ui:

input_boolean:
  itunes_shuffle:
    name: "Shuffle"
    icon: mdi:shuffle-variant

The automations which make sure it displays the correct status if someone changes it in iTunes or Remote rather than via homeassistant:

automation:

- id: turn_on_shuffle_from_input_boolean
  alias: turn_on_shuffle_from_input_boolean
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: input_boolean.itunes_shuffle
    from: 'off'
    to: 'on'
  condition: 
    condition: template
    value_template: '{{ state_attr("media_player.itunes", "shuffle") == false}}'
  action:
    service: rest_command.shuffle_on

- id: turn_off_shuffle_from_input_boolean
  alias: turn_off_shuffle_from_input_boolean
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: input_boolean.itunes_shuffle
    from: 'on'
    to: 'off'
  condition: 
    condition: template
    value_template: '{{ state_attr("media_player.itunes", "shuffle") == true}}'
  action:
    service: rest_command.shuffle_off
      
- id: sync_shuffle_input_boolean_to_external_on
  alias: sync_shuffle_input_boolean_to_external_on
  initial_state: 'true'
  trigger:
    - platform: state
      entity_id: media_player.itunes
    - platform: homeassistant
      event: start
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ state_attr("media_player.itunes", "shuffle") == true}}'
      - condition: state
        entity_id: input_boolean.itunes_shuffle
        state: 'off'
  action:
    service: input_boolean.turn_on
    data:
      entity_id: input_boolean.itunes_shuffle

- id: sync_shuffle_input_boolean_to_external_off
  alias: sync_shuffle_input_boolean_to_external_off
  initial_state: 'true'
  trigger:
    - platform: state
      entity_id: media_player.itunes
    - platform: homeassistant
      event: start
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ state_attr("media_player.itunes", "shuffle") == false}}'
      - condition: state
        entity_id: input_boolean.itunes_shuffle
        state: 'on'
  action:
    service: input_boolean.turn_off
    data:
      entity_id: input_boolean.itunes_shuffle
1 Like

Hello my dearest friend, it is very kind of you to share this news. I’m really grateful to you. I’m not at home right now and can’t try your code. When I return in October, I will finally be able to enjoy this function. Thanks again!!!

1 Like