mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Migrate to voluptuous (#3197)
This commit is contained in:
parent
9c600012a1
commit
6bbe3483d9
@ -9,24 +9,41 @@ import json
|
|||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK, SUPPORT_PREVIOUS_TRACK,
|
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 (
|
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
|
from homeassistant.loader import get_component
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
REQUIREMENTS = ['websocket-client==0.37.0']
|
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_GPMDP = SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
|
||||||
SUPPORT_SEEK | SUPPORT_VOLUME_SET
|
SUPPORT_SEEK | SUPPORT_VOLUME_SET
|
||||||
GPMDP_CONFIG_FILE = 'gpmpd.conf'
|
|
||||||
_CONFIGURING = {}
|
|
||||||
|
|
||||||
PLAYBACK_DICT = {'0': STATE_PAUSED, # Stopped
|
PLAYBACK_DICT = {'0': STATE_PAUSED, # Stopped
|
||||||
'1': STATE_PAUSED,
|
'1': STATE_PAUSED,
|
||||||
'2': STATE_PLAYING}
|
'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):
|
def request_configuration(hass, config, url, add_devices_callback):
|
||||||
"""Request configuration steps from the user."""
|
"""Request configuration steps from the user."""
|
||||||
@ -78,7 +95,7 @@ def request_configuration(hass, config, url, add_devices_callback):
|
|||||||
break
|
break
|
||||||
|
|
||||||
_CONFIGURING['gpmdp'] = configurator.request_config(
|
_CONFIGURING['gpmdp'] = configurator.request_config(
|
||||||
hass, "GPM Desktop Player", gpmdp_configuration_callback,
|
hass, DEFAULT_NAME, gpmdp_configuration_callback,
|
||||||
description=(
|
description=(
|
||||||
'Enter the pin that is displayed in the '
|
'Enter the pin that is displayed in the '
|
||||||
'Google Play Music Desktop Player.'),
|
'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."""
|
"""Setup gpmdp."""
|
||||||
name = config.get("name", "GPM Desktop Player")
|
name = config.get(CONF_NAME)
|
||||||
address = config.get("address")
|
host = config.get(CONF_HOST)
|
||||||
url = "ws://" + address + ":5672"
|
port = config.get(CONF_PORT)
|
||||||
|
url = 'ws://{}:{}'.format(host, port)
|
||||||
|
|
||||||
if not code:
|
if not code:
|
||||||
request_configuration(hass, config, url, add_devices_callback)
|
request_configuration(hass, config, url, add_devices)
|
||||||
return
|
return
|
||||||
|
|
||||||
if 'gpmdp' in _CONFIGURING:
|
if 'gpmdp' in _CONFIGURING:
|
||||||
configurator = get_component('configurator')
|
configurator = get_component('configurator')
|
||||||
configurator.request_done(_CONFIGURING.pop('gpmdp'))
|
configurator.request_done(_CONFIGURING.pop('gpmdp'))
|
||||||
|
|
||||||
add_devices_callback([GPMDP(name, url, code)])
|
add_devices([GPMDP(name, url, code)])
|
||||||
|
|
||||||
|
|
||||||
def _load_config(filename):
|
def _load_config(filename):
|
||||||
@ -110,7 +128,7 @@ def _load_config(filename):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(filename, "r") as fdesc:
|
with open(filename, 'r') as fdesc:
|
||||||
inp = fdesc.read()
|
inp = fdesc.read()
|
||||||
|
|
||||||
# In case empty file
|
# In case empty file
|
||||||
@ -126,10 +144,10 @@ def _load_config(filename):
|
|||||||
def _save_config(filename, config):
|
def _save_config(filename, config):
|
||||||
"""Save configuration."""
|
"""Save configuration."""
|
||||||
try:
|
try:
|
||||||
with open(filename, "w") as fdesc:
|
with open(filename, 'w') as fdesc:
|
||||||
fdesc.write(json.dumps(config, indent=4, sort_keys=True))
|
fdesc.write(json.dumps(config, indent=4, sort_keys=True))
|
||||||
except (IOError, TypeError) as error:
|
except (IOError, TypeError) as error:
|
||||||
_LOGGER.error("Saving config file failed: %s", error)
|
_LOGGER.error("Saving configuration file failed: %s", error)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -138,7 +156,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
"""Setup the GPMDP platform."""
|
"""Setup the GPMDP platform."""
|
||||||
codeconfig = _load_config(hass.config.path(GPMDP_CONFIG_FILE))
|
codeconfig = _load_config(hass.config.path(GPMDP_CONFIG_FILE))
|
||||||
if len(codeconfig):
|
if len(codeconfig):
|
||||||
code = codeconfig.get("CODE")
|
code = codeconfig.get('CODE')
|
||||||
elif discovery_info is not None:
|
elif discovery_info is not None:
|
||||||
if 'gpmdp' in _CONFIGURING:
|
if 'gpmdp' in _CONFIGURING:
|
||||||
return
|
return
|
||||||
@ -258,7 +276,7 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def media_seek_position(self):
|
def media_seek_position(self):
|
||||||
"""Time in seconds of current seek positon."""
|
"""Time in seconds of current seek position."""
|
||||||
return self._seek_position
|
return self._seek_position
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -306,9 +324,9 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
websocket = self.get_ws()
|
websocket = self.get_ws()
|
||||||
if websocket is None:
|
if websocket is None:
|
||||||
return
|
return
|
||||||
websocket.send(json.dumps({"namespace": "playback",
|
websocket.send(json.dumps({'namespace': 'playback',
|
||||||
"method": "setCurrentTime",
|
'method': 'setCurrentTime',
|
||||||
"arguments": [position*1000]}))
|
'arguments': [position*1000]}))
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
|
||||||
def volume_up(self):
|
def volume_up(self):
|
||||||
@ -332,7 +350,7 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
websocket = self.get_ws()
|
websocket = self.get_ws()
|
||||||
if websocket is None:
|
if websocket is None:
|
||||||
return
|
return
|
||||||
websocket.send(json.dumps({"namespace": "volume",
|
websocket.send(json.dumps({'namespace': 'volume',
|
||||||
"method": "setVolume",
|
'method': 'setVolume',
|
||||||
"arguments": [volume*100]}))
|
'arguments': [volume*100]}))
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user