iTunes Switches for AirPlay or Playlists

There’s no good reason as the iTunes API supports it. You can vote on my feature request here if you like:

For sure, Voted!

BTW I tried the switch but it didn’t update status when I toggled on iTunes or remote. Apparently the standard switch component only updates when used by hass. So I don’t think the value template is doing much here as even without it, it would be in sync after 1 use anyway.

I’m trying to see if we can move over to a RESTful switch which apparently does poll the state with the value template. But I can’t even get it to appear in my switches or logs for some reason. Will have to mess with it some more

Alright, so you inspired me to finally get around to making a shuffle that properly reports the state of the shuffle on the media player even if it gets changed from outside of hass and turns it on/off when toggled. If you change the state from outside hass via iTunes or Remote, 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 and which make the service calls when the input boolean is toggled:

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

trying to get the server / api working and getting an ‘Internal Server Error’ on any test ‘curl’

MacOS 10.15.7

Error: Command failed: osascript -l JavaScript -e 'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function getPlaylistsFromItunes(){  itunes = Application('\''iTunes'\'');  playlists = itunes.playlists();  playlistNames = [];  for (var i = 0; i < playlists.length; i++) {    playlist = playlists[i];    data = {};    data['\''id'\''] = playlist.id();    data['\''name'\''] = playlist.name();    data['\''loved'\''] = playlist.loved();    data['\''duration_in_seconds'\''] = playlist.duration();    data['\''time'\''] = playlist.time();    playlistNames.push(data);  }  return playlistNames;})());'
execution error: Error: Error: Application can't be found. (-2700)

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:315:20)
    at maybeClose (internal/child_process.js:1048:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: `osascript -l JavaScript -e 'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function getPlaylistsFromItunes(){  itunes = Application('\\''iTunes'\\'');  playlists = itunes.playlists();  playlistNames = [];  for (var i = 0; i < playlists.length; i++) {    playlist = playlists[i];    data = {};    data['\\''id'\\''] = playlist.id();    data['\\''name'\\''] = playlist.name();    data['\\''loved'\\''] = playlist.loved();    data['\\''duration_in_seconds'\\''] = playlist.duration();    data['\\''time'\\''] = playlist.time();    playlistNames.push(data);  }  return playlistNames;})());'`
}
'[2020-11-27T21:32:27.081Z] - ::ffff:10.10.100.60 - GET /playlists 500 77.727 ms - 21b'

Anythonghts ? I just having running with the script/server , tried it with SUDO also same results.

Might have found my answer…

is this true?

The API will only work with iTunes 12.x or lower.

if it is is there a solution for higher releases with Music not iTunes?

Seems to be TRUE. I found a way to INSTALL iTunes on Catalina.

worked on my Mac mini and now the API is responding to CURL

It is possible to use itunes-api with Catalina’s music app according to this thread, but I would recommend you read my guide on forked-daapd. I switched from iTunes to forked-daapd and will never go back. Read my guide and see why