-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathps_simple.py
54 lines (48 loc) · 1.33 KB
/
ps_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import re
import sys
import json
import time
import pyatv
import random
import asyncio
pair = pyatv.pair
Protocol = pyatv.const.Protocol
output_filename = "appletv.json"
loop = asyncio.get_event_loop()
async def scan():
print ("Scanning...", end="", flush=True)
atvs = await pyatv.scan(loop)
print ("done", flush=True)
print ("Select the number for the ATV to pair with:\n")
ar = {}
names = {}
for i, atv in enumerate(atvs):
n = i+1
ar[str(n)] = atv
name = f"{atv.name} ({atv.address})"
names[str(n)] = name
print(f"{n}) {name}")
ans = "-1"
while ans not in ar.keys():
ans = input("> ")
atv = ar[ans]
name = names[ans]
print ("pairing atv %s" % (atv))
pairing = await pair(atv, Protocol.AirPlay, loop)
await pairing.begin()
print ("Enter pairing code on screen")
code = input ("Pairing code: ")
pairing.pin(code)
await pairing.finish()
if pairing.has_paired:
print("Paired with device!")
print("Credentials:", pairing.service.credentials)
else:
print("Did not pair with device!")
creds = pairing.service.credentials
id = atv.identifier
nj = {"credentials": creds, "identifier": id, "name": name}
json.dump(nj, open(output_filename, "w"))
loop.run_until_complete(scan())
loop.close()