Migrate to voluptuous (#3197)

This commit is contained in:
Fabian Affolter 2016-09-05 17:45:06 +02:00 committed by Teagan Glenn
parent 9c600012a1
commit 6bbe3483d9

View File

@ -9,24 +9,41 @@ import json
import os
import socket
import voluptuous as vol
from homeassistant.components.media_player import (
MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK, SUPPORT_PREVIOUS_TRACK,
SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_SEEK, MediaPlayerDevice)
SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_SEEK, MediaPlayerDevice,
PLATFORM_SCHEMA)
from homeassistant.const import (
STATE_PLAYING, STATE_PAUSED, STATE_OFF)
STATE_PLAYING, STATE_PAUSED, STATE_OFF, CONF_HOST, CONF_PORT, CONF_NAME)
from homeassistant.loader import get_component
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['websocket-client==0.37.0']
_CONFIGURING = {}
_LOGGER = logging.getLogger(__name__)
DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'GPM Desktop Player'
DEFAULT_PORT = 5672
GPMDP_CONFIG_FILE = 'gpmpd.conf'
SUPPORT_GPMDP = SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
SUPPORT_SEEK | SUPPORT_VOLUME_SET
GPMDP_CONFIG_FILE = 'gpmpd.conf'
_CONFIGURING = {}
PLAYBACK_DICT = {'0': STATE_PAUSED, # Stopped
'1': STATE_PAUSED,
'2': STATE_PLAYING}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
})
def request_configuration(hass, config, url, add_devices_callback):
"""Request configuration steps from the user."""
@ -78,7 +95,7 @@ def request_configuration(hass, config, url, add_devices_callback):
break
_CONFIGURING['gpmdp'] = configurator.request_config(
hass, "GPM Desktop Player", gpmdp_configuration_callback,
hass, DEFAULT_NAME, gpmdp_configuration_callback,
description=(
'Enter the pin that is displayed in the '
'Google Play Music Desktop Player.'),
@ -87,21 +104,22 @@ def request_configuration(hass, config, url, add_devices_callback):
)
def setup_gpmdp(hass, config, code, add_devices_callback):
def setup_gpmdp(hass, config, code, add_devices):
"""Setup gpmdp."""
name = config.get("name", "GPM Desktop Player")
address = config.get("address")
url = "ws://" + address + ":5672"
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
url = 'ws://{}:{}'.format(host, port)
if not code:
request_configuration(hass, config, url, add_devices_callback)
request_configuration(hass, config, url, add_devices)
return
if 'gpmdp' in _CONFIGURING:
configurator = get_component('configurator')
configurator.request_done(_CONFIGURING.pop('gpmdp'))
add_devices_callback([GPMDP(name, url, code)])
add_devices([GPMDP(name, url, code)])
def _load_config(filename):
@ -110,7 +128,7 @@ def _load_config(filename):
return {}
try:
with open(filename, "r") as fdesc:
with open(filename, 'r') as fdesc:
inp = fdesc.read()
# In case empty file
@ -126,10 +144,10 @@ def _load_config(filename):
def _save_config(filename, config):
"""Save configuration."""
try:
with open(filename, "w") as fdesc:
with open(filename, 'w') as fdesc:
fdesc.write(json.dumps(config, indent=4, sort_keys=True))
except (IOError, TypeError) as error:
_LOGGER.error("Saving config file failed: %s", error)
_LOGGER.error("Saving configuration file failed: %s", error)
return False
return True
@ -138,7 +156,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup the GPMDP platform."""
codeconfig = _load_config(hass.config.path(GPMDP_CONFIG_FILE))
if len(codeconfig):
code = codeconfig.get("CODE")
code = codeconfig.get('CODE')
elif discovery_info is not None:
if 'gpmdp' in _CONFIGURING:
return
@ -258,7 +276,7 @@ class GPMDP(MediaPlayerDevice):
@property
def media_seek_position(self):
"""Time in seconds of current seek positon."""
"""Time in seconds of current seek position."""
return self._seek_position
@property
@ -306,9 +324,9 @@ class GPMDP(MediaPlayerDevice):
websocket = self.get_ws()
if websocket is None:
return
websocket.send(json.dumps({"namespace": "playback",
"method": "setCurrentTime",
"arguments": [position*1000]}))
websocket.send(json.dumps({'namespace': 'playback',
'method': 'setCurrentTime',
'arguments': [position*1000]}))
self.update_ha_state()
def volume_up(self):
@ -332,7 +350,7 @@ class GPMDP(MediaPlayerDevice):
websocket = self.get_ws()
if websocket is None:
return
websocket.send(json.dumps({"namespace": "volume",
"method": "setVolume",
"arguments": [volume*100]}))
websocket.send(json.dumps({'namespace': 'volume',
'method': 'setVolume',
'arguments': [volume*100]}))
self.update_ha_state()