Use voluptuous for iTunes (#3344)

* Migrate to voluptuous

* Add support for SSL/TLS
pull/3353/merge
Fabian Affolter 2016-09-14 08:01:51 +02:00 committed by Paulus Schoutsen
parent 79bff0fc57
commit 4791b5679e
1 changed files with 49 additions and 26 deletions

View File

@ -7,55 +7,75 @@ https://home-assistant.io/components/media_player.itunes/
import logging
import requests
import voluptuous as vol
from homeassistant.components.media_player import (
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,
SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, PLATFORM_SCHEMA,
MediaPlayerDevice)
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__)
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_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK | \
SUPPORT_PLAY_MEDIA
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):
"""iTunes API client."""
"""The iTunes API client."""
def __init__(self, host, port):
def __init__(self, host, port, use_ssl):
"""Initialize the iTunes device."""
self.host = host
self.port = port
self.use_ssl = use_ssl
@property
def _base_url(self):
"""Return the base url for endpoints."""
if self.port:
return self.host + ":" + str(self.port)
if self.use_ssl:
uri_scheme = 'https://'
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):
"""Make the actual request and returns the parsed response."""
url = self._base_url + path
"""Make the actual request and return the parsed response."""
url = '{}{}'.format(self._base_url, path)
try:
if method == 'GET':
response = requests.get(url)
elif method == "POST":
response = requests.put(url, params)
elif method == "PUT":
response = requests.put(url, params)
elif method == "DELETE":
response = requests.delete(url)
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)
return response.json()
except requests.exceptions.HTTPError:
@ -136,12 +156,14 @@ class Itunes(object):
# pylint: disable=unused-argument, abstract-method
# pylint: disable=too-many-instance-attributes
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the itunes platform."""
"""Setup the iTunes platform."""
add_devices([
ItunesDevice(
config.get('name', 'iTunes'),
config.get('host'),
config.get('port'),
config.get(CONF_NAME),
config.get(CONF_HOST),
config.get(CONF_PORT),
config.get(CONF_SSL),
add_devices
)
])
@ -150,15 +172,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class ItunesDevice(MediaPlayerDevice):
"""Representation of an iTunes API instance."""
# pylint: disable=too-many-public-methods
def __init__(self, name, host, port, add_devices):
# pylint: disable=too-many-public-methods, too-many-arguments
def __init__(self, name, host, port, use_ssl, add_devices):
"""Initialize the iTunes device."""
self._name = name
self._host = host
self._port = port
self._use_ssl = use_ssl
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.muted = None
@ -380,9 +403,9 @@ class AirPlayDevice(MediaPlayerDevice):
def icon(self):
"""Return the icon to use in the frontend, if any."""
if self.selected is True:
return "mdi:volume-high"
return 'mdi:volume-high'
else:
return "mdi:volume-off"
return 'mdi:volume-off'
@property
def state(self):