Skip to content

Commit

Permalink
Merge 641658f into fda8609
Browse files Browse the repository at this point in the history
  • Loading branch information
postlund committed Oct 31, 2019
2 parents fda8609 + 641658f commit ce017c6
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 226 deletions.
42 changes: 22 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ Requirements
- cryptography >= 1.8.1
- curve25519-donna >= 1.3
- ed25519 >= 1.4
- getmac >= 0.8
- netifaces >= 0.10.0
- protobuf >= 3.4.0
- srptools >= 0.2.0
Expand Down Expand Up @@ -116,37 +115,40 @@ It is possible to use the reference CLI application as well:
$ atvremote scan
Scan Results
========================================
Name: Living Room
Address: 10.0.0.10
Id: aabbccddeeff
Name: Apple TV
Address: 10.0.0.10
Identifiers:
- 00:11:22:33:44:55
- 2DF2E7507639C269
- AABBCCDDEEFFAABB
Services:
- Protocol: AirPlay, Port: 7000
- Protocol: MRP, Port: 49152, Credentials: None
Name: Bed Room
Address: 10.0.0.11
Id: ffeeddccbbaa
- Protocol: AirPlay, Port: 7000
- Protocol: DMAP, Port: 3689, Credentials: 00000000-1234-5678-9012-345678901234
Name: Vardagsrum
Address: 10.0.0.10
Identifiers:
- 01234568-9ABC-DEF0-1234-56789ABCDEF0
- 55:44:33:22:11:00
Services:
- Protocol: DMAP, Port: 3689, Credentials: 00000000-1234-5678-9012-345678901234
- Protocol: MRP, Port: 49152, Credentials: None
- Protocol: AirPlay, Port: 7000
# Automatically discover device
$ atvremote -a play
$ atvremote -a next
# Call commands on specific devices
$ atvremote -i 00:11:22:33:44:55 play
$ atvremote -i 55:44:33:22:11:00 next
# Manually specify device
$ atvremote --id ffeeddccbbaa --address 10.0.10.11 --device_credentials 00000000-1234-5678-9012-345678901234 playing
$ atvremote --id ffeeddccbbaa --address 10.0.10.11 --port 3689 --protocol dmap --device_credentials 00000000-1234-5678-9012-345678901234 playing
Media type: Music
Play state: Playing
Position: 0/397s (0.0%)
# Automatically find a specific device based on device id
$ atvremote --id aabbccddeeff -a playing
# Passing multiple commands
$ atvremote -a next next play playing stop
$ atvremote -i 00:11:22:33:44:55 next next play playing stop
# List all commands supported by a device
$ atvremote -a commands
$ atvremote commands
Remote control commands:
- down - Press key down
- left - Press key left
Expand Down
4 changes: 2 additions & 2 deletions examples/autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
async def print_what_is_playing(loop):
"""Find a device and print what is playing."""
print('Discovering devices on network...')
atvs = await pyatv.scan_for_apple_tvs(loop, timeout=5)
atvs = await pyatv.scan(loop, timeout=5)

if not atvs:
print('no device found', file=sys.stderr)
return

print('Connecting to {0}'.format(atvs[0].address))
atv = await pyatv.connect_to_apple_tv(atvs[0], loop)
atv = await pyatv.connect(atvs[0], loop)

try:
playing = await atv.metadata.playing()
Expand Down
2 changes: 1 addition & 1 deletion examples/manual_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def print_what_is_playing(loop):
details.add_service(conf.DmapService(None, HSGID))

print('Connecting to {}'.format(details.address))
atv = await pyatv.connect_to_apple_tv(details, loop)
atv = await pyatv.connect(details, loop)

try:
print((await atv.metadata.playing()))
Expand Down
4 changes: 2 additions & 2 deletions examples/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ async def pair_with_device(loop):
"""Make it possible to pair with device."""
my_zeroconf = Zeroconf(loop)

atvs = await pyatv.scan_for_apple_tvs(loop, timeout=5)
atvs = await pyatv.scan(loop, timeout=5)

if not atvs:
print('no device found', file=sys.stderr)
return

atv = await pyatv.connect_to_apple_tv(atvs[0], loop)
atv = await pyatv.connect(atvs[0], loop)

atv.pairing.pin(PIN_CODE)
await atv.pairing.start(zeroconf=my_zeroconf, name=REMOTE_NAME)
Expand Down
24 changes: 3 additions & 21 deletions pyatv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ def _handle_service(self, address, name, service):
atv.add_service(service)


async def scan_for_apple_tvs(loop, timeout=5,
identifier=None, only_usable=True,
protocol=None):
async def scan(loop, timeout=5, identifier=None, protocol=None):
"""Scan for Apple TVs using zeroconf (bonjour) and returns them."""
listener = _ServiceListener(loop)
zeroconf = Zeroconf(loop)
Expand All @@ -123,9 +121,6 @@ async def scan_for_apple_tvs(loop, timeout=5,
await zeroconf.close()

def _should_include(atv):
if only_usable and not atv.is_usable():
return False

if identifier and identifier not in atv.all_identifiers:
return False

Expand All @@ -138,12 +133,12 @@ def _should_include(atv):
return [x for x in found_devices if _should_include(x)]


async def connect_to_apple_tv(details, loop, protocol=None, session=None):
async def connect(details, loop, protocol=None, session=None):
"""Connect and logins to an Apple TV."""
if details.identifier is None:
raise exceptions.DeviceIdMissingError("no device identifier")

service = _get_service_used_to_connect(details, protocol)
service = details.main_service(protocol=protocol)

# If no session is given, create a default one
if session is None:
Expand All @@ -159,19 +154,6 @@ async def connect_to_apple_tv(details, loop, protocol=None, session=None):
return MrpAppleTV(loop, session, details, airplay)


def _get_service_used_to_connect(details, protocol):
if not protocol:
service = details.usable_service()
else:
service = details.get_service(protocol)

if not service:
raise exceptions.NoUsableServiceError(
'no usable service to connect to')

return service


def _setup_airplay(loop, session, details):
airplay_service = details.airplay_service()
airplay_player = player.AirPlayPlayer(
Expand Down
32 changes: 14 additions & 18 deletions pyatv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ async def help(self):

async def scan(self):
"""Scan for Apple TVs on the network."""
atvs = await pyatv.scan_for_apple_tvs(
self.loop, timeout=self.args.scan_timeout, only_usable=False)
atvs = await pyatv.scan(self.loop, timeout=self.args.scan_timeout)
_print_found_apple_tvs(atvs)

return 0
Expand Down Expand Up @@ -312,12 +311,13 @@ async def cli_handler(loop):
return 1

return await _handle_commands(args, loop)
if args.address:
return await _handle_commands(args, loop)

logging.error('To autodiscover an Apple TV, add -a')
if args.port == 0 or args.address is None or args.protocol is None:
logging.error(
'You must specify address, port and protocol in manual mode')
return 1

return 1
return await _handle_commands(args, loop)


def _print_found_apple_tvs(atvs, outstream=sys.stdout):
Expand All @@ -328,32 +328,29 @@ def _print_found_apple_tvs(atvs, outstream=sys.stdout):


async def _autodiscover_device(args, loop):
atvs = await pyatv.scan_for_apple_tvs(
atvs = await pyatv.scan(
loop, timeout=args.scan_timeout,
identifier=args.id, protocol=args.protocol, only_usable=True)
identifier=args.id, protocol=args.protocol)
if not atvs:
logging.error('Could not find any Apple TV on current network')
return None

if len(atvs) > 1:
logging.error('Found more than one Apple TV; '
'specify one using --address and --device-credentials')
logging.error(
'Found more than one Apple TV; specify one using --id')
_print_found_apple_tvs(atvs, outstream=sys.stderr)
return None

apple_tv = atvs[0]
service = apple_tv.usable_service() # scan only returns usable service
service = apple_tv.main_service()

# Common parameters for all protocols
args.id = apple_tv.identifier
args.address = apple_tv.address
args.name = apple_tv.name
args.protocol = service.protocol
args.port = service.port

# Protocol specific parameters (overrides cli arguments)
if service.protocol == const.PROTOCOL_DMAP:
args.device_credentials = service.device_credentials
args.device_credentials = service.credentials

logging.info('Auto-discovered %s at %s', args.name, args.address)

Expand Down Expand Up @@ -393,10 +390,9 @@ async def _handle_commands(args, loop):
args.id, args.device_credentials, port=args.port))
elif args.protocol == const.PROTOCOL_MRP:
details.add_service(MrpService(
args.id, args.port, device_credentials=args.device_credentials))
args.id, args.port, credentials=args.device_credentials))

atv = await pyatv.connect_to_apple_tv(
details, loop, protocol=args.protocol)
atv = await pyatv.connect(details, loop, protocol=args.protocol)
atv.push_updater.listener = PushListener()

try:
Expand Down

0 comments on commit ce017c6

Please sign in to comment.