diff --git a/homeassistant/components/media_player/firetv.py b/homeassistant/components/media_player/firetv.py index c0dedb27b77..9db17416bda 100644 --- a/homeassistant/components/media_player/firetv.py +++ b/homeassistant/components/media_player/firetv.py @@ -42,6 +42,7 @@ Possible states are: - disconnected (can't communicate with device) """ +import logging import requests from homeassistant.const import ( @@ -59,18 +60,35 @@ SUPPORT_FIRETV = SUPPORT_PAUSE | \ SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET 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 def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the firetv platform. """ - add_devices([ - FireTVDevice( - config.get('host', 'localhost:5556'), - config.get('device', 'default'), - config.get('name', 'Amazon Fire TV') - ) - ]) + host = config.get('host', 'localhost:5556') + device_id = config.get('device', 'default') + try: + response = requests.get(DEVICE_LIST_URL.format(host)).json() + 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): @@ -84,9 +102,6 @@ class FireTV(object): 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): self.host = host self.device_id = device_id @@ -99,15 +114,15 @@ class FireTV(object): """ try: response = requests.get( - FireTV.DEVICE_STATE_URL.format( + DEVICE_STATE_URL.format( self.host, self.device_id ) - ) - return response.json()['state'] - except requests.exceptions.HTTPError: - return STATE_UNKNOWN + ).json() + return response.get('state', STATE_UNKNOWN) except requests.exceptions.RequestException: + _LOGGER.error( + 'Could not retrieve device state for %s', self.device_id) return STATE_UNKNOWN def action(self, action_id): @@ -118,16 +133,16 @@ class FireTV(object): """ try: requests.get( - FireTV.DEVICE_ACTION_URL.format( + DEVICE_ACTION_URL.format( self.host, self.device_id, action_id ) ) - except requests.exceptions.HTTPError: - pass 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): @@ -136,6 +151,7 @@ class FireTVDevice(MediaPlayerDevice): def __init__(self, host, device, name): self._firetv = FireTV(host, device) self._name = name + self._state = STATE_UNKNOWN @property def name(self): @@ -155,15 +171,18 @@ class FireTVDevice(MediaPlayerDevice): @property def state(self): """ State of the player. """ - state_map = { + return self._state + + def update(self): + """ Update device state. """ + self._state = { 'idle': STATE_IDLE, 'off': STATE_OFF, 'play': STATE_PLAYING, 'pause': STATE_PAUSED, 'standby': STATE_STANDBY, 'disconnected': STATE_UNKNOWN, - } - return state_map.get(self._firetv.state, STATE_UNKNOWN) + }.get(self._firetv.state, STATE_UNKNOWN) def turn_on(self): """ Turns on the device. """