diff --git a/homeassistant/components/apple_tv/__init__.py b/homeassistant/components/apple_tv/__init__.py index 51c2ee7e1a5..38d520f73da 100644 --- a/homeassistant/components/apple_tv/__init__.py +++ b/homeassistant/components/apple_tv/__init__.py @@ -3,13 +3,15 @@ import asyncio import logging from typing import Sequence, TypeVar, Union +from pyatv import AppleTVDevice, connect_to_apple_tv, scan_for_apple_tvs +from pyatv.exceptions import DeviceAuthenticationError import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant.components.discovery import SERVICE_APPLE_TV from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_NAME from homeassistant.helpers import discovery from homeassistant.helpers.aiohttp_client import async_get_clientsession -import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -80,7 +82,6 @@ def request_configuration(hass, config, atv, credentials): async def configuration_callback(callback_data): """Handle the submitted configuration.""" - from pyatv import exceptions pin = callback_data.get("pin") @@ -93,7 +94,7 @@ def request_configuration(hass, config, atv, credentials): title=NOTIFICATION_AUTH_TITLE, notification_id=NOTIFICATION_AUTH_ID, ) - except exceptions.DeviceAuthenticationError as ex: + except DeviceAuthenticationError as ex: hass.components.persistent_notification.async_create( "Authentication failed! Did you enter correct PIN?

" "Details: {0}".format(ex), @@ -112,11 +113,10 @@ def request_configuration(hass, config, atv, credentials): ) -async def scan_for_apple_tvs(hass): +async def scan_apple_tvs(hass): """Scan for devices and present a notification of the ones found.""" - import pyatv - atvs = await pyatv.scan_for_apple_tvs(hass.loop, timeout=3) + atvs = await scan_for_apple_tvs(hass.loop, timeout=3) devices = [] for atv in atvs: @@ -149,7 +149,7 @@ async def async_setup(hass, config): entity_ids = service.data.get(ATTR_ENTITY_ID) if service.service == SERVICE_SCAN: - hass.async_add_job(scan_for_apple_tvs, hass) + hass.async_add_job(scan_apple_tvs, hass) return if entity_ids: @@ -207,7 +207,6 @@ async def async_setup(hass, config): async def _setup_atv(hass, hass_config, atv_config): """Set up an Apple TV.""" - import pyatv name = atv_config.get(CONF_NAME) host = atv_config.get(CONF_HOST) @@ -218,9 +217,9 @@ async def _setup_atv(hass, hass_config, atv_config): if host in hass.data[DATA_APPLE_TV]: return - details = pyatv.AppleTVDevice(name, host, login_id) + details = AppleTVDevice(name, host, login_id) session = async_get_clientsession(hass) - atv = pyatv.connect_to_apple_tv(details, hass.loop, session=session) + atv = connect_to_apple_tv(details, hass.loop, session=session) if credentials: await atv.airplay.load_credentials(credentials) diff --git a/homeassistant/components/apple_tv/media_player.py b/homeassistant/components/apple_tv/media_player.py index 9ac5ba77f98..c816be52259 100644 --- a/homeassistant/components/apple_tv/media_player.py +++ b/homeassistant/components/apple_tv/media_player.py @@ -1,6 +1,8 @@ """Support for Apple TV media player.""" import logging +import pyatv.const as atv_const + from homeassistant.components.media_player import MediaPlayerDevice from homeassistant.components.media_player.const import ( MEDIA_TYPE_MUSIC, @@ -112,22 +114,21 @@ class AppleTvDevice(MediaPlayerDevice): return STATE_OFF if self._playing: - from pyatv import const state = self._playing.play_state if state in ( - const.PLAY_STATE_IDLE, - const.PLAY_STATE_NO_MEDIA, - const.PLAY_STATE_LOADING, + atv_const.PLAY_STATE_IDLE, + atv_const.PLAY_STATE_NO_MEDIA, + atv_const.PLAY_STATE_LOADING, ): return STATE_IDLE - if state == const.PLAY_STATE_PLAYING: + if state == atv_const.PLAY_STATE_PLAYING: return STATE_PLAYING if state in ( - const.PLAY_STATE_PAUSED, - const.PLAY_STATE_FAST_FORWARD, - const.PLAY_STATE_FAST_BACKWARD, - const.PLAY_STATE_STOPPED, + atv_const.PLAY_STATE_PAUSED, + atv_const.PLAY_STATE_FAST_FORWARD, + atv_const.PLAY_STATE_FAST_BACKWARD, + atv_const.PLAY_STATE_STOPPED, ): # Catch fast forward/backward here so "play" is default action return STATE_PAUSED @@ -156,14 +157,13 @@ class AppleTvDevice(MediaPlayerDevice): def media_content_type(self): """Content type of current playing media.""" if self._playing: - from pyatv import const media_type = self._playing.media_type - if media_type == const.MEDIA_TYPE_VIDEO: + if media_type == atv_const.MEDIA_TYPE_VIDEO: return MEDIA_TYPE_VIDEO - if media_type == const.MEDIA_TYPE_MUSIC: + if media_type == atv_const.MEDIA_TYPE_MUSIC: return MEDIA_TYPE_MUSIC - if media_type == const.MEDIA_TYPE_TV: + if media_type == atv_const.MEDIA_TYPE_TV: return MEDIA_TYPE_TVSHOW @property