Add new feature to Apple TV platform (#8122)
* Lift Apple TV to pyatv 0.3.2 Update code to use basic new features. * Support button presses in Apple TV * Support device authentication * Convert Apple TV to a component A media_player platform and a remote platform will be loaded for each manually configured or discovered device. * Move device auth to apple_tv component * Update requirements and coverage config * Add scan support to apple_tv
This commit is contained in:
1 parent
8185587100
commit
ea5bec3ef4
7 files changed
+422
-102
No files matched your search
@@ -6,70 +6,41 @@ https://home-assistant.io/components/media_player.apple_tv/
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import hashlib
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.apple_tv import (
|
||||
ATTR_ATV, ATTR_POWER, DATA_APPLE_TV, DATA_ENTITIES)
|
||||
from homeassistant.components.media_player import (
|
||||
SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK,
|
||||
SUPPORT_STOP, SUPPORT_PLAY, SUPPORT_PLAY_MEDIA, SUPPORT_TURN_ON,
|
||||
SUPPORT_TURN_OFF, MediaPlayerDevice, PLATFORM_SCHEMA, MEDIA_TYPE_MUSIC,
|
||||
SUPPORT_TURN_OFF, MediaPlayerDevice, MEDIA_TYPE_MUSIC,
|
||||
MEDIA_TYPE_VIDEO, MEDIA_TYPE_TVSHOW)
|
||||
from homeassistant.const import (
|
||||
STATE_IDLE, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY, CONF_HOST,
|
||||
STATE_OFF, CONF_NAME, EVENT_HOMEASSISTANT_STOP)
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
|
||||
REQUIREMENTS = ['pyatv==0.2.1']
|
||||
DEPENDENCIES = ['apple_tv']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_LOGIN_ID = 'login_id'
|
||||
CONF_START_OFF = 'start_off'
|
||||
|
||||
DEFAULT_NAME = 'Apple TV'
|
||||
|
||||
DATA_APPLE_TV = 'apple_tv'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Required(CONF_LOGIN_ID): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_START_OFF, default=False): cv.boolean
|
||||
})
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
"""Set up the Apple TV platform."""
|
||||
import pyatv
|
||||
if not discovery_info:
|
||||
return
|
||||
|
||||
if discovery_info is not None:
|
||||
name = discovery_info['name']
|
||||
host = discovery_info['host']
|
||||
login_id = discovery_info['properties']['hG']
|
||||
start_off = False
|
||||
else:
|
||||
name = config.get(CONF_NAME)
|
||||
host = config.get(CONF_HOST)
|
||||
login_id = config.get(CONF_LOGIN_ID)
|
||||
start_off = config.get(CONF_START_OFF)
|
||||
# Manage entity cache for service handler
|
||||
if DATA_ENTITIES not in hass.data:
|
||||
hass.data[DATA_ENTITIES] = []
|
||||
|
||||
if DATA_APPLE_TV not in hass.data:
|
||||
hass.data[DATA_APPLE_TV] = []
|
||||
|
||||
if host in hass.data[DATA_APPLE_TV]:
|
||||
return False
|
||||
hass.data[DATA_APPLE_TV].append(host)
|
||||
|
||||
details = pyatv.AppleTVDevice(name, host, login_id)
|
||||
session = async_get_clientsession(hass)
|
||||
atv = pyatv.connect_to_apple_tv(details, hass.loop, session=session)
|
||||
entity = AppleTvDevice(atv, name, start_off)
|
||||
name = discovery_info[CONF_NAME]
|
||||
host = discovery_info[CONF_HOST]
|
||||
atv = hass.data[DATA_APPLE_TV][host][ATTR_ATV]
|
||||
power = hass.data[DATA_APPLE_TV][host][ATTR_POWER]
|
||||
entity = AppleTvDevice(atv, name, power)
|
||||
|
||||
@callback
|
||||
def on_hass_stop(event):
|
||||
@@ -78,44 +49,39 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
|
||||
|
||||
if entity not in hass.data[DATA_ENTITIES]:
|
||||
hass.data[DATA_ENTITIES].append(entity)
|
||||
|
||||
async_add_devices([entity])
|
||||
|
||||
|
||||
class AppleTvDevice(MediaPlayerDevice):
|
||||
"""Representation of an Apple TV device."""
|
||||
|
||||
def __init__(self, atv, name, is_off):
|
||||
def __init__(self, atv, name, power):
|
||||
"""Initialize the Apple TV device."""
|
||||
self._atv = atv
|
||||
self.atv = atv
|
||||
self._name = name
|
||||
self._is_off = is_off
|
||||
self._playing = None
|
||||
self._artwork_hash = None
|
||||
self._atv.push_updater.listener = self
|
||||
self._power = power
|
||||
self._power.listeners.append(self)
|
||||
self.atv.push_updater.listener = self
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_added_to_hass(self):
|
||||
"""Handle when an entity is about to be added to Home Assistant."""
|
||||
if not self._is_off:
|
||||
self._atv.push_updater.start()
|
||||
|
||||
@callback
|
||||
def _set_power_off(self, is_off):
|
||||
"""Set the power to off."""
|
||||
self._playing = None
|
||||
self._artwork_hash = None
|
||||
self._is_off = is_off
|
||||
if is_off:
|
||||
self._atv.push_updater.stop()
|
||||
else:
|
||||
self._atv.push_updater.start()
|
||||
self.hass.async_add_job(self.async_update_ha_state())
|
||||
self._power.init()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return an unique ID."""
|
||||
return self.atv.metadata.device_id
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
@@ -124,16 +90,16 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
if self._is_off:
|
||||
if not self._power.turned_on:
|
||||
return STATE_OFF
|
||||
|
||||
if self._playing is not None:
|
||||
from pyatv import const
|
||||
state = self._playing.play_state
|
||||
if state == const.PLAY_STATE_NO_MEDIA:
|
||||
return STATE_IDLE
|
||||
elif state == const.PLAY_STATE_PLAYING or \
|
||||
if state == const.PLAY_STATE_NO_MEDIA or \
|
||||
state == const.PLAY_STATE_LOADING:
|
||||
return STATE_IDLE
|
||||
elif state == const.PLAY_STATE_PLAYING:
|
||||
return STATE_PLAYING
|
||||
elif state == const.PLAY_STATE_PAUSED or \
|
||||
state == const.PLAY_STATE_FAST_FORWARD or \
|
||||
@@ -147,24 +113,8 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
def playstatus_update(self, updater, playing):
|
||||
"""Print what is currently playing when it changes."""
|
||||
self._playing = playing
|
||||
|
||||
if self.state == STATE_IDLE:
|
||||
self._artwork_hash = None
|
||||
elif self._has_playing_media_changed(playing):
|
||||
base = str(playing.title) + str(playing.artist) + \
|
||||
str(playing.album) + str(playing.total_time)
|
||||
self._artwork_hash = hashlib.md5(
|
||||
base.encode('utf-8')).hexdigest()
|
||||
|
||||
self.hass.async_add_job(self.async_update_ha_state())
|
||||
|
||||
def _has_playing_media_changed(self, new_playing):
|
||||
if self._playing is None:
|
||||
return True
|
||||
old_playing = self._playing
|
||||
return new_playing.media_type != old_playing.media_type or \
|
||||
new_playing.title != old_playing.title
|
||||
|
||||
@callback
|
||||
def playstatus_error(self, updater, exception):
|
||||
"""Inform about an error and restart push updates."""
|
||||
@@ -177,7 +127,6 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
# implemented here later.
|
||||
updater.start(initial_delay=10)
|
||||
self._playing = None
|
||||
self._artwork_hash = None
|
||||
self.hass.async_add_job(self.async_update_ha_state())
|
||||
|
||||
@property
|
||||
@@ -215,18 +164,18 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
@asyncio.coroutine
|
||||
def async_play_media(self, media_type, media_id, **kwargs):
|
||||
"""Send the play_media command to the media player."""
|
||||
yield from self._atv.remote_control.play_url(media_id, 0)
|
||||
yield from self.atv.airplay.play_url(media_id)
|
||||
|
||||
@property
|
||||
def media_image_hash(self):
|
||||
"""Hash value for media image."""
|
||||
if self.state != STATE_IDLE:
|
||||
return self._artwork_hash
|
||||
if self._playing is not None and self.state != STATE_IDLE:
|
||||
return self._playing.hash
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_get_media_image(self):
|
||||
"""Fetch media image of current playing image."""
|
||||
return (yield from self._atv.metadata.artwork()), 'image/png'
|
||||
return (yield from self.atv.metadata.artwork()), 'image/png'
|
||||
|
||||
@property
|
||||
def media_title(self):
|
||||
@@ -235,9 +184,9 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
if self.state == STATE_IDLE:
|
||||
return 'Nothing playing'
|
||||
title = self._playing.title
|
||||
return title if title else "No title"
|
||||
return title if title else 'No title'
|
||||
|
||||
return 'Not connected to Apple TV'
|
||||
return 'Establishing a connection to {0}...'.format(self._name)
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
@@ -254,12 +203,13 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
@asyncio.coroutine
|
||||
def async_turn_on(self):
|
||||
"""Turn the media player on."""
|
||||
self._set_power_off(False)
|
||||
self._power.set_power_on(True)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_turn_off(self):
|
||||
"""Turn the media player off."""
|
||||
self._set_power_off(True)
|
||||
self._playing = None
|
||||
self._power.set_power_on(False)
|
||||
|
||||
def async_media_play_pause(self):
|
||||
"""Pause media on media player.
|
||||
@@ -269,9 +219,9 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
if self._playing is not None:
|
||||
state = self.state
|
||||
if state == STATE_PAUSED:
|
||||
return self._atv.remote_control.play()
|
||||
return self.atv.remote_control.play()
|
||||
elif state == STATE_PLAYING:
|
||||
return self._atv.remote_control.pause()
|
||||
return self.atv.remote_control.pause()
|
||||
|
||||
def async_media_play(self):
|
||||
"""Play media.
|
||||
@@ -279,7 +229,15 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self._atv.remote_control.play()
|
||||
return self.atv.remote_control.play()
|
||||
|
||||
def async_media_stop(self):
|
||||
"""Stop the media player.
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self.atv.remote_control.stop()
|
||||
|
||||
def async_media_pause(self):
|
||||
"""Pause the media player.
|
||||
@@ -287,7 +245,7 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self._atv.remote_control.pause()
|
||||
return self.atv.remote_control.pause()
|
||||
|
||||
def async_media_next_track(self):
|
||||
"""Send next track command.
|
||||
@@ -295,7 +253,7 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self._atv.remote_control.next()
|
||||
return self.atv.remote_control.next()
|
||||
|
||||
def async_media_previous_track(self):
|
||||
"""Send previous track command.
|
||||
@@ -303,7 +261,7 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self._atv.remote_control.previous()
|
||||
return self.atv.remote_control.previous()
|
||||
|
||||
def async_media_seek(self, position):
|
||||
"""Send seek command.
|
||||
@@ -311,4 +269,4 @@ class AppleTvDevice(MediaPlayerDevice):
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
if self._playing is not None:
|
||||
return self._atv.remote_control.set_position(position)
|
||||
return self.atv.remote_control.set_position(position)
|
||||
Reference in new issue
Block a user