-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpair_request.py
More file actions
85 lines (71 loc) · 2.4 KB
/
Copy pathpair_request.py
File metadata and controls
85 lines (71 loc) · 2.4 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""
Request a Philips JointSpace pairing PIN and store credentials for the grant step.
Usage: python pair_request.py --tv 192.168.178.140
"""
import argparse
import json
import random
import string
from pathlib import Path
import requests
try: # suppress self-signed warnings (Philips TVs)
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
except Exception:
pass
DEFAULT_TV_IP = "192.168.178.140"
PAIR_STATE = Path("pair_state.json")
def rand_device_id(n: int = 16) -> str:
alphabet = string.ascii_letters + string.digits
return "".join(random.SystemRandom().choice(alphabet) for _ in range(n))
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--tv", default=DEFAULT_TV_IP, help="TV IP (default: %(default)s)")
args = ap.parse_args()
base = f"https://{args.tv}:1926"
device_id = rand_device_id()
device = {
"device_name": "tv2wled",
"device_os": "linux",
"app_name": "tv2wled",
"type": "native",
"id": device_id,
"app_id": "1",
}
payload = {"scope": ["read", "write", "control"], "device": device}
print(f"Pair request to {base}/6/pair/request ...")
r = requests.post(f"{base}/6/pair/request", json=payload, timeout=3, verify=False)
print("status:", r.status_code)
try:
body = r.json()
print("body:", body)
except Exception:
body = None
print("text:", r.text[:200])
if r.status_code != 200 or not body:
print("Pair request failed; no state saved.")
return
auth_key = body.get("auth_key")
ts = body.get("timestamp")
timeout = body.get("timeout")
if not auth_key or ts is None:
print("Pair request did not return auth_key/timestamp; no state saved.")
return
state = {
"tv_ip": args.tv,
"base_url": base,
"device_id": device_id,
"auth_key": auth_key,
"timestamp": ts,
"timeout": timeout,
}
PAIR_STATE.write_text(json.dumps(state, indent=2), encoding="utf-8")
print("\nPIN should be on the TV now (valid ~30s).")
print("Saved pairing state to pair_state.json:")
print(f" device_id (Digest user): {device_id}")
print(f" auth_key (Digest pass): {auth_key}")
print(f" timestamp: {ts}")
print("Next: python grant_pair.py --pin <PIN_FROM_TV>")
if __name__ == "__main__":
main()