core/homeassistant/components/media_player/itunes.py

451 lines
14 KiB
Python
Raw Normal View History

2015-09-12 03:06:03 +00:00
"""
2016-03-08 09:34:33 +00:00
Support for interfacing to iTunes API.
2015-09-12 03:06:03 +00:00
2015-10-23 16:26:36 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/media_player.itunes/
2015-09-12 03:06:03 +00:00
"""
import logging
2015-11-29 22:04:44 +00:00
import requests
import voluptuous as vol
2015-11-29 22:04:44 +00:00
2015-09-12 03:06:03 +00:00
from homeassistant.components.media_player import (
2016-02-19 05:27:50 +00:00
MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK, SUPPORT_TURN_OFF,
SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, PLATFORM_SCHEMA,
SUPPORT_PLAY, MediaPlayerDevice)
2015-09-12 03:06:03 +00:00
from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING, CONF_NAME,
CONF_HOST, CONF_PORT, CONF_SSL)
import homeassistant.helpers.config_validation as cv
2015-09-12 03:06:03 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'iTunes'
DEFAULT_PORT = 8181
DEFAULT_TIMEOUT = 10
DEFAULT_SSL = False
DOMAIN = 'itunes'
2015-09-12 03:06:03 +00:00
SUPPORT_ITUNES = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
2015-10-07 03:12:48 +00:00
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK | \
SUPPORT_PLAY_MEDIA | SUPPORT_PLAY
2015-09-12 03:06:03 +00:00
2015-09-14 21:27:00 +00:00
SUPPORT_AIRPLAY = SUPPORT_VOLUME_SET | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
})
2015-09-14 21:27:00 +00:00
2015-09-12 03:49:43 +00:00
2015-09-12 03:06:03 +00:00
class Itunes(object):
"""The iTunes API client."""
2015-09-12 03:06:03 +00:00
def __init__(self, host, port, use_ssl):
2016-03-08 09:34:33 +00:00
"""Initialize the iTunes device."""
2015-09-12 03:06:03 +00:00
self.host = host
self.port = port
self.use_ssl = use_ssl
2015-09-12 03:06:03 +00:00
@property
def _base_url(self):
2016-03-08 09:34:33 +00:00
"""Return the base url for endpoints."""
if self.use_ssl:
uri_scheme = 'https://'
else:
uri_scheme = 'http://'
2016-01-21 22:18:52 +00:00
if self.port:
return '{}{}:{}'.format(uri_scheme, self.host, self.port)
return '{}{}'.format(uri_scheme, self.host)
2015-09-12 03:06:03 +00:00
def _request(self, method, path, params=None):
"""Make the actual request and return the parsed response."""
url = '{}{}'.format(self._base_url, path)
2015-09-12 03:06:03 +00:00
try:
if method == 'GET':
response = requests.get(url, timeout=DEFAULT_TIMEOUT)
elif method == 'POST':
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
elif method == 'PUT':
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
elif method == 'DELETE':
response = requests.delete(url, timeout=DEFAULT_TIMEOUT)
2015-09-12 03:06:03 +00:00
2015-09-12 04:42:11 +00:00
return response.json()
2015-09-12 03:06:03 +00:00
except requests.exceptions.HTTPError:
return {'player_state': 'error'}
except requests.exceptions.RequestException:
return {'player_state': 'offline'}
def _command(self, named_command):
2016-03-08 09:34:33 +00:00
"""Make a request for a controlling command."""
2015-09-12 03:06:03 +00:00
return self._request('PUT', '/' + named_command)
def now_playing(self):
2016-03-08 09:34:33 +00:00
"""Return the current state."""
2015-09-12 03:06:03 +00:00
return self._request('GET', '/now_playing')
def set_volume(self, level):
2016-03-08 09:34:33 +00:00
"""Set the volume and returns the current state, level 0-100."""
2015-09-12 03:06:03 +00:00
return self._request('PUT', '/volume', {'level': level})
def set_muted(self, muted):
2016-03-08 09:34:33 +00:00
"""Mute and returns the current state, muted True or False."""
2015-09-12 03:06:03 +00:00
return self._request('PUT', '/mute', {'muted': muted})
def play(self):
2016-03-08 09:34:33 +00:00
"""Set playback to play and returns the current state."""
2015-09-12 03:06:03 +00:00
return self._command('play')
def pause(self):
2016-03-08 09:34:33 +00:00
"""Set playback to paused and returns the current state."""
2015-09-12 03:06:03 +00:00
return self._command('pause')
def next(self):
2016-03-08 09:34:33 +00:00
"""Skip to the next track and returns the current state."""
2015-09-12 03:06:03 +00:00
return self._command('next')
def previous(self):
2016-03-08 09:34:33 +00:00
"""Skip back and returns the current state."""
2015-09-12 03:06:03 +00:00
return self._command('previous')
def play_playlist(self, playlist_id_or_name):
2016-03-08 09:34:33 +00:00
"""Set a playlist to be current and returns the current state."""
response = self._request('GET', '/playlists')
playlists = response.get('playlists', [])
2015-10-07 06:02:25 +00:00
found_playlists = \
[playlist for playlist in playlists if
2015-10-07 06:13:13 +00:00
(playlist_id_or_name in [playlist["name"], playlist["id"]])]
2015-10-07 05:55:15 +00:00
if found_playlists:
playlist = found_playlists[0]
2015-10-07 05:55:15 +00:00
path = '/playlists/' + playlist['id'] + '/play'
return self._request('PUT', path)
2015-09-12 03:06:03 +00:00
def artwork_url(self):
2016-03-08 09:34:33 +00:00
"""Return a URL of the current track's album art."""
2015-09-12 03:06:03 +00:00
return self._base_url + '/artwork'
2015-09-14 21:27:00 +00:00
def airplay_devices(self):
2016-03-08 09:34:33 +00:00
"""Return a list of AirPlay devices."""
2015-09-14 21:27:00 +00:00
return self._request('GET', '/airplay_devices')
2015-09-14 21:39:43 +00:00
def airplay_device(self, device_id):
2016-03-08 09:34:33 +00:00
"""Return an AirPlay device."""
2015-09-14 21:39:43 +00:00
return self._request('GET', '/airplay_devices/' + device_id)
2015-09-14 21:27:00 +00:00
2015-09-14 21:39:43 +00:00
def toggle_airplay_device(self, device_id, toggle):
2016-03-08 09:34:33 +00:00
"""Toggle airplay device on or off, id, toggle True or False."""
2015-09-14 21:27:00 +00:00
command = 'on' if toggle else 'off'
2015-09-14 21:39:43 +00:00
path = '/airplay_devices/' + device_id + '/' + command
return self._request('PUT', path)
2015-09-14 21:27:00 +00:00
2015-09-14 21:39:43 +00:00
def set_volume_airplay_device(self, device_id, level):
2016-03-08 09:34:33 +00:00
"""Set volume, returns current state of device, id,level 0-100."""
2015-09-14 21:39:43 +00:00
path = '/airplay_devices/' + device_id + '/volume'
2015-09-14 21:27:00 +00:00
return self._request('PUT', path, {'level': level})
2015-09-12 04:49:34 +00:00
2015-09-12 03:06:03 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the iTunes platform."""
2015-09-12 03:06:03 +00:00
add_devices([
ItunesDevice(
config.get(CONF_NAME),
config.get(CONF_HOST),
config.get(CONF_PORT),
config.get(CONF_SSL),
2015-09-14 21:27:00 +00:00
add_devices
2015-09-12 03:06:03 +00:00
)
])
2015-09-12 03:49:43 +00:00
2015-09-12 03:06:03 +00:00
class ItunesDevice(MediaPlayerDevice):
2016-03-08 09:34:33 +00:00
"""Representation of an iTunes API instance."""
2015-09-12 03:06:03 +00:00
def __init__(self, name, host, port, use_ssl, add_devices):
2016-03-08 09:34:33 +00:00
"""Initialize the iTunes device."""
2015-09-12 03:06:03 +00:00
self._name = name
self._host = host
self._port = port
self._use_ssl = use_ssl
2015-09-14 21:27:00 +00:00
self._add_devices = add_devices
2015-09-12 03:06:03 +00:00
self.client = Itunes(self._host, self._port, self._use_ssl)
2015-09-12 03:06:03 +00:00
self.current_volume = None
self.muted = None
self.current_title = None
self.current_album = None
self.current_artist = None
self.current_playlist = None
self.content_id = None
self.player_state = None
2015-09-14 21:27:00 +00:00
self.airplay_devices = {}
2015-09-12 03:06:03 +00:00
self.update()
2015-09-12 04:42:11 +00:00
def update_state(self, state_hash):
2016-03-08 09:34:33 +00:00
"""Update all the state properties with the passed in dictionary."""
2015-09-12 03:06:03 +00:00
self.player_state = state_hash.get('player_state', None)
self.current_volume = state_hash.get('volume', 0)
self.muted = state_hash.get('muted', None)
self.current_title = state_hash.get('name', None)
self.current_album = state_hash.get('album', None)
self.current_artist = state_hash.get('artist', None)
self.current_playlist = state_hash.get('playlist', None)
self.content_id = state_hash.get('id', None)
@property
def name(self):
2016-03-08 09:34:33 +00:00
"""Return the name of the device."""
2015-09-12 03:06:03 +00:00
return self._name
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Return the state of the device."""
2015-09-12 03:49:43 +00:00
if self.player_state == 'offline' or self.player_state is None:
2015-09-12 03:06:03 +00:00
return 'offline'
if self.player_state == 'error':
return 'error'
if self.player_state == 'stopped':
return STATE_IDLE
if self.player_state == 'paused':
return STATE_PAUSED
return STATE_PLAYING
2015-09-12 03:06:03 +00:00
def update(self):
2016-03-08 09:34:33 +00:00
"""Retrieve latest state."""
2015-09-12 03:06:03 +00:00
now_playing = self.client.now_playing()
2015-09-12 04:42:11 +00:00
self.update_state(now_playing)
2015-09-12 03:06:03 +00:00
2015-09-14 21:27:00 +00:00
found_devices = self.client.airplay_devices()
found_devices = found_devices.get('airplay_devices', [])
new_devices = []
for device_data in found_devices:
device_id = device_data.get('id')
if self.airplay_devices.get(device_id):
# update it
airplay_device = self.airplay_devices.get(device_id)
airplay_device.update_state(device_data)
else:
# add it
airplay_device = AirPlayDevice(device_id, self.client)
airplay_device.update_state(device_data)
self.airplay_devices[device_id] = airplay_device
new_devices.append(airplay_device)
if new_devices:
self._add_devices(new_devices)
2015-09-12 03:06:03 +00:00
@property
def is_volume_muted(self):
2016-03-08 09:34:33 +00:00
"""Boolean if volume is currently muted."""
2015-09-12 03:06:03 +00:00
return self.muted
@property
def volume_level(self):
2016-03-08 09:34:33 +00:00
"""Volume level of the media player (0..1)."""
2015-09-12 03:06:03 +00:00
return self.current_volume/100.0
@property
def media_content_id(self):
2016-03-08 09:34:33 +00:00
"""Content ID of current playing media."""
2015-09-12 03:06:03 +00:00
return self.content_id
@property
def media_content_type(self):
2016-03-08 09:34:33 +00:00
"""Content type of current playing media."""
2015-09-12 03:06:03 +00:00
return MEDIA_TYPE_MUSIC
@property
def media_image_url(self):
2016-03-08 09:34:33 +00:00
"""Image url of current playing media."""
2015-09-12 04:16:51 +00:00
if self.player_state in (STATE_PLAYING, STATE_IDLE, STATE_PAUSED) and \
self.current_title is not None:
2015-09-12 03:06:03 +00:00
return self.client.artwork_url()
return 'https://cloud.githubusercontent.com/assets/260/9829355' \
'/33fab972-58cf-11e5-8ea2-2ca74bdaae40.png'
2015-09-12 04:16:51 +00:00
2015-09-12 03:06:03 +00:00
@property
def media_title(self):
2016-03-08 09:34:33 +00:00
"""Title of current playing media."""
2015-09-12 03:06:03 +00:00
return self.current_title
@property
def media_artist(self):
2016-03-08 09:34:33 +00:00
"""Artist of current playing media (Music track only)."""
2015-09-12 03:06:03 +00:00
return self.current_artist
@property
def media_album_name(self):
2016-03-08 09:34:33 +00:00
"""Album of current playing media (Music track only)."""
2015-09-12 03:06:03 +00:00
return self.current_album
2015-10-07 03:12:30 +00:00
@property
def media_playlist(self):
2016-03-08 09:34:33 +00:00
"""Title of the currently playing playlist."""
2015-10-07 03:12:30 +00:00
return self.current_playlist
2015-09-12 03:06:03 +00:00
@property
def supported_features(self):
"""Flag media player features that are supported."""
2015-09-12 03:06:03 +00:00
return SUPPORT_ITUNES
def set_volume_level(self, volume):
2016-03-08 09:34:33 +00:00
"""Set volume level, range 0..1."""
2015-09-12 04:42:11 +00:00
response = self.client.set_volume(int(volume * 100))
self.update_state(response)
2015-09-12 03:06:03 +00:00
def mute_volume(self, mute):
2016-03-08 09:34:33 +00:00
"""Mute (true) or unmute (false) media player."""
2015-09-12 04:42:11 +00:00
response = self.client.set_muted(mute)
self.update_state(response)
2015-09-12 03:06:03 +00:00
def media_play(self):
2016-03-08 09:34:33 +00:00
"""Send media_play command to media player."""
2015-09-12 04:42:11 +00:00
response = self.client.play()
self.update_state(response)
2015-09-12 03:06:03 +00:00
def media_pause(self):
2016-03-08 09:34:33 +00:00
"""Send media_pause command to media player."""
2015-09-12 04:42:11 +00:00
response = self.client.pause()
self.update_state(response)
2015-09-12 03:06:03 +00:00
def media_next_track(self):
2016-03-08 09:34:33 +00:00
"""Send media_next command to media player."""
2015-09-12 04:42:11 +00:00
response = self.client.next()
self.update_state(response)
2015-09-12 03:06:03 +00:00
def media_previous_track(self):
2016-03-08 09:34:33 +00:00
"""Send media_previous command media player."""
2015-09-12 04:42:11 +00:00
response = self.client.previous()
self.update_state(response)
2015-09-14 21:27:00 +00:00
def play_media(self, media_type, media_id, **kwargs):
2016-03-08 09:34:33 +00:00
"""Send the play_media command to the media player."""
2015-10-07 05:55:15 +00:00
if media_type == MEDIA_TYPE_PLAYLIST:
response = self.client.play_playlist(media_id)
self.update_state(response)
2015-10-07 03:12:41 +00:00
2015-09-14 21:27:00 +00:00
class AirPlayDevice(MediaPlayerDevice):
2016-03-08 09:34:33 +00:00
"""Representation an AirPlay device via an iTunes API instance."""
2015-09-14 21:27:00 +00:00
2015-09-14 21:39:43 +00:00
def __init__(self, device_id, client):
2016-03-08 09:34:33 +00:00
"""Initialize the AirPlay device."""
2015-09-14 21:39:43 +00:00
self._id = device_id
2015-09-14 21:27:00 +00:00
self.client = client
self.device_name = "AirPlay"
self.kind = None
self.active = False
self.selected = False
self.volume = 0
self.supports_audio = False
self.supports_video = False
2015-09-14 21:39:43 +00:00
self.player_state = None
2015-09-14 21:27:00 +00:00
def update_state(self, state_hash):
2016-03-08 09:34:33 +00:00
"""Update all the state properties with the passed in dictionary."""
2015-09-14 21:27:00 +00:00
if 'player_state' in state_hash:
self.player_state = state_hash.get('player_state', None)
if 'name' in state_hash:
name = state_hash.get('name', '')
2015-09-16 01:40:39 +00:00
self.device_name = (name + ' AirTunes Speaker').strip()
2015-09-14 21:27:00 +00:00
if 'kind' in state_hash:
self.kind = state_hash.get('kind', None)
if 'active' in state_hash:
self.active = state_hash.get('active', None)
if 'selected' in state_hash:
self.selected = state_hash.get('selected', None)
if 'sound_volume' in state_hash:
self.volume = state_hash.get('sound_volume', 0)
if 'supports_audio' in state_hash:
self.supports_audio = state_hash.get('supports_audio', None)
if 'supports_video' in state_hash:
self.supports_video = state_hash.get('supports_video', None)
@property
def name(self):
2016-03-08 09:34:33 +00:00
"""Return the name of the device."""
2015-09-14 21:27:00 +00:00
return self.device_name
@property
def icon(self):
2016-03-08 09:34:33 +00:00
"""Return the icon to use in the frontend, if any."""
if self.selected is True:
return 'mdi:volume-high'
return 'mdi:volume-off'
2015-09-14 21:27:00 +00:00
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Return the state of the device."""
2015-09-14 21:27:00 +00:00
if self.selected is True:
return STATE_ON
return STATE_OFF
2015-09-14 21:27:00 +00:00
def update(self):
2016-03-08 09:34:33 +00:00
"""Retrieve latest state."""
2015-09-14 21:27:00 +00:00
@property
def volume_level(self):
2016-03-08 09:34:33 +00:00
"""Return the volume."""
2015-09-14 21:27:00 +00:00
return float(self.volume)/100.0
@property
def media_content_type(self):
2016-03-08 09:34:33 +00:00
"""Flag of media content that is supported."""
2015-09-14 21:27:00 +00:00
return MEDIA_TYPE_MUSIC
@property
def supported_features(self):
"""Flag media player features that are supported."""
2015-09-14 21:27:00 +00:00
return SUPPORT_AIRPLAY
def set_volume_level(self, volume):
2016-03-08 09:34:33 +00:00
"""Set volume level, range 0..1."""
2015-09-14 21:27:00 +00:00
volume = int(volume * 100)
response = self.client.set_volume_airplay_device(self._id, volume)
self.update_state(response)
def turn_on(self):
2016-03-08 09:34:33 +00:00
"""Select AirPlay."""
2015-09-14 21:27:00 +00:00
self.update_state({"selected": True})
self.schedule_update_ha_state()
2015-09-14 21:27:00 +00:00
response = self.client.toggle_airplay_device(self._id, True)
self.update_state(response)
def turn_off(self):
2016-03-08 09:34:33 +00:00
"""Deselect AirPlay."""
2015-09-14 21:27:00 +00:00
self.update_state({"selected": False})
self.schedule_update_ha_state()
2015-09-14 21:27:00 +00:00
response = self.client.toggle_airplay_device(self._id, False)
self.update_state(response)