def scan(self): """Scan for Apple TVs on the network.""" atvs = yield from pyatv.scan_for_apple_tvs( self.loop, timeout=self.args.scan_timeout, only_usable=False) _print_found_apple_tvs(atvs) return 0
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_scan(args, loop): atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=args.scan_timeout, only_home_sharing=False) _print_found_apple_tvs(atvs) return 0
def test_scan_abort_on_first_found(self): zeroconf_stub.stub(pyatv, HOMESHARING_SERVICE_1, HOMESHARING_SERVICE_2) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0, abort_on_found=True) self.assertEqual(len(atvs), 1) self.assertEqual(atvs[0].name, 'Apple TV 1')
def test_scan_home_sharing_overrules(self): zeroconf_stub.stub(pyatv, DEVICE_SERVICE_1, HOMESHARING_SERVICE_3) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0) self.assertEqual(len(atvs), 1) self.assertEqual(atvs[0].name, 'Apple TV 3') self.assertEqual(atvs[0].address, ipaddress.ip_address('10.0.0.3')) self.assertEqual(atvs[0].login_id, 'cccc') self.assertEqual(atvs[0].port, 3689)
def test_scan_for_particular_device(self): zeroconf_stub.stub(pyatv, HOMESHARING_SERVICE_1, HOMESHARING_SERVICE_2) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0, only_usable=False, device_ip='10.0.0.2') self.assertEqual(len(atvs), 1) self.assertEqual(atvs[0].name, 'Apple TV 2') self.assertEqual(atvs[0].address, ipaddress.ip_address('10.0.0.2'))
def test_scan_all_devices(self): zeroconf_stub.stub(pyatv, DEVICE_SERVICE_1) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0, only_home_sharing=False) self.assertEqual(len(atvs), 1) self.assertEqual(atvs[0].name, 'Apple TV 3') self.assertEqual(atvs[0].address, ipaddress.ip_address('10.0.0.3')) self.assertEqual(atvs[0].login_id, None) self.assertEqual(atvs[0].port, 3689)
def test_scan_mrp(self): zeroconf_stub.stub(pyatv, MRP_SERVICE_1, MRP_SERVICE_2) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, only_usable=False, timeout=0) self.assertEqual(len(atvs), 2) dev1 = AppleTV(ipaddress.ip_address('10.0.0.4'), 'Apple TV 4') self.assertIn(dev1, atvs) dev2 = AppleTV(ipaddress.ip_address('10.0.0.5'), 'Apple TV 5') self.assertIn(dev2, atvs)
def _handle(loop): atvs = yield from pyatv.scan_for_apple_tvs( loop, timeout=timeout, abort_on_found=True) # Take the first device found if atvs: atv = pyatv.connect_to_apple_tv(atvs[0], loop) try: yield from handler(atv) finally: yield from atv.logout() else: if not_found is not None: yield from not_found()
def test_scan_for_apple_tvs(self): zeroconf_stub.stub(pyatv, HOMESHARING_SERVICE_1, HOMESHARING_SERVICE_2) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0) self.assertEqual(len(atvs), 2) # First device dev1 = AppleTVDevice('Apple TV 1', ipaddress.ip_address('10.0.0.1'), 'aaaa') self.assertIn(dev1, atvs) # Second device dev2 = AppleTVDevice('Apple TV 2', ipaddress.ip_address('10.0.0.2'), 'bbbb') self.assertIn(dev2, atvs)
def test_scan_airplay_device(self): zeroconf_stub.stub(pyatv, AIRPLAY_SERVICE_1) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0, only_usable=False) self.assertEqual(len(atvs), 1) self.assertEqual(atvs[0].name, 'Apple TV 6') self.assertEqual(atvs[0].address, ipaddress.ip_address('10.0.0.6')) services = atvs[0].services() self.assertEqual(len(services), 1) service = services[0] self.assertEqual(service.port, 7000)
def test_scan_for_apple_tvs(self): zeroconf_stub.stub(pyatv, HOMESHARING_SERVICE_1, HOMESHARING_SERVICE_2, MRP_SERVICE_1, AIRPLAY_SERVICE_1) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0) self.assertEqual(len(atvs), 3) # First device dev1 = AppleTV(ipaddress.ip_address('10.0.0.1'), 'Apple TV 1') self.assertIn(dev1, atvs) # Second device dev2 = AppleTV(ipaddress.ip_address('10.0.0.2'), 'Apple TV 2') self.assertIn(dev2, atvs) # Third device dev3 = AppleTV(ipaddress.ip_address('10.0.0.4'), 'Apple TV 4') self.assertIn(dev3, atvs)
def print_what_is_playing(loop): print('Discovering devices on network...') atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=5) if len(atvs) == 0: sys.stderr.print('no device found\n') return print('Connecting to {}'.format(atvs[0].address)) atv = pyatv.connect_to_apple_tv(atvs[0], loop) try: playing = yield from atv.metadata.playing() print('Currently playing:') print(playing) finally: # Do not forget to logout yield from atv.logout()
def _handle_autodiscover(args, loop): atvs = yield from pyatv.scan_for_apple_tvs(timeout=args.scan_timeout) if len(atvs) == 0: logging.error('Could not find any Apple TV on current network') return 1 elif len(atvs) > 1: logging.error('Found more than one Apple TV; ' 'specify one using --address and --hsgid') _print_found_apple_tvs(atvs, outstream=sys.stderr) return 1 # Simple hack to re-use existing command handling and respect options apple_tv = atvs[0] args.address = apple_tv.address args.hsgid = apple_tv.hsgid args.name = apple_tv.name logging.info('Auto-discovered %s at %s', args.name, args.address) yield from _handle_command(args, loop) return 0
def print_what_is_playing(loop): """Find a device and print what is playing.""" print('Discovering devices on network...') atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=5) if not atvs: print('no device found', file=sys.stderr) return print('Connecting to {0}'.format(atvs[0].address)) atv = pyatv.connect_to_apple_tv(atvs[0], loop) try: playing = yield from atv.metadata.playing() print('Currently playing:') print(playing) finally: # Do not forget to logout yield from atv.logout()
def scan_for_apple_tvs(hass): """Scan for devices and present a notification of the ones found.""" import pyatv atvs = yield from pyatv.scan_for_apple_tvs(hass.loop, timeout=3) devices = [] for atv in atvs: login_id = atv.login_id if login_id is None: login_id = 'Home Sharing disabled' devices.append('Name: {0}<br />Host: {1}<br />Login ID: {2}'.format( atv.name, atv.address, login_id)) if not devices: devices = ['No device(s) found'] hass.components.persistent_notification.async_create( 'The following devices were found:<br /><br />' + '<br /><br />'.join(devices), title=NOTIFICATION_SCAN_TITLE, notification_id=NOTIFICATION_SCAN_ID)
def _autodiscover_device(args, loop): atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=args.scan_timeout, abort_on_found=True, device_ip=args.address, protocol=args.protocol, only_usable=True) if not atvs: logging.error('Could not find any Apple TV on current network') return None elif len(atvs) > 1: logging.error('Found more than one Apple TV; ' 'specify one using --address and --login_id') _print_found_apple_tvs(atvs, outstream=sys.stderr) return None apple_tv = atvs[0] # Extract the preferred service here instead of a usable service. The # reason for this is that some services are never usable after a scan, # like MRP, but might become usable after the user has filled in # additional information (which will be done here). service = apple_tv.preferred_service() if not service: print("fel fel fel") return None # Common parameters for all protocols args.address = apple_tv.address args.name = apple_tv.name args.protocol = service.protocol args.port = service.port # Protocol specific parameters if service.protocol == const.PROTOCOL_DMAP: args.login_id = service.login_id logging.info('Auto-discovered %s at %s', args.name, args.address) return apple_tv
def test_scan_no_device_found(self): zeroconf_stub.stub(pyatv) atvs = yield from pyatv.scan_for_apple_tvs(self.loop, timeout=0) self.assertEqual(len(atvs), 0)
def _handle_scan(args, loop): atvs = yield from pyatv.scan_for_apple_tvs(loop, timeout=args.scan_timeout) _print_found_apple_tvs(atvs)