-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroon_controller.py
More file actions
387 lines (330 loc) · 13.5 KB
/
roon_controller.py
File metadata and controls
387 lines (330 loc) · 13.5 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""NFC Roon Controller - Roon API Integration"""
from roonapi import RoonApi, RoonDiscovery
from contextlib import contextmanager
import threading
import time
from config import APP_INFO, SETTINGS
from utils import load_token, save_token
_lock = threading.Lock()
class RoonController:
"""Controller for Roon API interactions"""
def __init__(self):
self.api = None
self._zone_cache = {}
self._reconnect_thread = None
self._should_run = True
self._last_activity = time.time()
def connect(self) -> bool:
"""Connect to Roon server"""
try:
servers = RoonDiscovery(None).all()
if not servers:
print("No Roon server found")
return False
token = load_token()
self.api = RoonApi(APP_INFO, token, *servers[0])
if self.api.token != token:
save_token(self.api.token)
print("Token saved")
print(f"Roon connected: {servers[0][0]}:{servers[0][1]}")
self._zone_cache.clear()
self._last_activity = time.time()
# Start watchdog thread
self._start_watchdog()
return True
except Exception as e:
print(f"Roon connection error: {e}")
return False
def _start_watchdog(self):
"""Start connection monitoring thread"""
if self._reconnect_thread and self._reconnect_thread.is_alive():
return
self._should_run = True
self._reconnect_thread = threading.Thread(target=self._watchdog_loop, daemon=True)
self._reconnect_thread.start()
print("Watchdog started")
def _watchdog_loop(self):
"""Monitor connection every 30 seconds"""
while self._should_run:
time.sleep(30)
try:
if not self._is_connected():
print("Roon connection lost, reconnecting...")
self._reconnect()
except Exception as e:
print(f"Watchdog error: {e}")
def _is_connected(self) -> bool:
"""Check if Roon connection is active"""
if not self.api:
return False
try:
_ = self.api.zones
return True
except:
return False
def _reconnect(self):
"""Attempt to reconnect"""
for attempt in range(3):
print(f"Reconnection attempt {attempt + 1}/3...")
try:
if self.connect():
print("Reconnection successful")
return True
except Exception as e:
print(f"Failed: {e}")
time.sleep(5)
print("Reconnection failed after 3 attempts")
return False
def _ensure_connected(self) -> bool:
"""Ensure connection is active before operation"""
if self._is_connected():
self._last_activity = time.time()
return True
return self._reconnect()
def _get_zone_id(self, ref=None) -> str | None:
"""Resolve zone_id from name or ID"""
if not self._ensure_connected():
return None
# Direct ID
if ref and ref in self.api.zones:
return ref
# Search by name (with cache)
if ref:
if ref in self._zone_cache:
cached = self._zone_cache[ref]
if cached in self.api.zones:
return cached
del self._zone_cache[ref]
for zid, z in self.api.zones.items():
if z.get("display_name") == ref:
self._zone_cache[ref] = zid
return zid
# Default zone from settings
default_zone = SETTINGS.get("default_zone", "")
if default_zone:
for zid, z in self.api.zones.items():
if z.get("display_name") == default_zone:
return zid
# First available
return next(iter(self.api.zones), None)
def get_zone_name(self, zid: str) -> str | None:
"""Get zone name from ID"""
try:
if self._ensure_connected() and zid and zid in self.api.zones:
return self.api.zones[zid].get("display_name")
except:
pass
return None
def get_zones(self) -> list:
"""List all zones"""
if not self._ensure_connected():
return []
try:
return [{"zone_id": z, "name": d.get("display_name", "?"), "state": d.get("state", "?")}
for z, d in self.api.zones.items()]
except Exception as e:
print(f"Error getting zones: {e}")
return []
# === Playback ===
def play_content(self, content_type: str, data: dict, zone_id=None) -> bool:
"""Main entry point for playback"""
if not self._ensure_connected():
print("Roon not connected")
return False
zid = self._get_zone_id(zone_id)
if not zid:
print("Zone not found")
return False
print(f"Zone: {self.get_zone_name(zid)}")
try:
handlers = {
"album": lambda: self._play_album(data.get("title"), data.get("artist"), zid),
"genre": lambda: self._play_genre(data.get("genre"), data.get("subgenre"), zid),
"playlist": lambda: self._play_playlist(data.get("playlist"), zid),
}
return handlers.get(content_type, lambda: False)()
except Exception as e:
print(f"Playback error: {e}")
return False
def _play_album(self, title, artist, zid) -> bool:
"""Play album with fallback paths"""
paths = [["Library", "Albums", title], ["Library", "Artists", artist, title]]
for path in paths:
try:
self.api.play_media(zid, path)
print(f"Playing: {title}")
return True
except:
continue
print(f"Failed to play: {artist} - {title}")
return False
def _play_genre(self, genre, subgenre, zid) -> bool:
"""Play genre"""
try:
path = ["Genres", genre] + ([subgenre] if subgenre else [])
self.api.play_media(zid, path)
print(f"Playing: {' > '.join(path[1:])}")
return True
except Exception as e:
print(f"Genre playback failed: {e}")
return False
def _play_playlist(self, playlist, zid) -> bool:
"""Play playlist"""
try:
result = self.api.play_media(zid, ["Playlists", playlist])
if result is False or (isinstance(result, dict) and result.get("action") == "message"):
print(f"Smart playlist not supported: {playlist}")
return False
print(f"Playing: {playlist}")
return True
except:
print(f"Smart playlist not supported: {playlist}")
return False
# === Controls ===
def control_playback(self, action: str, value=None, zone_id=None) -> bool:
"""Control playback (pause/volume)"""
if not self._ensure_connected():
print("Roon not connected")
return False
zid = self._get_zone_id(zone_id)
if not zid:
print("Zone not found")
return False
try:
zone = self.api.zones.get(zid)
print(f"Zone: {zone.get('display_name', zid)}")
if action == "pause":
self.api.playback_control(zid, "playpause")
print("Pause/Play OK")
return True
if action == "volume":
outputs = zone.get("outputs", [])
if not outputs:
print("No output found")
return False
output_id = outputs[0].get("output_id")
vol = int(value) if value is not None else 50
print(f"Volume: output={output_id}, value={vol}")
self.api.set_volume_percent(output_id, vol)
print(f"Volume set: {vol}")
return True
if action == "shuffle":
# Toggle shuffle mode
settings = zone.get("settings", {})
current_shuffle = settings.get("shuffle", False)
new_shuffle = not current_shuffle
self.api.change_settings(zid, {"shuffle": new_shuffle})
print(f"Shuffle: {current_shuffle} -> {new_shuffle}")
return True
except Exception as e:
print(f"Control error: {e}")
return False
# === Browse with context manager ===
@contextmanager
def _browse(self, target: str):
"""Context manager for browse navigation"""
with _lock:
try:
self.api.browse_browse({"hierarchy": "browse", "pop_all": True})
root = self.api.browse_load({"hierarchy": "browse", "offset": 0, "count": 50})
key = next((i["item_key"] for i in root.get("items", []) if i.get("title") == target), None)
if key:
self.api.browse_browse({"hierarchy": "browse", "item_key": key})
yield self.api.browse_load({"hierarchy": "browse", "offset": 0, "count": 200})
else:
yield None
except Exception as e:
print(f"Browse error: {e}")
yield None
def get_genres(self) -> list:
"""List genres"""
if not self._ensure_connected():
return []
with self._browse("Genres") as items:
if not items:
return []
result = [{"name": i["title"]} for i in items.get("items", []) if i.get("title")]
print(f"{len(result)} genres found")
return result
def get_subgenres(self, genre: str) -> list:
"""List subgenres for a genre"""
if not self._ensure_connected():
return []
with self._browse("Genres") as items:
if not items:
return []
key = next((i["item_key"] for i in items.get("items", []) if i.get("title") == genre), None)
if not key:
return []
self.api.browse_browse({"hierarchy": "browse", "item_key": key})
sub = self.api.browse_load({"hierarchy": "browse", "offset": 0, "count": 200})
result = [{"name": i["title"]} for i in sub.get("items", []) if i.get("title")]
print(f"{len(result)} subgenres for {genre}")
return result
def get_playlists(self) -> list:
"""List playlists"""
if not self._ensure_connected():
return []
with self._browse("Playlists") as items:
if not items:
return []
result = [{"name": i["title"]} for i in items.get("items", []) if i.get("title")]
print(f"{len(result)} playlists found")
return result
def search(self, query: str) -> list:
"""Search albums"""
if not self._ensure_connected() or len(query) < 2:
return []
with _lock:
try:
self.api.browse_browse({"hierarchy": "search", "input": query, "pop_all": True})
cats = self.api.browse_load({"hierarchy": "search", "offset": 0, "set_display_offset": 0})
key = next((i["item_key"] for i in cats.get("items", []) if i.get("title") == "Albums"), None)
if not key:
return []
self.api.browse_browse({"hierarchy": "search", "item_key": key})
albums = self.api.browse_load({"hierarchy": "search", "offset": 0, "set_display_offset": 0, "count": 20})
results = [
{"title": i.get("title", ""), "subtitle": i.get("subtitle", ""),
"hint": i.get("hint", ""), "image_key": i.get("image_key", "")}
for i in albums.get("items", [])[:15]
if not any(s in i.get("hint", "").lower() for s in ["qobuz", "tidal", "streaming"])
]
print(f"Search '{query}': {len(results)} results")
return results
except Exception as e:
print(f"Search error: {e}")
return []
def get_image_url(self, key: str) -> str | None:
"""Get image URL from key"""
if not self._ensure_connected() or not key:
return None
try:
return self.api.get_image(key)
except:
return None
def get_now_playing(self, zone_id=None) -> dict | None:
"""Get current track information"""
if not self._ensure_connected():
return None
try:
zid = self._get_zone_id(zone_id)
if not zid or zid not in self.api.zones:
return None
zone = self.api.zones[zid]
now = zone.get("now_playing")
if not now:
return None
return {
"title": now.get("three_line", {}).get("line1", ""),
"artist": now.get("three_line", {}).get("line2", ""),
"album": now.get("three_line", {}).get("line3", ""),
"image_key": now.get("image_key", ""),
"length": now.get("length", 0),
"seek_position": now.get("seek_position"),
"state": zone.get("state", "stopped"),
"zone_name": zone.get("display_name", "")
}
except Exception as e:
print(f"Error getting now playing: {e}")
return None