示例#1
0
    def test_auto_connect_with_device(self, connect_func, scan_func):
        scan_func.return_value = [self.config]
        connect_func.return_value = self.mock_device

        self.found_device = None

        async def found_handler(atv):
            self.found_device = atv

        helpers.auto_connect(found_handler)

        self.assertEqual(self.found_device, self.mock_device)
        self.assertTrue(self.mock_device.logout.called)
示例#2
0
    def test_auto_connect_with_no_device(self, scan_func):
        self.device_found = True

        async def found_handler():
            self.assertTrue(False, msg='should not be called')

        async def not_found_handler():
            self.device_found = False

        helpers.auto_connect(found_handler,
                             not_found=not_found_handler)

        self.assertFalse(self.device_found)
示例#3
0
    def test_auto_connect_with_device(self, connect_func, scan_func):
        scan_func.return_value = [self.device_details]
        connect_func.return_value = self.mock_device

        self.found_device = None

        @asyncio.coroutine
        def found_handler(atv):
            self.found_device = atv

        helpers.auto_connect(found_handler, event_loop=self.loop)

        self.assertEqual(self.found_device, self.mock_device)
        self.assertTrue(self.mock_device.logout.called)
示例#4
0
    def test_auto_connect_with_no_device(self, scan_func):
        self.device_found = True

        @asyncio.coroutine
        def found_handler():
            self.assertTrue(False, msg='should not be called')

        @asyncio.coroutine
        def not_found_handler():
            self.device_found = False

        helpers.auto_connect(found_handler,
                             not_found=not_found_handler,
                             event_loop=self.loop)

        self.assertFalse(self.device_found)
示例#5
0
"""Simple example that connects to a device with autodiscover."""

import asyncio
from pyatv import helpers


# Method that is dispatched by the asyncio event loop
@asyncio.coroutine
def print_what_is_playing(atv):
    playing = yield from atv.metadata.playing()
    print('Currently playing:')
    print(playing)


# logout is automatically performed by auto_connect
helpers.auto_connect(print_what_is_playing)
示例#6
0
"""Example for device authentication."""

import sys
import asyncio
from pyatv import (exceptions, helpers)


@asyncio.coroutine
def authenticate_with_device(atv):
    """Perform device authentication and print credentials."""
    credentials = yield from atv.airplay.generate_credentials()
    yield from atv.airplay.load_credentials(credentials)

    try:
        yield from atv.airplay.start_authentication()
        pin = input('PIN Code: ')
        yield from atv.airplay.finish_authentication(pin)
        print('Credentials: {0}'.format(credentials))

    except exceptions.DeviceAuthenticationError:
        print('Failed to authenticate', file=sys.stderr)


helpers.auto_connect(authenticate_with_device)
示例#7
0
                                     json=payload) as resp:
            # This just prints the response - do something useful here
            print('Response: ', await resp.text())

    def playstatus_update(self, _, playstatus):
        asyncio.ensure_future(self._post(playstatus))

    def playstatus_error(self, updater, exception):
        print('An error occurred (restarting): {0}'.format(exception))
        updater.start(initial_delay=1)


async def _listen_for_push_updates(atv):
    print('Starting to listen for updates')
    try:
        with aiohttp.ClientSession() as session:
            atv.push_updater.listener = PushListener(session)
            await atv.push_updater.start()
    except Exception as ex:
        print('Got an error: ', ex)
    finally:
        await atv.logout()


async def _no_device_found():
    print('No Apple TV found', file=sys.stderr)


if __name__ == '__main__':
    helpers.auto_connect(_listen_for_push_updates, not_found=_no_device_found)