-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
266 lines (230 loc) · 9.52 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Sample API Client."""
from __future__ import annotations
import asyncio
import xml.etree.ElementTree as Et
from collections.abc import Mapping
from urllib.parse import urlencode
from xml.sax.saxutils import escape
from async_upnp_client.aiohttp import AiohttpRequester
from async_upnp_client.const import HttpRequest, HttpResponse
from async_upnp_client.exceptions import UpnpCommunicationError, UpnpConnectionError, UpnpConnectionTimeoutError
from .const import LOGGER, KeyCode
from .exceptions import (
CommunicationException,
CommunicationTimeoutException,
NotPairedException,
PairingTimeoutException,
)
from .notify_server import NotifyServer
from .utils import magneta_hash
PAIRING_EVENT_TIMEOUT = 5
PAIRING_ATTEMPTS = 3
class Client:
"""Sample API Client."""
def __init__(
self,
host: str,
port: int,
user_id: str,
instance_id: str,
notify_server: NotifyServer,
) -> None:
"""Sample API Client."""
self._host = host
self._port = port
self._url = "http://" + self._host + ":" + str(self._port)
self._notify_server = notify_server
self._terminal_id = magneta_hash(instance_id)
self._user_id = magneta_hash(user_id)
self._requester = AiohttpRequester(http_headers={"User-Agent": "Homeassistant MagentaTV Integration"})
self._verification_code = None
self._event_registration_id = None
self._pairing_event = asyncio.Event()
self._event_listeners = []
def subscribe(self, callback):
if callback not in self._event_listeners:
self._event_listeners.append(callback)
async def async_close(self):
if self._event_registration_id:
await self._notify_server.async_unsubscribe(self._event_registration_id)
self._event_registration_id = None
self._pairing_event.clear()
self._verification_code = None
async def _on_event(self, changes):
# is paired:
if self._pairing_event.is_set():
# notify listeners
asyncio.gather(*[listener(changes) for listener in self._event_listeners])
elif "messageBody" in changes:
body = changes.get("messageBody")
if "X-pairingCheck:" in body:
pairing_code = changes.get("messageBody").removeprefix("X-pairingCheck:")
self._verification_code = magneta_hash(pairing_code + self._terminal_id + self._user_id)
self._pairing_event.set()
async def _register_for_events(self):
self._event_registration_id = await self._notify_server._async_subscribe_to_service(
(self._host, self._port),
"X-CTC_RemotePairing",
self._on_event,
)
LOGGER.debug(
"Registered for events on %s. ID: %s",
self._host,
self._event_registration_id,
)
async def async_pair(self) -> str:
attempts = 0
while not self._pairing_event.is_set():
attempts += 1
try:
await self._register_for_events()
await self._async_send_pairing_request()
LOGGER.info("Waiting for Pairing Code")
await asyncio.wait_for(self._pairing_event.wait(), timeout=PAIRING_EVENT_TIMEOUT)
LOGGER.info("Received Pairing Code")
await self._async_verify_pairing()
LOGGER.info("Pairing Verified. Success !")
except UpnpConnectionError as ex:
await self.async_close()
LOGGER.debug("Could not connect", exc_info=ex)
raise CommunicationException("No connection could be made to the receiver") from None
except (asyncio.CancelledError, asyncio.TimeoutError) as ex:
await self.async_close()
# pairing was not successfull, reset the client to start fresh
LOGGER.debug("Pairing Timed out", exc_info=ex)
if attempts > PAIRING_ATTEMPTS:
raise PairingTimeoutException(
f"No pairingCode received from the receiver within {attempts} attempts waiting {PAIRING_EVENT_TIMEOUT} each"
) from None
self.assert_paired()
return self._verification_code
def is_paired(self) -> bool:
return self._verification_code is not None
def assert_paired(self):
if self._verification_code is None:
raise NotPairedException("Client needs to be paired in order to use this function")
async def async_get_player_state(self) -> str:
self.assert_paired()
response = await self._async_send_upnp_soap(
"X-CTC_RemotePairing",
"X-getPlayerState",
{
"pairingDeviceID": self._terminal_id,
"verificationCode": self._verification_code,
},
)
assert response.status_code == 200
tree = Et.fromstring(text=response.body)
result = {}
for child in tree[0][0]:
result[child.tag] = child.text
return result
async def _async_send_pairing_request(self):
response = await self._async_send_upnp_soap(
"X-CTC_RemotePairing",
"X-pairingRequest",
{
"pairingDeviceID": self._terminal_id,
"friendlyName": "Homeassistant Integration",
"userID": self._user_id,
},
)
assert response.status_code == 200
async def _async_verify_pairing(self):
self.assert_paired()
response = await self._async_send_upnp_soap(
"X-CTC_RemotePairing",
"X-pairingCheck",
{
"pairingDeviceID": self._terminal_id,
"verificationCode": self._verification_code,
},
)
assert response.status_code == 200
assert "<pairingResult>0</pairingResult>" in response.body
async def _async_send_upnp_soap(
self, service: str, action: str, attributes: Mapping[str, str]
) -> HttpResponse:
try:
attributes = "".join([f" <{k}>{escape(v)}</{k}>\n" for k, v in attributes.items()])
full_body = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n'
" <s:Body>\n"
f' <u:{action} xmlns:u="urn:schemas-upnp-org:service:{service}:1">\n'
f"{attributes}"
f" </u:{action}>\n"
" </s:Body>\n"
"</s:Envelope>"
)
return await self._requester.async_http_request(
http_request=HttpRequest(
method="POST",
url=f"{self._url}/upnp/service/{service}/Control",
headers={
"SOAPACTION": f"urn:schemas-upnp-org:service:{service}:1#{action}",
"HOST": f"{self._host}:{self._port}",
"Content-Type": 'text/xml; charset="utf-8"',
},
body=full_body,
)
)
except UpnpConnectionTimeoutError as ex:
raise CommunicationTimeoutException() from ex
except UpnpCommunicationError as ex:
raise CommunicationException() from ex
async def async_send_action(self):
self.assert_paired()
# TODO - make it work
params = {
"type": "EVENT_REMOTE_CONTROL",
"action": "functionCall",
"functionType": "startPlay",
"mediaCode": "7216", # ?
"mediaType": "1", # ?
"userID": self._user_id,
"ContentID": "0", # ?
"playByBookmark": "0",
"playByTime": "0",
}
response = await self._async_send_upnp_soap(
"AVTransport",
"SetAVTransportURI",
{
"InstanceID": "0",
"CurrentURI": f"http://iptv?{urlencode(params)}&pairingInfo={self._terminal_id}:{self._verification_code}&platform=IPTV",
"CurrentURIMetaData": "",
},
)
LOGGER.debug("%s: %s", "Set URI", response.body)
response = await self._async_send_upnp_soap(
"AVTransport",
"Play",
{"InstanceID": "0", "Speed": "1"},
)
LOGGER.debug("%s: %s", "Play", response.body)
async def async_send_key(self, key: KeyCode):
self.assert_paired()
response = await self._async_send_upnp_soap(
"X-CTC_RemoteControl",
"X_CTC_RemoteKey",
{
"InstanceID": "0",
"KeyCode": f"keyCode={key.value}^{self._terminal_id}:{self._verification_code}^userID:{self._user_id}",
},
)
LOGGER.info("%s - %s: %s", "RemoteKey", key, response)
assert response.status_code== 200
async def async_send_character_input(self, character_input: str):
self.assert_paired()
assert "^" not in character_input # broken
response = await self._async_send_upnp_soap(
"X-CTC_RemoteControl",
"X_CTC_RemoteKey",
{
"InstanceID": "0",
"KeyCode": f"characterInput={character_input}^{self._terminal_id}:{self._verification_code}^userID:{self._user_id}",
},
)
LOGGER.info("%s - '%s': %s", "Send Character Input", character_input, response.body)
assert response.status_code == 200