Migrate to voluptuous (#3194)

🐬
This commit is contained in:
Fabian Affolter 2016-09-05 17:40:57 +02:00 committed by Teagan Glenn
parent aed59aea7d
commit 9c600012a1

View file

@ -7,46 +7,55 @@ https://home-assistant.io/components/media_player.firetv/
import logging import logging
import requests import requests
import voluptuous as vol
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, PLATFORM_SCHEMA,
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_SET, MediaPlayerDevice) SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_SET, MediaPlayerDevice)
from homeassistant.const import ( from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY, STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY,
STATE_UNKNOWN) STATE_UNKNOWN, CONF_HOST, CONF_PORT, CONF_NAME, CONF_DEVICE, CONF_DEVICES)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
SUPPORT_FIRETV = SUPPORT_PAUSE | \ SUPPORT_FIRETV = SUPPORT_PAUSE | \
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_PREVIOUS_TRACK | \ SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_PREVIOUS_TRACK | \
SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET
DOMAIN = 'firetv' DEFAULT_DEVICE = 'default'
DEVICE_LIST_URL = 'http://{0}/devices/list' DEFAULT_HOST = 'localhost'
DEVICE_STATE_URL = 'http://{0}/devices/state/{1}' DEFAULT_NAME = 'Amazon Fire TV'
DEVICE_ACTION_URL = 'http://{0}/devices/action/{1}/{2}' DEFAULT_PORT = 5556
DEVICE_ACTION_URL = 'http://{0}:{1}/devices/action/{2}/{3}'
DEVICE_LIST_URL = 'http://{0}:{1}/devices/list'
DEVICE_STATE_URL = 'http://{0}:{1}/devices/state/{2}'
_LOGGER = logging.getLogger(__name__) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_DEVICE, default=DEFAULT_DEVICE): cv.string,
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,
})
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the FireTV platform.""" """Setup the FireTV platform."""
host = config.get('host', 'localhost:5556') name = config.get(CONF_NAME)
device_id = config.get('device', 'default') host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
device_id = config.get(CONF_DEVICE)
try: try:
response = requests.get(DEVICE_LIST_URL.format(host)).json() response = requests.get(DEVICE_LIST_URL.format(host, port)).json()
if device_id in response['devices'].keys(): if device_id in response[CONF_DEVICES].keys():
add_devices([ add_devices([FireTVDevice(host, port, device_id, name)])
FireTVDevice( _LOGGER.info('Device %s accessible and ready for control',
host, device_id)
device_id,
config.get('name', 'Amazon Fire TV')
)
])
_LOGGER.info(
'Device %s accessible and ready for control', device_id)
else: else:
_LOGGER.warning( _LOGGER.warning('Device %s is not registered with firetv-server',
'Device %s is not registered with firetv-server', device_id) device_id)
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
_LOGGER.error('Could not connect to firetv-server at %s', host) _LOGGER.error('Could not connect to firetv-server at %s', host)
@ -62,9 +71,10 @@ class FireTV(object):
be running via Python 2). be running via Python 2).
""" """
def __init__(self, host, device_id): def __init__(self, host, port, device_id):
"""Initialize the FireTV server.""" """Initialize the FireTV server."""
self.host = host self.host = host
self.port = port
self.device_id = device_id self.device_id = device_id
@property @property
@ -73,10 +83,7 @@ class FireTV(object):
try: try:
response = requests.get( response = requests.get(
DEVICE_STATE_URL.format( DEVICE_STATE_URL.format(
self.host, self.host, self.port, self.device_id), timeout=10).json()
self.device_id
)
).json()
return response.get('state', STATE_UNKNOWN) return response.get('state', STATE_UNKNOWN)
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
_LOGGER.error( _LOGGER.error(
@ -86,13 +93,8 @@ class FireTV(object):
def action(self, action_id): def action(self, action_id):
"""Perform an action on the device.""" """Perform an action on the device."""
try: try:
requests.get( requests.get(DEVICE_ACTION_URL.format(
DEVICE_ACTION_URL.format( self.host, self.port, self.device_id, action_id), timeout=10)
self.host,
self.device_id,
action_id
)
)
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
_LOGGER.error( _LOGGER.error(
'Action request for %s was not accepted for device %s', 'Action request for %s was not accepted for device %s',
@ -103,9 +105,9 @@ class FireTVDevice(MediaPlayerDevice):
"""Representation of an Amazon Fire TV device on the network.""" """Representation of an Amazon Fire TV device on the network."""
# pylint: disable=abstract-method # pylint: disable=abstract-method
def __init__(self, host, device, name): def __init__(self, host, port, device, name):
"""Initialize the FireTV device.""" """Initialize the FireTV device."""
self._firetv = FireTV(host, device) self._firetv = FireTV(host, port, device)
self._name = name self._name = name
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN