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

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