-
Notifications
You must be signed in to change notification settings - Fork 11
/
fsapi.py
212 lines (164 loc) · 5.93 KB
/
fsapi.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
#/usr/bin/env python
import requests
from lxml import objectify
class FSAPI(object):
RW_MODES = {
0: 'internet',
1: 'spotify',
2: 'music',
3: 'dab',
4: 'fm',
5: 'aux',
}
RO_MODES = {
6: 'dmr',
7: 'network',
}
PLAY_STATES = {
0: 'stopped',
1: 'unknown',
2: 'playing',
3: 'paused',
}
EQS = {
0: 'custom',
1: 'normal',
2: 'flat',
3: 'jazz',
4: 'rock',
5: 'movie',
6: 'classic',
7: 'pop',
8: 'news',
}
def __init__(self, fsapi_device_url, pin):
self.pin = pin
self.sid = None
self.webfsapi = None
self.fsapi_device_url = fsapi_device_url
self.webfsapi = self.get_fsapi_endpoint()
self.sid = self.create_session()
def get_fsapi_endpoint(self):
r = requests.get(self.fsapi_device_url)
doc = objectify.fromstring(r.content)
return doc.webfsapi.text
def create_session(self):
doc = self.call('CREATE_SESSION')
return doc.sessionId.text
def call(self, path, extra=None):
if not self.webfsapi:
raise Exception('No server found')
if type(extra) is not dict:
extra = dict()
params = dict(
pin=self.pin,
sid=self.sid,
)
params.update(**extra)
r = requests.get('%s/%s' % (self.webfsapi, path), params=params)
return objectify.fromstring(r.content)
def __del__(self):
self.call('DELETE_SESSION')
# Read-only ###################################################################################
@property
def version(self):
doc = self.call('GET/netRemote.sys.info.version')
return doc.value.c8_array.text
@property
def play_status(self):
doc = self.call('GET/netRemote.play.status')
return self.PLAY_STATES.get(doc.value.u8)
@property
def play_info_name(self):
doc = self.call('GET/netRemote.play.info.name')
return doc.value.c8_array.text or ''
@property
def play_info_text(self):
doc = self.call('GET/netRemote.play.info.text')
return doc.value.c8_array.text or ''
@property
def eq_bands(self):
doc = self.call('LIST_GET_NEXT/netRemote.sys.caps.eqBands/-1', dict(
maxItems=100,
))
if not doc.status == 'FS_OK':
return None
ret = list()
for index, item in enumerate(list(doc.iterchildren('item'))):
temp = dict(band=index)
for field in list(item.iterchildren()):
temp[field.get('name')] = list(field.iterchildren()).pop()
ret.append(temp)
return ret
@property
def notifications(self):
doc = self.call('GET_NOTIFIES')
if doc.status != 'FS_OK':
return None
return {
'node': doc.notify.get('node'),
'value': list(doc.notify.value.iterchildren()).pop(),
}
# Read-write ##################################################################################
def get_volume(self):
doc = self.call('GET/netRemote.sys.audio.volume')
return doc.value.u8.text
def set_volume(self, value):
doc = self.call('SET/netRemote.sys.audio.volume', dict(value=value))
return doc.status == 'FS_OK'
volume = property(get_volume, set_volume)
def get_friendly_name(self):
doc = self.call('GET/netRemote.sys.info.friendlyName')
return doc.value.c8_array.text
def set_friendly_name(self, value):
doc = self.call('SET/netRemote.sys.info.friendlyName', dict(value=value))
return doc.status == 'FS_OK'
friendly_name = property(get_friendly_name, set_friendly_name)
def get_mute(self):
doc = self.call('GET/netRemote.sys.audio.mute')
return bool(doc.value.u8)
def set_mute(self, value=False):
if type(value) is not bool:
raise RuntimeError('Mute must be boolean')
doc = self.call('SET/netRemote.sys.audio.mute', dict(value=int(value)))
return doc.status == 'FS_OK'
mute = property(get_mute, set_mute)
def get_power(self):
doc = self.call('GET/netRemote.sys.power')
return bool(doc.value.u8)
def set_power(self, value=False):
if type(value) is not bool:
raise RuntimeError('Mute must be boolean, not `%s`' % type(value))
doc = self.call('SET/netRemote.sys.power', dict(value=int(value)))
return doc.status == 'FS_OK'
power = property(get_power, set_power)
def get_mode(self):
doc = self.call('GET/netRemote.sys.mode')
modes = self.RW_MODES
modes.update(self.RO_MODES)
return modes.get(doc.value.u32)
def set_mode(self, value):
modes = {v: k for k, v in self.RW_MODES.items()}
if value not in modes:
raise RuntimeError('Not allowed to set mode to `%s`' % value)
doc = self.call('SET/netRemote.sys.mode', dict(value=modes.get(value)))
return doc.status == 'FS_OK'
mode = property(get_mode, set_mode)
def get_eq_preset(self):
doc = self.call('GET/netRemote.sys.audio.eqPreset')
return self.EQS.get(doc.value.u8)
def set_eq_preset(self, value):
eqs = {v: k for k, v in self.EQS.items()}
if value not in eqs:
raise RuntimeError('Not allowed to set EQ to `%s`' % value)
doc = self.call('SET/netRemote.sys.audio.eqPreset', dict(value=eqs.get(value)))
return doc.status == 'FS_OK'
eq_preset = property(get_eq_preset, set_eq_preset)
def eq_custom(self, band, value=None):
if type(value) is int:
doc = self.call('SET/netRemote.sys.audio.eqCustom.param%s' % band, dict(
value=value,
))
return doc.status == 'FS_OK'
doc = self.call('GET/netRemote.sys.audio.eqCustom.param%s' % band)
return doc.value.s16