def _setup_atv(hass, atv_config): """Setup an Apple TV.""" import pyatv name = atv_config.get(CONF_NAME) host = atv_config.get(CONF_HOST) login_id = atv_config.get(CONF_LOGIN_ID) start_off = atv_config.get(CONF_START_OFF) credentials = atv_config.get(CONF_CREDENTIALS) if host in hass.data[DATA_APPLE_TV]: return details = pyatv.AppleTVDevice(name, host, login_id) session = async_get_clientsession(hass) atv = pyatv.connect_to_apple_tv(details, hass.loop, session=session) if credentials: yield from atv.airplay.load_credentials(credentials) power = AppleTVPowerManager(hass, atv, start_off) hass.data[DATA_APPLE_TV][host] = {ATTR_ATV: atv, ATTR_POWER: power} hass.async_add_job( discovery.async_load_platform(hass, 'media_player', DOMAIN, atv_config)) hass.async_add_job( discovery.async_load_platform(hass, 'remote', DOMAIN, atv_config))
def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Setup the Apple TV platform.""" import pyatv if discovery_info is not None: name = discovery_info['name'] host = discovery_info['host'] login_id = discovery_info['hsgid'] 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) 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) yield from async_add_entities([entity], update_before_add=True)
def __init__(self, sh, *args, **kwargs): """ Initalizes the plugin. The parameters describe for this method are pulled from the entry in plugin.yaml. """ self.logger = logging.getLogger(__name__) # get the parameters for the plugin (as defined in metadata plugin.yaml): self._name = 'Unknown' self._device_id = None self._ip = self.get_parameter_value('ip') self._login_id = self.get_parameter_value('login_id') self._atv_scan_timeout = 5 self._atv_reconnect_timeout = 10 self._atv_device = pyatv.AppleTVDevice(self._name, self._ip, self._login_id) self._atv = None self._items = {} self._loop = asyncio.get_event_loop() self._push_listener_loop = None self._cycle = 5 self._scheduler_running = False self._playstatus = None self._is_playing = False self._position = 0 self._position_timestamp = None self._credentials = None self._credentialsfile = None self._credentials_verified = False self.__push_listener_thread = None self.init_webinterface() return
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Setup the Apple TV platform.""" import pyatv 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) 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) @callback def on_hass_stop(event): """Stop push updates when hass stops.""" atv.push_updater.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop) async_add_devices([entity])
def _handle_command(args, loop): details = pyatv.AppleTVDevice(args.name, args.address, args.hsgid) atv = pyatv.connect_to_apple_tv(details, loop) try: playing_resp = yield from atv.metadata.playing() ctrl = retrieve_commands(atv.remote_control, developer=args.developer) metadata = retrieve_commands(atv.metadata, developer=args.developer) playing = retrieve_commands(playing_resp, developer=args.developer) # Parse input command and argument from user cmd, cmd_args = _extract_command_with_args(args.command) if cmd == 'commands': _print_commands('Remote control', ctrl) _print_commands('Metadata', metadata) _print_commands('Playing commands', playing, newline=False) elif cmd in ctrl: yield from _exec_command(atv.remote_control, cmd, *cmd_args) elif cmd in metadata: yield from _exec_command(atv.metadata, cmd, *cmd_args) elif cmd in playing: yield from _exec_command(playing_resp, cmd, *cmd_args) else: logging.error('Unknown command: %s', args.command) finally: yield from atv.logout()
def print_what_is_playing(loop): print('Discovering devices on network...') try: atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=5) if len(atvs) == 0: raise IndexError print('Connecting to {}'.format(atvs[0].address)) atv = pyatv.connect_to_apple_tv(atvs[0], loop) except IndexError: """ New module for device discovery below """ print('auto discover failed') login_dict = device_search().login_info print(login_dict) details = pyatv.AppleTVDevice(login_dict['NAME'], login_dict['ADDRESS'], login_dict['HSGID']) atv = pyatv.connect_to_apple_tv(details, loop) try: playing = yield from atv.metadata.playing() print('Currently playing:') print(playing) # yield from atv.remote_control.play() # yield atv.remote_control.select() finally: # Do not forget to logout yield from atv.logout()
def _handle_commands(args, loop): details = pyatv.AppleTVDevice(args.name, args.address, args.login_id) atv = pyatv.connect_to_apple_tv(details, loop) atv.push_updater.listener = PushListener() try: for cmd in args.command: ret = yield from _handle_command(args, cmd, atv, loop) if ret != 0: return ret finally: yield from atv.logout() return 0
def _handle_command(args, loop): details = pyatv.AppleTVDevice(args.name, args.address, args.login_id) atv = pyatv.connect_to_apple_tv(details, loop) atv.push_updater.listener = PushListener() try: playing_resp = yield from atv.metadata.playing() ctrl = retrieve_commands(atv.remote_control, developer=args.developer) metadata = retrieve_commands(atv.metadata, developer=args.developer) playing = retrieve_commands(playing_resp, developer=args.developer) other = {'push_updates': 'Listen for push updates.'} # Parse input command and argument from user cmd, cmd_args = _extract_command_with_args(args.command) if cmd == 'commands': _print_commands('Remote control', ctrl) _print_commands('Metadata', metadata) _print_commands('Playing', playing) _print_commands('Other', other, newline=False) elif cmd == 'artwork': artwork = yield from atv.metadata.artwork() if artwork is not None: with open('artwork.png', 'wb') as file: file.write(artwork) else: print('No artwork is currently available.') elif cmd == 'push_updates': print('Press ENTER to stop') atv.push_updater.start() yield from loop.run_in_executor(None, sys.stdin.readline) atv.push_updater.stop() elif cmd in ctrl: yield from _exec_command(atv.remote_control, cmd, *cmd_args) elif cmd in metadata: yield from _exec_command(atv.metadata, cmd, *cmd_args) elif cmd in playing: yield from _exec_command(playing_resp, cmd, *cmd_args) else: logging.error('Unknown command: %s', args.command) finally: yield from atv.logout()
def _handle_commands(args, loop): #print('_handle_commands: name={0}, address={1}, login_id={2}'.format(args.name, args.address, args.login_id)) details = pyatv.AppleTVDevice(args.name, args.address, args.login_id) atv = pyatv.connect_to_apple_tv(details, loop) atv.push_updater.listener = PushListener() try: if args.airplay_credentials is not None: yield from atv.airplay.load_credentials(args.airplay_credentials) for cmd in args.command: print('process cmd "{0}"'.format(str(cmd))) if cmd is None: break ret = yield from _handle_device_command(args, cmd, atv, loop) if ret != 0: return ret finally: yield from atv.logout() return 0
def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Setup the Apple TV platform.""" import pyatv if discovery_info is not None: name = discovery_info['name'] host = discovery_info['host'] login_id = discovery_info['hsgid'] else: name = config.get(CONF_NAME) host = config.get(CONF_HOST) login_id = config.get(CONF_LOGIN_ID) 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) @asyncio.coroutine def async_stop_subscription(event): """Logout device to close its session.""" _LOGGER.info("Closing Apple TV session") yield from atv.logout() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_subscription) yield from async_add_entities([entity], update_before_add=True)
"""Simple example that shows how to manually connect to an Apple TV.""" import pyatv import asyncio # Enter details used to connect NAME = 'My Apple TV' ADDRESS = '10.0.10.22' HSGID = '00000000-1111-2222-3333-444444444444' DETAILS = pyatv.AppleTVDevice(NAME, ADDRESS, HSGID) # Method that is dispatched by the asyncio event loop @asyncio.coroutine def print_what_is_playing(loop, details): print('Connecting to {}'.format(details.address)) atv = pyatv.connect_to_apple_tv(details, loop) try: playing = yield from atv.metadata.playing() print('Currently playing:') print(playing) except: # Do not forget to logout yield from atv.logout() # Setup event loop and connect loop = asyncio.get_event_loop() loop.run_until_complete(print_what_is_playing(loop, DETAILS))
from plexapi.server import PlexServer import asyncio import plexapi.exceptions import pyatv import pyatv.interface import time import requests.exceptions import config import kodi_rpc import os plex = PlexServer('http://{}:{}'.format(config.plex.host, config.plex.port), config.plex.onlineToken) apple_tv = pyatv.AppleTVDevice(config.apple_tv.name, config.apple_tv.host, config.apple_tv.loginId) def _get_poneys(): print('Looking for poneys in PLEX library') tv_shows = plex.library.section('TV Shows') mlp = tv_shows.get('My Little Pony: Friendship is Magic') for season in mlp: if season.isWatched: continue for episode in season: if episode.isWatched: continue print('S{}E{} - {}'.format(season.index, episode.index, episode.title)) return episode