parent
79bff0fc57
commit
4791b5679e
|
@ -7,55 +7,75 @@ https://home-assistant.io/components/media_player.itunes/
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
|
MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
|
||||||
SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK, SUPPORT_TURN_OFF,
|
SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK, SUPPORT_TURN_OFF,
|
||||||
SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
|
SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, PLATFORM_SCHEMA,
|
||||||
MediaPlayerDevice)
|
MediaPlayerDevice)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_IDLE, STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING)
|
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
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_NAME = 'iTunes'
|
||||||
|
DEFAULT_PORT = 8181
|
||||||
|
DEFAULT_TIMEOUT = 10
|
||||||
|
DEFAULT_SSL = False
|
||||||
|
DOMAIN = 'itunes'
|
||||||
|
|
||||||
SUPPORT_ITUNES = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
SUPPORT_ITUNES = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
||||||
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK | \
|
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK | \
|
||||||
SUPPORT_PLAY_MEDIA
|
SUPPORT_PLAY_MEDIA
|
||||||
|
|
||||||
SUPPORT_AIRPLAY = SUPPORT_VOLUME_SET | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
|
SUPPORT_AIRPLAY = SUPPORT_VOLUME_SET | SUPPORT_TURN_ON | SUPPORT_TURN_OFF
|
||||||
|
|
||||||
DOMAIN = 'itunes'
|
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,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
class Itunes(object):
|
class Itunes(object):
|
||||||
"""iTunes API client."""
|
"""The iTunes API client."""
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port, use_ssl):
|
||||||
"""Initialize the iTunes device."""
|
"""Initialize the iTunes device."""
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.use_ssl = use_ssl
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _base_url(self):
|
def _base_url(self):
|
||||||
"""Return the base url for endpoints."""
|
"""Return the base url for endpoints."""
|
||||||
if self.port:
|
if self.use_ssl:
|
||||||
return self.host + ":" + str(self.port)
|
uri_scheme = 'https://'
|
||||||
else:
|
else:
|
||||||
return self.host
|
uri_scheme = 'http://'
|
||||||
|
|
||||||
|
if self.port:
|
||||||
|
return '{}{}:{}'.format(uri_scheme, self.host, self.port)
|
||||||
|
else:
|
||||||
|
return '{}{}'.format(uri_scheme, self.host)
|
||||||
|
|
||||||
def _request(self, method, path, params=None):
|
def _request(self, method, path, params=None):
|
||||||
"""Make the actual request and returns the parsed response."""
|
"""Make the actual request and return the parsed response."""
|
||||||
url = self._base_url + path
|
url = '{}{}'.format(self._base_url, path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if method == 'GET':
|
if method == 'GET':
|
||||||
response = requests.get(url)
|
response = requests.get(url, timeout=DEFAULT_TIMEOUT)
|
||||||
elif method == "POST":
|
elif method == 'POST':
|
||||||
response = requests.put(url, params)
|
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
|
||||||
elif method == "PUT":
|
elif method == 'PUT':
|
||||||
response = requests.put(url, params)
|
response = requests.put(url, params, timeout=DEFAULT_TIMEOUT)
|
||||||
elif method == "DELETE":
|
elif method == 'DELETE':
|
||||||
response = requests.delete(url)
|
response = requests.delete(url, timeout=DEFAULT_TIMEOUT)
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.exceptions.HTTPError:
|
except requests.exceptions.HTTPError:
|
||||||
|
@ -136,12 +156,14 @@ class Itunes(object):
|
||||||
# pylint: disable=unused-argument, abstract-method
|
# pylint: disable=unused-argument, abstract-method
|
||||||
# pylint: disable=too-many-instance-attributes
|
# pylint: disable=too-many-instance-attributes
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the itunes platform."""
|
"""Setup the iTunes platform."""
|
||||||
add_devices([
|
add_devices([
|
||||||
ItunesDevice(
|
ItunesDevice(
|
||||||
config.get('name', 'iTunes'),
|
config.get(CONF_NAME),
|
||||||
config.get('host'),
|
config.get(CONF_HOST),
|
||||||
config.get('port'),
|
config.get(CONF_PORT),
|
||||||
|
config.get(CONF_SSL),
|
||||||
|
|
||||||
add_devices
|
add_devices
|
||||||
)
|
)
|
||||||
])
|
])
|
||||||
|
@ -150,15 +172,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
class ItunesDevice(MediaPlayerDevice):
|
class ItunesDevice(MediaPlayerDevice):
|
||||||
"""Representation of an iTunes API instance."""
|
"""Representation of an iTunes API instance."""
|
||||||
|
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods, too-many-arguments
|
||||||
def __init__(self, name, host, port, add_devices):
|
def __init__(self, name, host, port, use_ssl, add_devices):
|
||||||
"""Initialize the iTunes device."""
|
"""Initialize the iTunes device."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
|
self._use_ssl = use_ssl
|
||||||
self._add_devices = add_devices
|
self._add_devices = add_devices
|
||||||
|
|
||||||
self.client = Itunes(self._host, self._port)
|
self.client = Itunes(self._host, self._port, self._use_ssl)
|
||||||
|
|
||||||
self.current_volume = None
|
self.current_volume = None
|
||||||
self.muted = None
|
self.muted = None
|
||||||
|
@ -380,9 +403,9 @@ class AirPlayDevice(MediaPlayerDevice):
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon to use in the frontend, if any."""
|
"""Return the icon to use in the frontend, if any."""
|
||||||
if self.selected is True:
|
if self.selected is True:
|
||||||
return "mdi:volume-high"
|
return 'mdi:volume-high'
|
||||||
else:
|
else:
|
||||||
return "mdi:volume-off"
|
return 'mdi:volume-off'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
|
Loading…
Reference in New Issue