sonosify 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sonosify/__init__.py +67 -0
- sonosify/_parsing.py +22 -0
- sonosify/cli.py +645 -0
- sonosify/client.py +373 -0
- sonosify/controller.py +85 -0
- sonosify/didl.py +89 -0
- sonosify/discovery.py +192 -0
- sonosify/errors.py +31 -0
- sonosify/events.py +350 -0
- sonosify/models.py +57 -0
- sonosify/soap.py +104 -0
- sonosify/spotify.py +61 -0
- sonosify/topology.py +69 -0
- sonosify-0.1.0.dist-info/METADATA +170 -0
- sonosify-0.1.0.dist-info/RECORD +17 -0
- sonosify-0.1.0.dist-info/WHEEL +4 -0
- sonosify-0.1.0.dist-info/entry_points.txt +2 -0
sonosify/client.py
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
5
|
+
|
|
6
|
+
from sonosify._parsing import int_or_none
|
|
7
|
+
from sonosify.didl import parse_favorites, parse_track_metadata, radio_metadata
|
|
8
|
+
from sonosify.events import DEFAULT_SERVICES, EventService, EventSubscription, TransportState
|
|
9
|
+
from sonosify.models import Favorite, PlaybackState, Speaker, Track
|
|
10
|
+
from sonosify.soap import soap_call
|
|
11
|
+
from sonosify.spotify import parse_track_id, track_metadata
|
|
12
|
+
|
|
13
|
+
DEFAULT_TIMEOUT = 15.0
|
|
14
|
+
|
|
15
|
+
_AV_TRANSPORT = "urn:schemas-upnp-org:service:AVTransport:1"
|
|
16
|
+
_RENDERING_CONTROL = "urn:schemas-upnp-org:service:RenderingControl:1"
|
|
17
|
+
_CONTENT_DIRECTORY = "urn:schemas-upnp-org:service:ContentDirectory:1"
|
|
18
|
+
_DEVICE_PROPERTIES = "urn:schemas-upnp-org:service:DeviceProperties:1"
|
|
19
|
+
_ZONE_GROUP_TOPOLOGY = "urn:schemas-upnp-org:service:ZoneGroupTopology:1"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class TransportInfo(BaseModel):
|
|
23
|
+
"""Typed view of the AVTransport GetTransportInfo response."""
|
|
24
|
+
|
|
25
|
+
model_config = ConfigDict(populate_by_name=True)
|
|
26
|
+
|
|
27
|
+
state: TransportState | None = Field(None, alias="CurrentTransportState")
|
|
28
|
+
status: str = Field("", alias="CurrentTransportStatus")
|
|
29
|
+
speed: str = Field("", alias="CurrentSpeed")
|
|
30
|
+
|
|
31
|
+
@field_validator("state", mode="before")
|
|
32
|
+
@classmethod
|
|
33
|
+
def _coerce_state(cls, value: object) -> object:
|
|
34
|
+
if not isinstance(value, str):
|
|
35
|
+
return value
|
|
36
|
+
try:
|
|
37
|
+
return TransportState(value)
|
|
38
|
+
except ValueError:
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class PositionInfo(BaseModel):
|
|
43
|
+
"""Typed view of the AVTransport GetPositionInfo response."""
|
|
44
|
+
|
|
45
|
+
model_config = ConfigDict(populate_by_name=True)
|
|
46
|
+
|
|
47
|
+
track: int | None = Field(None, alias="Track")
|
|
48
|
+
track_uri: str = Field("", alias="TrackURI")
|
|
49
|
+
track_duration: str = Field("", alias="TrackDuration")
|
|
50
|
+
track_metadata: str = Field("", alias="TrackMetaData")
|
|
51
|
+
relative_time: str = Field("", alias="RelTime")
|
|
52
|
+
absolute_time: str = Field("", alias="AbsTime")
|
|
53
|
+
|
|
54
|
+
@field_validator("track", mode="before")
|
|
55
|
+
@classmethod
|
|
56
|
+
def _coerce_track(cls, value: object) -> object:
|
|
57
|
+
return int_or_none(value) if isinstance(value, str) else value
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class SonosClient:
|
|
61
|
+
def __init__(
|
|
62
|
+
self,
|
|
63
|
+
ip: str,
|
|
64
|
+
*,
|
|
65
|
+
port: int = 1400,
|
|
66
|
+
uid: str = "",
|
|
67
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
68
|
+
http_client: httpx.AsyncClient | None = None,
|
|
69
|
+
) -> None:
|
|
70
|
+
self.ip = ip
|
|
71
|
+
self.port = port
|
|
72
|
+
self.uid = uid
|
|
73
|
+
self._owns_client = http_client is None
|
|
74
|
+
self._http = http_client or httpx.AsyncClient(timeout=timeout)
|
|
75
|
+
|
|
76
|
+
@classmethod
|
|
77
|
+
def from_speaker(cls, speaker: Speaker, *, timeout: float = DEFAULT_TIMEOUT) -> Self:
|
|
78
|
+
return cls(speaker.ip, port=speaker.port, uid=speaker.uid, timeout=timeout)
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def base_url(self) -> str:
|
|
82
|
+
return f"http://{self.ip}:{self.port}"
|
|
83
|
+
|
|
84
|
+
async def close(self) -> None:
|
|
85
|
+
if self._owns_client:
|
|
86
|
+
await self._http.aclose()
|
|
87
|
+
|
|
88
|
+
async def __aenter__(self) -> SonosClient:
|
|
89
|
+
return self
|
|
90
|
+
|
|
91
|
+
async def __aexit__(self, exc_type: object, exc: object, traceback: object) -> None:
|
|
92
|
+
await self.close()
|
|
93
|
+
|
|
94
|
+
async def play(self) -> None:
|
|
95
|
+
await self.__av_transport("Play", Speed="1")
|
|
96
|
+
|
|
97
|
+
async def pause(self) -> None:
|
|
98
|
+
await self.__av_transport("Pause")
|
|
99
|
+
|
|
100
|
+
async def stop(self) -> None:
|
|
101
|
+
await self.__av_transport("Stop")
|
|
102
|
+
|
|
103
|
+
async def next(self) -> None:
|
|
104
|
+
await self.__av_transport("Next")
|
|
105
|
+
|
|
106
|
+
async def previous(self) -> None:
|
|
107
|
+
await self.__av_transport("Previous")
|
|
108
|
+
|
|
109
|
+
async def seek_queue(self, position: int) -> None:
|
|
110
|
+
await self.__av_transport("Seek", Unit="TRACK_NR", Target=str(position))
|
|
111
|
+
|
|
112
|
+
async def play_uri(self, uri: str, *, title: str = "", radio: bool = False) -> None:
|
|
113
|
+
metadata = radio_metadata(title or uri, uri) if radio else ""
|
|
114
|
+
await self.__av_transport("SetAVTransportURI", CurrentURI=uri, CurrentURIMetaData=metadata)
|
|
115
|
+
await self.play()
|
|
116
|
+
|
|
117
|
+
async def open(
|
|
118
|
+
self,
|
|
119
|
+
value: str | Favorite,
|
|
120
|
+
*,
|
|
121
|
+
title: str = "",
|
|
122
|
+
radio: bool = False,
|
|
123
|
+
next_: bool = False,
|
|
124
|
+
) -> int | None:
|
|
125
|
+
if isinstance(value, Favorite):
|
|
126
|
+
await self.open_favorite(value)
|
|
127
|
+
return None
|
|
128
|
+
await self.play_uri(value, title=title, radio=radio)
|
|
129
|
+
return None
|
|
130
|
+
|
|
131
|
+
async def enqueue_uri(
|
|
132
|
+
self,
|
|
133
|
+
uri: str,
|
|
134
|
+
*,
|
|
135
|
+
metadata: str = "",
|
|
136
|
+
next_: bool = False,
|
|
137
|
+
play: bool = False,
|
|
138
|
+
) -> int | None:
|
|
139
|
+
result = await self.__av_transport(
|
|
140
|
+
"AddURIToQueue",
|
|
141
|
+
EnqueuedURI=uri,
|
|
142
|
+
EnqueuedURIMetaData=metadata,
|
|
143
|
+
DesiredFirstTrackNumberEnqueued="0",
|
|
144
|
+
EnqueueAsNext="1" if next_ else "0",
|
|
145
|
+
)
|
|
146
|
+
value = result.get("FirstTrackNumberEnqueued")
|
|
147
|
+
position = int(value) if value and value.isdigit() else None
|
|
148
|
+
if play and position is not None:
|
|
149
|
+
await self.seek_queue(position)
|
|
150
|
+
await self.play()
|
|
151
|
+
return position
|
|
152
|
+
|
|
153
|
+
async def open_track(
|
|
154
|
+
self, value: str, *, title: str = "", next_: bool = False, play: bool = False
|
|
155
|
+
) -> int | None:
|
|
156
|
+
track = parse_track_id(value)
|
|
157
|
+
if play and not next_:
|
|
158
|
+
await self.play_uri(track.sonos_uri, title=title or track.uri)
|
|
159
|
+
return None
|
|
160
|
+
return await self.enqueue_uri(
|
|
161
|
+
track.sonos_uri,
|
|
162
|
+
metadata=track_metadata(track, title),
|
|
163
|
+
next_=next_,
|
|
164
|
+
play=play,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
async def line_in(self, source: Speaker | str | None = None) -> None:
|
|
168
|
+
source_uid = self._source_uid(source)
|
|
169
|
+
await self.play_uri(f"x-rincon-stream:{source_uid}")
|
|
170
|
+
|
|
171
|
+
async def tv(self) -> None:
|
|
172
|
+
if not self.uid:
|
|
173
|
+
raise ValueError(
|
|
174
|
+
"tv playback requires a SonosClient created from a discovered Speaker with uid"
|
|
175
|
+
)
|
|
176
|
+
await self.play_uri(f"x-sonos-htastream:{self.uid}:spdif")
|
|
177
|
+
|
|
178
|
+
async def get_volume(self) -> int:
|
|
179
|
+
result = await self._rendering("GetVolume", Channel="Master")
|
|
180
|
+
return int(result.get("CurrentVolume", "0"))
|
|
181
|
+
|
|
182
|
+
async def set_volume(self, volume: int) -> None:
|
|
183
|
+
volume = max(0, min(100, volume))
|
|
184
|
+
await self._rendering("SetVolume", Channel="Master", DesiredVolume=str(volume))
|
|
185
|
+
|
|
186
|
+
async def adjust_volume(self, delta: int) -> int:
|
|
187
|
+
volume = max(0, min(100, await self.get_volume() + delta))
|
|
188
|
+
await self.set_volume(volume)
|
|
189
|
+
return volume
|
|
190
|
+
|
|
191
|
+
async def get_group_volume(self) -> int:
|
|
192
|
+
result = await self._rendering("GetGroupVolume")
|
|
193
|
+
return int(result.get("CurrentVolume", "0"))
|
|
194
|
+
|
|
195
|
+
async def set_group_volume(self, volume: int) -> None:
|
|
196
|
+
volume = max(0, min(100, volume))
|
|
197
|
+
await self._rendering("SetGroupVolume", DesiredVolume=str(volume))
|
|
198
|
+
|
|
199
|
+
async def adjust_group_volume(self, delta: int) -> int:
|
|
200
|
+
volume = max(0, min(100, await self.get_group_volume() + delta))
|
|
201
|
+
await self.set_group_volume(volume)
|
|
202
|
+
return volume
|
|
203
|
+
|
|
204
|
+
async def get_mute(self) -> bool:
|
|
205
|
+
result = await self._rendering("GetMute", Channel="Master")
|
|
206
|
+
return result.get("CurrentMute") == "1"
|
|
207
|
+
|
|
208
|
+
async def set_mute(self, muted: bool) -> None:
|
|
209
|
+
await self._rendering("SetMute", Channel="Master", DesiredMute="1" if muted else "0")
|
|
210
|
+
|
|
211
|
+
async def toggle_mute(self) -> bool:
|
|
212
|
+
muted = not await self.get_mute()
|
|
213
|
+
await self.set_mute(muted)
|
|
214
|
+
return muted
|
|
215
|
+
|
|
216
|
+
async def get_transport_info(self) -> TransportInfo:
|
|
217
|
+
result = await self.__av_transport("GetTransportInfo")
|
|
218
|
+
return TransportInfo.model_validate(result)
|
|
219
|
+
|
|
220
|
+
async def get_position_info(self) -> PositionInfo:
|
|
221
|
+
result = await self.__av_transport("GetPositionInfo")
|
|
222
|
+
return PositionInfo.model_validate(result)
|
|
223
|
+
|
|
224
|
+
async def now_playing(self) -> PlaybackState:
|
|
225
|
+
transport = await self.get_transport_info()
|
|
226
|
+
position = await self.get_position_info()
|
|
227
|
+
track = parse_track_metadata(
|
|
228
|
+
position.track_metadata,
|
|
229
|
+
uri=position.track_uri,
|
|
230
|
+
duration=position.track_duration,
|
|
231
|
+
position=position.track,
|
|
232
|
+
)
|
|
233
|
+
return PlaybackState(
|
|
234
|
+
state=transport.state.value if transport.state else "",
|
|
235
|
+
track=track,
|
|
236
|
+
relative_time=position.relative_time,
|
|
237
|
+
absolute_time=position.absolute_time,
|
|
238
|
+
track_duration=position.track_duration,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
async def queue(self, *, start: int = 0, count: int = 100) -> list[Track]:
|
|
242
|
+
result = await self.__content_directory(
|
|
243
|
+
"Browse",
|
|
244
|
+
ObjectID="Q:0",
|
|
245
|
+
BrowseFlag="BrowseDirectChildren",
|
|
246
|
+
Filter="*",
|
|
247
|
+
StartingIndex=str(start),
|
|
248
|
+
RequestedCount=str(count),
|
|
249
|
+
SortCriteria="",
|
|
250
|
+
)
|
|
251
|
+
tracks = []
|
|
252
|
+
for index, favorite in enumerate(
|
|
253
|
+
parse_favorites(result.get("Result", "")), start=start + 1
|
|
254
|
+
):
|
|
255
|
+
tracks.append(
|
|
256
|
+
Track(
|
|
257
|
+
title=favorite.title,
|
|
258
|
+
uri=favorite.uri,
|
|
259
|
+
album_art_uri=favorite.album_art_uri,
|
|
260
|
+
position=index,
|
|
261
|
+
)
|
|
262
|
+
)
|
|
263
|
+
return tracks
|
|
264
|
+
|
|
265
|
+
async def clear_queue(self) -> None:
|
|
266
|
+
await self.__av_transport("RemoveAllTracksFromQueue")
|
|
267
|
+
|
|
268
|
+
async def remove_queue_item(self, position: int) -> None:
|
|
269
|
+
await self.__av_transport("RemoveTrackFromQueue", ObjectID=f"Q:0/{position}")
|
|
270
|
+
|
|
271
|
+
async def favorites(self) -> list[Favorite]:
|
|
272
|
+
result = await self.__content_directory(
|
|
273
|
+
"Browse",
|
|
274
|
+
ObjectID="FV:2",
|
|
275
|
+
BrowseFlag="BrowseDirectChildren",
|
|
276
|
+
Filter="*",
|
|
277
|
+
StartingIndex="0",
|
|
278
|
+
RequestedCount="1000",
|
|
279
|
+
SortCriteria="",
|
|
280
|
+
)
|
|
281
|
+
return parse_favorites(result.get("Result", ""))
|
|
282
|
+
|
|
283
|
+
async def open_favorite(self, favorite: Favorite) -> None:
|
|
284
|
+
await self.__av_transport(
|
|
285
|
+
"SetAVTransportURI",
|
|
286
|
+
CurrentURI=favorite.uri,
|
|
287
|
+
CurrentURIMetaData=favorite.metadata,
|
|
288
|
+
)
|
|
289
|
+
await self.play()
|
|
290
|
+
|
|
291
|
+
async def join(self, coordinator: Speaker | str) -> None:
|
|
292
|
+
coordinator_uid = self._source_uid(coordinator)
|
|
293
|
+
await self.__av_transport(
|
|
294
|
+
"SetAVTransportURI",
|
|
295
|
+
CurrentURI=f"x-rincon:{coordinator_uid}",
|
|
296
|
+
CurrentURIMetaData="",
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
async def unjoin(self) -> None:
|
|
300
|
+
await self.__av_transport("BecomeCoordinatorOfStandaloneGroup")
|
|
301
|
+
|
|
302
|
+
def watch(
|
|
303
|
+
self,
|
|
304
|
+
*,
|
|
305
|
+
services: tuple[str | EventService, ...] = DEFAULT_SERVICES,
|
|
306
|
+
callback_host: str | None = None,
|
|
307
|
+
callback_port: int = 0,
|
|
308
|
+
timeout_seconds: int = 300,
|
|
309
|
+
) -> EventSubscription:
|
|
310
|
+
return EventSubscription(
|
|
311
|
+
self.ip,
|
|
312
|
+
port=self.port,
|
|
313
|
+
services=services,
|
|
314
|
+
callback_host=callback_host,
|
|
315
|
+
callback_port=callback_port,
|
|
316
|
+
timeout_seconds=timeout_seconds,
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
async def get_zone_group_state(self) -> str:
|
|
320
|
+
result = await self._soap(
|
|
321
|
+
"/ZoneGroupTopology/Control", _ZONE_GROUP_TOPOLOGY, "GetZoneGroupState"
|
|
322
|
+
)
|
|
323
|
+
return result.get("ZoneGroupState", "")
|
|
324
|
+
|
|
325
|
+
async def get_room_name(self) -> str:
|
|
326
|
+
result = await self._soap(
|
|
327
|
+
"/DeviceProperties/Control", _DEVICE_PROPERTIES, "GetZoneAttributes"
|
|
328
|
+
)
|
|
329
|
+
return result.get("CurrentZoneName", "")
|
|
330
|
+
|
|
331
|
+
async def __av_transport(self, action: str, **args: object) -> dict[str, str]:
|
|
332
|
+
return await self._soap(
|
|
333
|
+
"/MediaRenderer/AVTransport/Control",
|
|
334
|
+
_AV_TRANSPORT,
|
|
335
|
+
action,
|
|
336
|
+
InstanceID="0",
|
|
337
|
+
**args,
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
async def _rendering(self, action: str, **args: object) -> dict[str, str]:
|
|
341
|
+
return await self._soap(
|
|
342
|
+
"/MediaRenderer/RenderingControl/Control",
|
|
343
|
+
_RENDERING_CONTROL,
|
|
344
|
+
action,
|
|
345
|
+
InstanceID="0",
|
|
346
|
+
**args,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
async def __content_directory(self, action: str, **args: object) -> dict[str, str]:
|
|
350
|
+
return await self._soap(
|
|
351
|
+
"/MediaServer/ContentDirectory/Control",
|
|
352
|
+
_CONTENT_DIRECTORY,
|
|
353
|
+
action,
|
|
354
|
+
**args,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
async def _soap(
|
|
358
|
+
self,
|
|
359
|
+
path: str,
|
|
360
|
+
service_urn: str,
|
|
361
|
+
action: str,
|
|
362
|
+
**args: object,
|
|
363
|
+
) -> dict[str, str]:
|
|
364
|
+
return await soap_call(self._http, f"{self.base_url}{path}", service_urn, action, args)
|
|
365
|
+
|
|
366
|
+
def _source_uid(self, source: Speaker | str | None) -> str:
|
|
367
|
+
if isinstance(source, Speaker):
|
|
368
|
+
return source.uid
|
|
369
|
+
if isinstance(source, str):
|
|
370
|
+
return source.removeprefix("uuid:")
|
|
371
|
+
if self.uid:
|
|
372
|
+
return self.uid
|
|
373
|
+
raise ValueError("line-in playback requires a source Speaker or RINCON uid")
|
sonosify/controller.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from collections.abc import AsyncIterator
|
|
2
|
+
from contextlib import asynccontextmanager
|
|
3
|
+
|
|
4
|
+
from sonosify.client import DEFAULT_TIMEOUT, SonosClient
|
|
5
|
+
from sonosify.discovery import DEFAULT_DISCOVERY_TIMEOUT, discover
|
|
6
|
+
from sonosify.events import DEFAULT_SERVICES, EventService, EventSubscription
|
|
7
|
+
from sonosify.topology import SonosSystem
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SonosController:
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
*,
|
|
14
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
15
|
+
discovery_timeout: float = DEFAULT_DISCOVERY_TIMEOUT,
|
|
16
|
+
include_invisible: bool = False,
|
|
17
|
+
) -> None:
|
|
18
|
+
self.timeout = timeout
|
|
19
|
+
self.discovery_timeout = discovery_timeout
|
|
20
|
+
self.include_invisible = include_invisible
|
|
21
|
+
self.system: SonosSystem | None = None
|
|
22
|
+
|
|
23
|
+
async def discover(self) -> SonosSystem:
|
|
24
|
+
self.system = await discover(
|
|
25
|
+
timeout=self.timeout,
|
|
26
|
+
discovery_timeout=self.discovery_timeout,
|
|
27
|
+
include_invisible=self.include_invisible,
|
|
28
|
+
)
|
|
29
|
+
return self.system
|
|
30
|
+
|
|
31
|
+
async def client(
|
|
32
|
+
self,
|
|
33
|
+
room: str | None = None,
|
|
34
|
+
*,
|
|
35
|
+
ip: str | None = None,
|
|
36
|
+
coordinator: bool = True,
|
|
37
|
+
) -> SonosClient:
|
|
38
|
+
system = self.system or await self.discover()
|
|
39
|
+
return system.client(
|
|
40
|
+
room,
|
|
41
|
+
ip=ip,
|
|
42
|
+
coordinator=coordinator,
|
|
43
|
+
include_invisible=self.include_invisible,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
@asynccontextmanager
|
|
47
|
+
async def watch(
|
|
48
|
+
self,
|
|
49
|
+
room: str | None = None,
|
|
50
|
+
*,
|
|
51
|
+
ip: str | None = None,
|
|
52
|
+
coordinator: bool = True,
|
|
53
|
+
services: tuple[str | EventService, ...] = DEFAULT_SERVICES,
|
|
54
|
+
callback_host: str | None = None,
|
|
55
|
+
callback_port: int = 0,
|
|
56
|
+
timeout_seconds: int = 300,
|
|
57
|
+
) -> AsyncIterator[EventSubscription]:
|
|
58
|
+
async with (
|
|
59
|
+
await self.client(room, ip=ip, coordinator=coordinator) as client,
|
|
60
|
+
client.watch(
|
|
61
|
+
services=services,
|
|
62
|
+
callback_host=callback_host,
|
|
63
|
+
callback_port=callback_port,
|
|
64
|
+
timeout_seconds=timeout_seconds,
|
|
65
|
+
) as watcher,
|
|
66
|
+
):
|
|
67
|
+
yield watcher
|
|
68
|
+
|
|
69
|
+
async def play(self, room: str | None = None, *, ip: str | None = None) -> None:
|
|
70
|
+
async with await self.client(room, ip=ip) as client:
|
|
71
|
+
await client.play()
|
|
72
|
+
|
|
73
|
+
async def pause(self, room: str | None = None, *, ip: str | None = None) -> None:
|
|
74
|
+
async with await self.client(room, ip=ip) as client:
|
|
75
|
+
await client.pause()
|
|
76
|
+
|
|
77
|
+
async def stop(self, room: str | None = None, *, ip: str | None = None) -> None:
|
|
78
|
+
async with await self.client(room, ip=ip) as client:
|
|
79
|
+
await client.stop()
|
|
80
|
+
|
|
81
|
+
async def set_volume(
|
|
82
|
+
self, volume: int, room: str | None = None, *, ip: str | None = None
|
|
83
|
+
) -> None:
|
|
84
|
+
async with await self.client(room, ip=ip, coordinator=False) as client:
|
|
85
|
+
await client.set_volume(volume)
|
sonosify/didl.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from html import escape
|
|
2
|
+
from xml.etree import ElementTree
|
|
3
|
+
|
|
4
|
+
from sonosify.models import Favorite, Track
|
|
5
|
+
|
|
6
|
+
DIDL_NS = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
|
|
7
|
+
DC_NS = "http://purl.org/dc/elements/1.1/"
|
|
8
|
+
UPNP_NS = "urn:schemas-upnp-org:metadata-1-0/upnp/"
|
|
9
|
+
RINCON_NS = "urn:schemas-rinconnetworks-com:metadata-1-0/"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def parse_track_metadata(
|
|
13
|
+
metadata: str, *, uri: str = "", duration: str = "", position: int | None = None
|
|
14
|
+
) -> Track:
|
|
15
|
+
if not metadata:
|
|
16
|
+
return Track(uri=uri, duration=duration, position=position)
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
root = ElementTree.fromstring(metadata)
|
|
20
|
+
except ElementTree.ParseError:
|
|
21
|
+
return Track(uri=uri, duration=duration, position=position)
|
|
22
|
+
|
|
23
|
+
item = _first(root, "item")
|
|
24
|
+
if item is None:
|
|
25
|
+
item = root
|
|
26
|
+
return Track(
|
|
27
|
+
title=_text(item, "title"),
|
|
28
|
+
creator=_text(item, "creator"),
|
|
29
|
+
album=_text(item, "album"),
|
|
30
|
+
uri=uri or _text(item, "res"),
|
|
31
|
+
album_art_uri=_text(item, "albumArtURI"),
|
|
32
|
+
duration=duration,
|
|
33
|
+
position=position,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def parse_favorites(metadata: str) -> list[Favorite]:
|
|
38
|
+
if not metadata:
|
|
39
|
+
return []
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
root = ElementTree.fromstring(metadata)
|
|
43
|
+
except ElementTree.ParseError:
|
|
44
|
+
return []
|
|
45
|
+
|
|
46
|
+
favorites: list[Favorite] = []
|
|
47
|
+
for item in root.iter():
|
|
48
|
+
if _local_name(item.tag) not in {"item", "container"}:
|
|
49
|
+
continue
|
|
50
|
+
favorites.append(
|
|
51
|
+
Favorite(
|
|
52
|
+
title=_text(item, "title"),
|
|
53
|
+
uri=_text(item, "res"),
|
|
54
|
+
metadata=ElementTree.tostring(item, encoding="unicode"),
|
|
55
|
+
album_art_uri=_text(item, "albumArtURI"),
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
return favorites
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def radio_metadata(title: str, uri: str) -> str:
|
|
62
|
+
safe_title = escape(title, quote=False)
|
|
63
|
+
safe_uri = escape(uri, quote=False)
|
|
64
|
+
return (
|
|
65
|
+
f'<DIDL-Lite xmlns:dc="{DC_NS}" xmlns:upnp="{UPNP_NS}" '
|
|
66
|
+
f'xmlns:r="{RINCON_NS}" xmlns="{DIDL_NS}">'
|
|
67
|
+
'<item id="R:0/0/0" parentID="R:0/0" restricted="true">'
|
|
68
|
+
f"<dc:title>{safe_title}</dc:title>"
|
|
69
|
+
"<upnp:class>object.item.audioItem.audioBroadcast</upnp:class>"
|
|
70
|
+
f'<res protocolInfo="x-rincon-mp3radio:*:*:*">{safe_uri}</res>'
|
|
71
|
+
"</item>"
|
|
72
|
+
"</DIDL-Lite>"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _first(root: ElementTree.Element, local_name: str) -> ElementTree.Element | None:
|
|
77
|
+
for element in root.iter():
|
|
78
|
+
if _local_name(element.tag) == local_name:
|
|
79
|
+
return element
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _text(root: ElementTree.Element, local_name: str) -> str:
|
|
84
|
+
element = _first(root, local_name)
|
|
85
|
+
return "" if element is None or element.text is None else element.text.strip()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _local_name(tag: str) -> str:
|
|
89
|
+
return tag.rsplit("}", 1)[-1]
|