Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use voluptuous for SqueezeBox #3212

Merged
merged 2 commits into from
Sep 5, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 27 additions & 25 deletions homeassistant/components/media_player/squeezebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,55 @@
import telnetlib
import urllib.parse

import voluptuous as vol

from homeassistant.components.media_player import (
DOMAIN, MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, PLATFORM_SCHEMA,
SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, MediaPlayerDevice)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, STATE_IDLE, STATE_OFF,
STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN)
STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN, CONF_NAME, CONF_PORT)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'SqueezeBox'
DEFAULT_PORT = 9090

KNOWN_DEVICES = []

SUPPORT_SQUEEZEBOX = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | \
SUPPORT_VOLUME_MUTE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
SUPPORT_SEEK | SUPPORT_TURN_ON | SUPPORT_TURN_OFF

KNOWN_DEVICES = []
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is name used for? It isn't referenced anywhere else in this file and not mentioned in the component documentation.

vol.Optional(CONF_PASSWORD): cv.string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be awesome to keep these in order:

HOST
PORT
NAME
USERNAME
PASSWORD

But unneeded

vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_USERNAME): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the squeezebox platform."""
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)

if discovery_info is not None:
host = discovery_info[0]
port = 9090
port = DEFAULT_PORT
else:
host = config.get(CONF_HOST)
port = int(config.get('port', 9090))

if not host:
_LOGGER.error(
"Missing required configuration items in %s: %s",
DOMAIN,
CONF_HOST)
return False
port = config.get(CONF_PORT)

# Only add a media server once
if host in KNOWN_DEVICES:
return False
KNOWN_DEVICES.append(host)

lms = LogitechMediaServer(
host, port,
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD))
lms = LogitechMediaServer(host, port, username, password)

if not lms.init_success:
return False
Expand Down Expand Up @@ -77,18 +84,13 @@ def _get_http_port(self):
try:
http_port = self.query('pref', 'httpport', '?')
if not http_port:
_LOGGER.error(
"Unable to read data from server %s:%s",
self.host,
self.port)
_LOGGER.error("Unable to read data from server %s:%s",
self.host, self.port)
return
return http_port
except ConnectionError as ex:
_LOGGER.error(
"Failed to connect to server %s:%s - %s",
self.host,
self.port,
ex)
_LOGGER.error("Failed to connect to server %s:%s - %s",
self.host, self.port, ex)
return

def create_players(self):
Expand Down