This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmedia_remote.py
50 lines (39 loc) · 1.55 KB
/
media_remote.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
import asyncio
import sys
import os
from typing import cast
import pyatv
import pyatv.core.relayer
class MediaRemoteProtocol(pyatv.interface.DeviceListener):
def __init__(self, config) -> None:
super().__init__()
self.config = config
self.atv = None
self.protocol = None
async def connect(self, atv):
loop = asyncio.get_event_loop()
if not os.path.exists('data/pairing.state'):
pairing = await pyatv.pair(atv, pyatv.Protocol.AirPlay, loop)
await pairing.begin()
code = input("Enter code displayed by Apple TV: ")
pairing.pin(code)
await pairing.finish()
if pairing.has_paired:
with open("data/pairing.state", "w") as f:
f.write(pairing.service.credentials)
else:
print("Could not pair", file=sys.stderr)
exit(1)
else:
with open("data/pairing.state", "r") as f:
atv.set_credentials(pyatv.Protocol.AirPlay, f.read())
self.atv = await pyatv.connect(atv, loop)
self.atv.listener = self
self.protocol = cast(pyatv.protocols.mrp_proto.MrpProtocol,
cast(pyatv.core.relayer.Relayer, self.atv.remote_control).main_instance.protocol)
print("ready!")
def connection_lost(self, exception: Exception) -> None:
loop = asyncio.get_event_loop()
asyncio.run_coroutine_threadsafe(pyatv.connect(self.atv, loop), loop)
def connection_closed(self) -> None:
pass