Migrate to voluptuous (#3163)

🐬
pull/3176/head
Fabian Affolter 2016-09-04 04:18:11 +02:00 committed by Teagan Glenn
parent 2aab77a486
commit 6a2f0fc456
1 changed files with 30 additions and 18 deletions

View File

@ -6,34 +6,47 @@ https://home-assistant.io/components/media_player.cmus/
""" """
import logging import logging
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_PREVIOUS_TRACK, SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_PREVIOUS_TRACK, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
SUPPORT_VOLUME_SET, SUPPORT_PLAY_MEDIA, SUPPORT_SEEK, SUPPORT_VOLUME_SET, SUPPORT_PLAY_MEDIA, SUPPORT_SEEK, PLATFORM_SCHEMA,
MediaPlayerDevice) MediaPlayerDevice)
from homeassistant.const import (STATE_OFF, STATE_PAUSED, STATE_PLAYING, from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PASSWORD, STATE_OFF, STATE_PAUSED, STATE_PLAYING, CONF_HOST, CONF_NAME, CONF_PORT,
CONF_PORT) CONF_PASSWORD)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pycmus==0.1.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pycmus==0.1.0']
DEFAULT_NAME = 'cmus'
DEFAULT_PORT = 3000
SUPPORT_CMUS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \ SUPPORT_CMUS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \ SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
SUPPORT_PLAY_MEDIA | SUPPORT_SEEK SUPPORT_PLAY_MEDIA | SUPPORT_SEEK
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Inclusive(CONF_HOST, 'remote'): cv.string,
vol.Inclusive(CONF_PASSWORD, 'remote'): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
def setup_platform(hass, config, add_devices, discover_info=None): def setup_platform(hass, config, add_devices, discover_info=None):
"""Setup the CMUS platform.""" """Setup the CMUS platform."""
from pycmus import exceptions from pycmus import exceptions
host = config.get(CONF_HOST, None) host = config.get(CONF_HOST)
password = config.get(CONF_PASSWORD, None) password = config.get(CONF_PASSWORD)
port = config.get(CONF_PORT, None) port = config.get(CONF_PORT)
name = config.get(CONF_NAME, None) name = config.get(CONF_NAME)
if host and not password:
_LOGGER.error("A password must be set if using a remote cmus server")
return False
try: try:
cmus_remote = CmusDevice(host, password, port, name) cmus_remote = CmusDevice(host, password, port, name)
except exceptions.InvalidPassword: except exceptions.InvalidPassword:
@ -43,7 +56,7 @@ def setup_platform(hass, config, add_devices, discover_info=None):
class CmusDevice(MediaPlayerDevice): class CmusDevice(MediaPlayerDevice):
"""Representation of a running CMUS.""" """Representation of a running cmus."""
# pylint: disable=no-member, too-many-public-methods, abstract-method # pylint: disable=no-member, too-many-public-methods, abstract-method
def __init__(self, server, password, port, name): def __init__(self, server, password, port, name):
@ -51,13 +64,12 @@ class CmusDevice(MediaPlayerDevice):
from pycmus import remote from pycmus import remote
if server: if server:
port = port or 3000 self.cmus = remote.PyCmus(
self.cmus = remote.PyCmus(server=server, password=password, server=server, password=password, port=port)
port=port) auto_name = 'cmus-{}'.format(server)
auto_name = "cmus-%s" % server
else: else:
self.cmus = remote.PyCmus() self.cmus = remote.PyCmus()
auto_name = "cmus-local" auto_name = 'cmus-local'
self._name = name or auto_name self._name = name or auto_name
self.status = {} self.status = {}
self.update() self.update()