-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpair_and_save.py
61 lines (51 loc) · 1.56 KB
/
pair_and_save.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
55
56
57
58
59
60
61
import os
import re
import sys
import json
import time
import pyatv
import random
import asyncio
import inquirer
from yaspin import yaspin
pair = pyatv.pair
Protocol = pyatv.const.Protocol
output_filename = "appletv.json"
async def scan(loop):
with yaspin(text="Scanning") as sp:
atvs = await pyatv.scan(loop)
#print ("Scanning...", end="", flush=True)
#print ("done", flush=True)
print ("Select the number for the ATV to pair with:\n")
ar = {}
names = {}
choices = []
for atv in atvs:
name = f"{atv.name} ({atv.address})"
ar[name] = atv
choices.append(name)
questions = [ inquirer.List("atv", message="Select a device to pair with", choices=choices)]
answers = inquirer.prompt(questions)
name = answers['atv']
atv = ar[name]
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"))
print (f"Saved credentials to {output_filename}")
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(scan(loop))
loop.close()