core/homeassistant/components/media_player/firetv.py

178 lines
5.4 KiB
Python
Raw Normal View History

2015-10-09 01:07:36 +00:00
"""
2016-03-08 09:34:33 +00:00
Support for functionality to interact with FireTV devices.
2015-10-09 01:07:36 +00:00
2015-10-23 17:01:19 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/media_player.firetv/
2015-10-09 01:07:36 +00:00
"""
2015-10-10 20:45:13 +00:00
import logging
2015-10-09 01:07:36 +00:00
2016-02-19 05:27:50 +00:00
import requests
2015-10-09 01:07:36 +00:00
from homeassistant.components.media_player import (
2016-02-19 05:27:50 +00:00
SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK,
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_SET, MediaPlayerDevice)
from homeassistant.const import (
STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY,
STATE_UNKNOWN)
2015-10-09 01:07:36 +00:00
SUPPORT_FIRETV = SUPPORT_PAUSE | \
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_PREVIOUS_TRACK | \
SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET
DOMAIN = 'firetv'
2015-10-10 20:45:13 +00:00
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__)
2015-10-09 01:07:36 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 09:34:33 +00:00
"""Setup the FireTV platform."""
2015-10-10 20:45:13 +00:00
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:
2015-11-29 21:49:05 +00:00
_LOGGER.warning(
2015-10-10 20:45:13 +00:00
'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)
2015-10-09 01:07:36 +00:00
class FireTV(object):
2016-03-08 09:34:33 +00:00
"""The firetv-server client.
2015-10-09 01:07:36 +00:00
2015-10-23 17:01:19 +00:00
Should a native Python 3 ADB module become available, python-firetv can
support Python 3, it can be added as a dependency, and this class can be
dispensed of.
2015-10-09 01:07:36 +00:00
2015-10-23 17:01:19 +00:00
For now, it acts as a client to the firetv-server HTTP server (which must
be running via Python 2).
2015-10-09 01:07:36 +00:00
"""
def __init__(self, host, device_id):
2016-03-08 09:34:33 +00:00
"""Initialize the FireTV server."""
2015-10-09 01:07:36 +00:00
self.host = host
self.device_id = device_id
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Get the device state. An exception means UNKNOWN state."""
2015-10-09 01:07:36 +00:00
try:
response = requests.get(
2015-10-10 20:45:13 +00:00
DEVICE_STATE_URL.format(
2015-10-09 01:07:36 +00:00
self.host,
self.device_id
)
2015-10-10 20:45:13 +00:00
).json()
return response.get('state', STATE_UNKNOWN)
2015-10-09 01:07:36 +00:00
except requests.exceptions.RequestException:
2015-10-10 20:45:13 +00:00
_LOGGER.error(
'Could not retrieve device state for %s', self.device_id)
2015-10-09 01:07:36 +00:00
return STATE_UNKNOWN
def action(self, action_id):
2016-03-08 09:34:33 +00:00
"""Perform an action on the device."""
2015-10-09 01:07:36 +00:00
try:
requests.get(
2015-10-10 20:45:13 +00:00
DEVICE_ACTION_URL.format(
2015-10-09 01:07:36 +00:00
self.host,
self.device_id,
action_id
)
)
except requests.exceptions.RequestException:
2015-10-10 20:45:13 +00:00
_LOGGER.error(
'Action request for %s was not accepted for device %s',
action_id, self.device_id)
2015-10-09 01:07:36 +00:00
class FireTVDevice(MediaPlayerDevice):
2016-03-08 09:34:33 +00:00
"""Representation of an Amazon Fire TV device on the network."""
2015-10-09 01:07:36 +00:00
2016-02-02 08:31:36 +00:00
# pylint: disable=abstract-method
2015-10-09 01:07:36 +00:00
def __init__(self, host, device, name):
2016-03-08 09:34:33 +00:00
"""Initialize the FireTV device."""
2015-10-09 01:07:36 +00:00
self._firetv = FireTV(host, device)
self._name = name
2015-10-10 20:45:13 +00:00
self._state = STATE_UNKNOWN
2015-10-09 01:07:36 +00:00
@property
def name(self):
2016-03-08 09:34:33 +00:00
"""Return the device name."""
2015-10-09 01:07:36 +00:00
return self._name
@property
def should_poll(self):
2016-03-08 09:34:33 +00:00
"""Device should be polled."""
2015-10-09 01:07:36 +00:00
return True
@property
def supported_media_commands(self):
2016-03-08 09:34:33 +00:00
"""Flag of media commands that are supported."""
2015-10-09 01:07:36 +00:00
return SUPPORT_FIRETV
@property
def state(self):
2016-03-08 09:34:33 +00:00
"""Return the state of the player."""
2015-10-10 20:45:13 +00:00
return self._state
def update(self):
2016-03-08 09:34:33 +00:00
"""Get the latest date and update device state."""
2015-10-10 20:45:13 +00:00
self._state = {
2015-10-09 01:07:36 +00:00
'idle': STATE_IDLE,
'off': STATE_OFF,
'play': STATE_PLAYING,
'pause': STATE_PAUSED,
'standby': STATE_STANDBY,
'disconnected': STATE_UNKNOWN,
2015-10-10 20:45:13 +00:00
}.get(self._firetv.state, STATE_UNKNOWN)
2015-10-09 01:07:36 +00:00
def turn_on(self):
2016-03-08 09:34:33 +00:00
"""Turn on the device."""
2015-10-09 01:07:36 +00:00
self._firetv.action('turn_on')
def turn_off(self):
2016-03-08 09:34:33 +00:00
"""Turn off the device."""
2015-10-09 01:07:36 +00:00
self._firetv.action('turn_off')
def media_play(self):
2016-03-08 09:34:33 +00:00
"""Send play command."""
2015-10-09 01:07:36 +00:00
self._firetv.action('media_play')
def media_pause(self):
2016-03-08 09:34:33 +00:00
"""Send pause command."""
2015-10-09 01:07:36 +00:00
self._firetv.action('media_pause')
def media_play_pause(self):
2016-03-08 09:34:33 +00:00
"""Send play/pause command."""
2015-10-09 01:07:36 +00:00
self._firetv.action('media_play_pause')
def volume_up(self):
2016-03-08 09:34:33 +00:00
"""Send volume up command."""
2015-10-09 01:07:36 +00:00
self._firetv.action('volume_up')
def volume_down(self):
2016-03-08 09:34:33 +00:00
"""Send volume down command."""
2015-10-09 01:07:36 +00:00
self._firetv.action('volume_down')
def media_previous_track(self):
2016-03-08 09:34:33 +00:00
"""Send previous track command (results in rewind)."""
2015-10-09 01:07:36 +00:00
self._firetv.action('media_previous')
def media_next_track(self):
2016-03-08 09:34:33 +00:00
"""Send next track command (results in fast-forward)."""
2015-10-09 01:07:36 +00:00
self._firetv.action('media_next')