addressed PR comments

This commit is contained in:
happyleavesaoc 2015-10-10 16:45:13 -04:00
parent d4d91bfdbb
commit 168516f5da

View File

@ -42,6 +42,7 @@ Possible states are:
- disconnected (can't communicate with device) - disconnected (can't communicate with device)
""" """
import logging
import requests import requests
from homeassistant.const import ( from homeassistant.const import (
@ -59,18 +60,35 @@ SUPPORT_FIRETV = SUPPORT_PAUSE | \
SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET
DOMAIN = 'firetv' DOMAIN = 'firetv'
DEVICE_LIST_URL = 'http://{0}/devices/list'
DEVICE_STATE_URL = 'http://{0}/devices/state/{1}'
DEVICE_ACTION_URL = 'http://{0}/devices/action/{1}/{2}'
_LOGGER = logging.getLogger(__name__)
# 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):
""" Sets up the firetv platform. """ """ Sets up the firetv platform. """
add_devices([ host = config.get('host', 'localhost:5556')
FireTVDevice( device_id = config.get('device', 'default')
config.get('host', 'localhost:5556'), try:
config.get('device', 'default'), response = requests.get(DEVICE_LIST_URL.format(host)).json()
config.get('name', 'Amazon Fire TV') if device_id in response['devices'].keys():
) add_devices([
]) FireTVDevice(
host,
device_id,
config.get('name', 'Amazon Fire TV')
)
])
_LOGGER.info(
'Device %s accessible and ready for control', device_id)
else:
_LOGGER.warn(
'Device %s is not registered with firetv-server', device_id)
except requests.exceptions.RequestException:
_LOGGER.error('Could not connect to firetv-server at %s', host)
class FireTV(object): class FireTV(object):
@ -84,9 +102,6 @@ class FireTV(object):
HTTP server (which must be running via Python 2). HTTP server (which must be running via Python 2).
""" """
DEVICE_STATE_URL = 'http://{0}/devices/state/{1}'
DEVICE_ACTION_URL = 'http://{0}/devices/action/{1}/{2}'
def __init__(self, host, device_id): def __init__(self, host, device_id):
self.host = host self.host = host
self.device_id = device_id self.device_id = device_id
@ -99,15 +114,15 @@ class FireTV(object):
""" """
try: try:
response = requests.get( response = requests.get(
FireTV.DEVICE_STATE_URL.format( DEVICE_STATE_URL.format(
self.host, self.host,
self.device_id self.device_id
) )
) ).json()
return response.json()['state'] return response.get('state', STATE_UNKNOWN)
except requests.exceptions.HTTPError:
return STATE_UNKNOWN
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
_LOGGER.error(
'Could not retrieve device state for %s', self.device_id)
return STATE_UNKNOWN return STATE_UNKNOWN
def action(self, action_id): def action(self, action_id):
@ -118,16 +133,16 @@ class FireTV(object):
""" """
try: try:
requests.get( requests.get(
FireTV.DEVICE_ACTION_URL.format( DEVICE_ACTION_URL.format(
self.host, self.host,
self.device_id, self.device_id,
action_id action_id
) )
) )
except requests.exceptions.HTTPError:
pass
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
pass _LOGGER.error(
'Action request for %s was not accepted for device %s',
action_id, self.device_id)
class FireTVDevice(MediaPlayerDevice): class FireTVDevice(MediaPlayerDevice):
@ -136,6 +151,7 @@ class FireTVDevice(MediaPlayerDevice):
def __init__(self, host, device, name): def __init__(self, host, device, name):
self._firetv = FireTV(host, device) self._firetv = FireTV(host, device)
self._name = name self._name = name
self._state = STATE_UNKNOWN
@property @property
def name(self): def name(self):
@ -155,15 +171,18 @@ class FireTVDevice(MediaPlayerDevice):
@property @property
def state(self): def state(self):
""" State of the player. """ """ State of the player. """
state_map = { return self._state
def update(self):
""" Update device state. """
self._state = {
'idle': STATE_IDLE, 'idle': STATE_IDLE,
'off': STATE_OFF, 'off': STATE_OFF,
'play': STATE_PLAYING, 'play': STATE_PLAYING,
'pause': STATE_PAUSED, 'pause': STATE_PAUSED,
'standby': STATE_STANDBY, 'standby': STATE_STANDBY,
'disconnected': STATE_UNKNOWN, 'disconnected': STATE_UNKNOWN,
} }.get(self._firetv.state, STATE_UNKNOWN)
return state_map.get(self._firetv.state, STATE_UNKNOWN)
def turn_on(self): def turn_on(self):
""" Turns on the device. """ """ Turns on the device. """