core/homeassistant/components/itunes/media_player.py

508 lines
15 KiB
Python
Raw Normal View History

"""Support for interfacing to iTunes API."""
2015-11-29 22:04:44 +00:00
import requests
import voluptuous as vol
2015-11-29 22:04:44 +00:00
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
from homeassistant.components.media_player.const import (
2019-07-31 19:25:30 +00:00
MEDIA_TYPE_MUSIC,
MEDIA_TYPE_PLAYLIST,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SEEK,
SUPPORT_SHUFFLE_SET,
2019-07-31 19:25:30 +00:00
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_SET,
)
2015-09-12 03:06:03 +00:00
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_SSL,
STATE_IDLE,
STATE_OFF,
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
)
import homeassistant.helpers.config_validation as cv
2015-09-12 03:06:03 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "iTunes"
DEFAULT_PORT = 8181
DEFAULT_SSL = False
2018-09-09 12:26:06 +00:00
DEFAULT_TIMEOUT = 10
2019-07-31 19:25:30 +00:00
DOMAIN = "itunes"
SUPPORT_ITUNES = (
SUPPORT_PAUSE
| SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_MUTE
| SUPPORT_PREVIOUS_TRACK
| SUPPORT_NEXT_TRACK
| SUPPORT_SEEK
| SUPPORT_PLAY_MEDIA
| SUPPORT_PLAY
| SUPPORT_TURN_OFF
| SUPPORT_SHUFFLE_SET
)
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
2019-07-31 19:25:30 +00:00
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
class Itunes:
"""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):
2018-09-09 12:26:06 +00:00
"""Return the base URL for endpoints."""
if self.use_ssl:
2019-07-31 19:25:30 +00:00
uri_scheme = "https://"
else:
2019-07-31 19:25:30 +00:00
uri_scheme = "http://"
2016-01-21 22:18:52 +00:00
if self.port:
return f"{uri_scheme}{self.host}:{self.port}"
return f"{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 = f"{self._base_url}{path}"
2015-09-12 03:06:03 +00:00
try:
2019-07-31 19:25:30 +00:00
if method == "GET":
response = requests.get(url, timeout=DEFAULT_TIMEOUT)
2019-07-31 19:25:30 +00:00
elif method == "POST":
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
2019-07-31 19:25:30 +00:00
elif method == "PUT":
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
2019-07-31 19:25:30 +00:00
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:
2019-07-31 19:25:30 +00:00
return {"player_state": "error"}
2015-09-12 03:06:03 +00:00
except requests.exceptions.RequestException:
2019-07-31 19:25:30 +00:00
return {"player_state": "offline"}
2015-09-12 03:06:03 +00:00
def _command(self, named_command):
2016-03-08 09:34:33 +00:00
"""Make a request for a controlling command."""
return self._request("PUT", f"/{named_command}")
2015-09-12 03:06:03 +00:00
def now_playing(self):
2016-03-08 09:34:33 +00:00
"""Return the current state."""
2019-07-31 19:25:30 +00:00
return self._request("GET", "/now_playing")
2015-09-12 03:06:03 +00:00
def set_volume(self, level):
2016-03-08 09:34:33 +00:00
"""Set the volume and returns the current state, level 0-100."""
2019-07-31 19:25:30 +00:00
return self._request("PUT", "/volume", {"level": level})
2015-09-12 03:06:03 +00:00
def set_muted(self, muted):
2016-03-08 09:34:33 +00:00
"""Mute and returns the current state, muted True or False."""
2019-07-31 19:25:30 +00:00
return self._request("PUT", "/mute", {"muted": muted})
2015-09-12 03:06:03 +00:00
def set_shuffle(self, shuffle):
"""Set the shuffle mode, shuffle True or False."""
2019-07-31 19:25:30 +00:00
return self._request(
"PUT", "/shuffle", {"mode": ("songs" if shuffle else "off")}
)
2015-09-12 03:06:03 +00:00
def play(self):
2016-03-08 09:34:33 +00:00
"""Set playback to play and returns the current state."""
2019-07-31 19:25:30 +00:00
return self._command("play")
2015-09-12 03:06:03 +00:00
def pause(self):
2016-03-08 09:34:33 +00:00
"""Set playback to paused and returns the current state."""
2019-07-31 19:25:30 +00:00
return self._command("pause")
2015-09-12 03:06:03 +00:00
def next(self):
2016-03-08 09:34:33 +00:00
"""Skip to the next track and returns the current state."""
2019-07-31 19:25:30 +00:00
return self._command("next")
2015-09-12 03:06:03 +00:00
def previous(self):
2016-03-08 09:34:33 +00:00
"""Skip back and returns the current state."""
2019-07-31 19:25:30 +00:00
return self._command("previous")
2015-09-12 03:06:03 +00:00
def stop(self):
"""Stop playback and return the current state."""
2019-07-31 19:25:30 +00:00
return self._command("stop")
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."""
2019-07-31 19:25:30 +00:00
response = self._request("GET", "/playlists")
playlists = response.get("playlists", [])
2019-07-31 19:25:30 +00:00
found_playlists = [
playlist
for playlist in playlists
if (playlist_id_or_name in [playlist["name"], playlist["id"]])
]
2015-10-07 05:55:15 +00:00
if found_playlists:
playlist = found_playlists[0]
path = f"/playlists/{playlist['id']}/play"
2019-07-31 19:25:30 +00:00
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."""
return f"{self._base_url}/artwork"
2015-09-12 03:06:03 +00:00
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."""
2019-07-31 19:25:30 +00:00
return self._request("GET", "/airplay_devices")
2015-09-14 21:27:00 +00:00
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."""
return self._request("GET", f"/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."""
2019-07-31 19:25:30 +00:00
command = "on" if toggle else "off"
path = f"/airplay_devices/{device_id}/{command}"
2019-07-31 19:25:30 +00:00
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."""
path = f"/airplay_devices/{device_id}/volume"
2019-07-31 19:25:30 +00:00
return self._request("PUT", path, {"level": level})
2015-09-14 21:27:00 +00:00
2015-09-12 04:49:34 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the iTunes platform."""
2019-07-31 19:25:30 +00:00
add_entities(
[
ItunesDevice(
config.get(CONF_NAME),
config.get(CONF_HOST),
config.get(CONF_PORT),
config[CONF_SSL],
2019-07-31 19:25:30 +00:00
add_entities,
)
]
)
2015-09-12 03:06:03 +00:00
2015-09-12 03:49:43 +00:00
class ItunesDevice(MediaPlayerEntity):
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_entities):
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
self._add_entities = add_entities
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.shuffled = None
2015-09-12 03:06:03 +00:00
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."""
2019-07-31 19:25:30 +00:00
self.player_state = state_hash.get("player_state", None)
2015-09-12 03:06:03 +00:00
2019-07-31 19:25:30 +00:00
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)
2015-09-12 03:06:03 +00:00
2019-07-31 19:25:30 +00:00
_shuffle = state_hash.get("shuffle", None)
self.shuffled = _shuffle == "songs"
2015-09-12 03:06:03 +00:00
@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."""
2019-07-31 19:25:30 +00:00
if self.player_state == "offline" or self.player_state is None:
return "offline"
2015-09-12 03:06:03 +00:00
2019-07-31 19:25:30 +00:00
if self.player_state == "error":
return "error"
2015-09-12 03:06:03 +00:00
2019-07-31 19:25:30 +00:00
if self.player_state == "stopped":
2015-09-12 03:06:03 +00:00
return STATE_IDLE
2019-07-31 19:25:30 +00:00
if self.player_state == "paused":
2015-09-12 03:06:03 +00:00
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()
2019-07-31 19:25:30 +00:00
found_devices = found_devices.get("airplay_devices", [])
2015-09-14 21:27:00 +00:00
new_devices = []
for device_data in found_devices:
2019-07-31 19:25:30 +00:00
device_id = device_data.get("id")
2015-09-14 21:27:00 +00:00
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_entities(new_devices)
2015-09-14 21:27:00 +00:00
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)."""
2019-07-31 19:25:30 +00:00
return self.current_volume / 100.0
2015-09-12 03:06:03 +00:00
@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."""
2019-07-31 19:25:30 +00:00
if (
self.player_state in (STATE_PLAYING, STATE_IDLE, STATE_PAUSED)
and self.current_title is not None
):
return f"{self.client.artwork_url()}?id={self.content_id}"
2019-07-31 19:25:30 +00:00
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
@property
def shuffle(self):
"""Boolean if shuffle is enabled."""
return self.shuffled
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 set_shuffle(self, shuffle):
"""Shuffle (true) or no shuffle (false) media player."""
response = self.client.set_shuffle(shuffle)
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."""
response = self.client.next()
2015-09-12 04:42:11 +00:00
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
def turn_off(self):
"""Turn the media player off."""
response = self.client.stop()
self.update_state(response)
2015-09-14 21:27:00 +00:00
class AirPlayDevice(MediaPlayerEntity):
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."""
2019-07-31 19:25:30 +00:00
if "player_state" in state_hash:
self.player_state = state_hash.get("player_state", None)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "name" in state_hash:
name = state_hash.get("name", "")
self.device_name = f"{name} AirTunes Speaker".strip()
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "kind" in state_hash:
self.kind = state_hash.get("kind", None)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "active" in state_hash:
self.active = state_hash.get("active", None)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "selected" in state_hash:
self.selected = state_hash.get("selected", None)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "sound_volume" in state_hash:
self.volume = state_hash.get("sound_volume", 0)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "supports_audio" in state_hash:
self.supports_audio = state_hash.get("supports_audio", None)
2015-09-14 21:27:00 +00:00
2019-07-31 19:25:30 +00:00
if "supports_video" in state_hash:
self.supports_video = state_hash.get("supports_video", None)
2015-09-14 21:27:00 +00:00
@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:
2019-07-31 19:25:30 +00:00
return "mdi:volume-high"
2019-07-31 19:25:30 +00:00
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."""
2019-07-31 19:25:30 +00:00
return float(self.volume) / 100.0
2015-09-14 21:27:00 +00:00
@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)