soco 0.31.1__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.
- soco/__init__.py +37 -0
- soco/alarms.py +603 -0
- soco/cache.py +213 -0
- soco/config.py +90 -0
- soco/core.py +3114 -0
- soco/data_structure_quirks.py +42 -0
- soco/data_structures.py +1323 -0
- soco/data_structures_entry.py +51 -0
- soco/discovery.py +767 -0
- soco/events.py +487 -0
- soco/events_asyncio.py +701 -0
- soco/events_base.py +780 -0
- soco/events_twisted.py +571 -0
- soco/exceptions.py +145 -0
- soco/groups.py +200 -0
- soco/ms_data_structures.py +682 -0
- soco/music_library.py +661 -0
- soco/music_services/__init__.py +8 -0
- soco/music_services/accounts.py +192 -0
- soco/music_services/data_structures.py +446 -0
- soco/music_services/music_service.py +1003 -0
- soco/music_services/token_store.py +108 -0
- soco/plugins/__init__.py +45 -0
- soco/plugins/example.py +44 -0
- soco/plugins/plex.py +209 -0
- soco/plugins/sharelink.py +285 -0
- soco/plugins/spotify.py +32 -0
- soco/plugins/wimp.py +525 -0
- soco/services.py +958 -0
- soco/snapshot.py +301 -0
- soco/soap.py +330 -0
- soco/utils.py +200 -0
- soco/xml.py +79 -0
- soco/zonegroupstate.py +400 -0
- soco-0.31.1.dist-info/METADATA +285 -0
- soco-0.31.1.dist-info/RECORD +40 -0
- soco-0.31.1.dist-info/WHEEL +5 -0
- soco-0.31.1.dist-info/licenses/AUTHORS.rst +39 -0
- soco-0.31.1.dist-info/licenses/LICENSE.rst +22 -0
- soco-0.31.1.dist-info/top_level.txt +1 -0
soco/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""SoCo (Sonos Controller) is a simple library to control Sonos speakers."""
|
|
2
|
+
|
|
3
|
+
# There is no need for all strings here to be unicode, and Py2 cannot import
|
|
4
|
+
# modules with unicode names so do not use from __future__ import
|
|
5
|
+
# unicode_literals
|
|
6
|
+
# https://github.com/SoCo/SoCo/issues/98
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
|
|
12
|
+
from .core import SoCo, soco_reset
|
|
13
|
+
from .discovery import discover
|
|
14
|
+
from .exceptions import SoCoException, UnknownSoCoException
|
|
15
|
+
|
|
16
|
+
# Will be parsed by setup.py to determine package metadata
|
|
17
|
+
__author__ = "The SoCo-Team <python-soco@googlegroups.com>"
|
|
18
|
+
# Please increment the version number and add the suffix "-dev" after
|
|
19
|
+
# a release, to make it possible to identify in-development code
|
|
20
|
+
__version__ = "0.31.1"
|
|
21
|
+
__website__ = "https://github.com/SoCo/SoCo"
|
|
22
|
+
__license__ = "MIT License"
|
|
23
|
+
|
|
24
|
+
# You really should not `import *` - it is poor practice
|
|
25
|
+
# but if you do, here is what you get:
|
|
26
|
+
__all__ = [
|
|
27
|
+
"discover",
|
|
28
|
+
"SoCo",
|
|
29
|
+
"soco_reset",
|
|
30
|
+
"SoCoException",
|
|
31
|
+
"UnknownSoCoException",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
# http://docs.python.org/2/howto/logging.html#library-config
|
|
35
|
+
# Avoids spurious error messages if no logger is configured by the user
|
|
36
|
+
|
|
37
|
+
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
soco/alarms.py
ADDED
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
"""This module contains classes relating to Sonos Alarms."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import re
|
|
5
|
+
from datetime import datetime, timedelta
|
|
6
|
+
|
|
7
|
+
from . import discovery
|
|
8
|
+
from .core import _SocoSingletonBase, PLAY_MODES
|
|
9
|
+
from .exceptions import SoCoException
|
|
10
|
+
from .xml import XML
|
|
11
|
+
|
|
12
|
+
log = logging.getLogger(__name__)
|
|
13
|
+
TIME_FORMAT = "%H:%M:%S"
|
|
14
|
+
RECURRENCE_KEYWORD_EQUIVALENT = {
|
|
15
|
+
"DAILY": "ON_0123456",
|
|
16
|
+
"ONCE": "ON_", # Never reoccurs
|
|
17
|
+
"WEEKDAYS": "ON_12345",
|
|
18
|
+
"WEEKENDS": "ON_06",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_valid_recurrence(text):
|
|
23
|
+
"""Check that ``text`` is a valid recurrence string.
|
|
24
|
+
|
|
25
|
+
A valid recurrence string is ``DAILY``, ``ONCE``, ``WEEKDAYS``,
|
|
26
|
+
``WEEKENDS`` or of the form ``ON_DDDDDD`` where ``D`` is a number from 0-6
|
|
27
|
+
representing a day of the week (Sunday is 0), e.g. ``ON_034`` meaning
|
|
28
|
+
Sunday, Wednesday and Thursday
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
text (str): the recurrence string to check.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
bool: `True` if the recurrence string is valid, else `False`.
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
|
|
38
|
+
>>> from soco.alarms import is_valid_recurrence
|
|
39
|
+
>>> is_valid_recurrence('WEEKENDS')
|
|
40
|
+
True
|
|
41
|
+
>>> is_valid_recurrence('')
|
|
42
|
+
False
|
|
43
|
+
>>> is_valid_recurrence('ON_132') # Mon, Tue, Wed
|
|
44
|
+
True
|
|
45
|
+
>>> is_valid_recurrence('ON_666') # Sat
|
|
46
|
+
True
|
|
47
|
+
>>> is_valid_recurrence('ON_3421') # Mon, Tue, Wed, Thur
|
|
48
|
+
True
|
|
49
|
+
>>> is_valid_recurrence('ON_123456789') # Too many digits
|
|
50
|
+
False
|
|
51
|
+
"""
|
|
52
|
+
if text in ("DAILY", "ONCE", "WEEKDAYS", "WEEKENDS"):
|
|
53
|
+
return True
|
|
54
|
+
return re.search(r"^ON_[0-6]{1,7}$", text) is not None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Alarms(_SocoSingletonBase):
|
|
58
|
+
"""A class representing all known Sonos Alarms.
|
|
59
|
+
|
|
60
|
+
Is a singleton and every `Alarms()` object will return the same instance.
|
|
61
|
+
|
|
62
|
+
Example use:
|
|
63
|
+
|
|
64
|
+
>>> get_alarms()
|
|
65
|
+
{469: <Alarm id:469@22:07:41 at 0x7f5198797dc0>,
|
|
66
|
+
470: <Alarm id:470@22:07:46 at 0x7f5198797d60>}
|
|
67
|
+
>>> alarms = Alarms()
|
|
68
|
+
>>> alarms.update()
|
|
69
|
+
>>> alarms.alarms
|
|
70
|
+
{469: <Alarm id:469@22:07:41 at 0x7f5198797dc0>,
|
|
71
|
+
470: <Alarm id:470@22:07:46 at 0x7f5198797d60>}
|
|
72
|
+
>>> for alarm in alarms:
|
|
73
|
+
... alarm
|
|
74
|
+
...
|
|
75
|
+
<Alarm id:469@22:07:41 at 0x7f5198797dc0>
|
|
76
|
+
<Alarm id:470@22:07:46 at 0x7f5198797d60>
|
|
77
|
+
>>> alarms[470]
|
|
78
|
+
<Alarm id:470@22:07:46 at 0x7f5198797d60>
|
|
79
|
+
>>> new_alarm = Alarm(zone)
|
|
80
|
+
>>> new_alarm.save()
|
|
81
|
+
471
|
|
82
|
+
>>> new_alarm.recurrence = "ONCE"
|
|
83
|
+
>>> new_alarm.save()
|
|
84
|
+
471
|
|
85
|
+
>>> alarms.alarms
|
|
86
|
+
{469: <Alarm id:469@22:07:41 at 0x7f5198797dc0>,
|
|
87
|
+
470: <Alarm id:470@22:07:46 at 0x7f5198797d60>,
|
|
88
|
+
471: <Alarm id:471@22:08:40 at 0x7f51987f1b50>}
|
|
89
|
+
>>> alarms[470].remove()
|
|
90
|
+
>>> alarms.alarms
|
|
91
|
+
{469: <Alarm id:469@22:07:41 at 0x7f5198797dc0>,
|
|
92
|
+
471: <Alarm id:471@22:08:40 at 0x7f51987f1b50>}
|
|
93
|
+
>>> for alarm in alarms:
|
|
94
|
+
... alarm.remove()
|
|
95
|
+
...
|
|
96
|
+
>>> a.alarms
|
|
97
|
+
{}
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
_class_group = "Alarms"
|
|
101
|
+
|
|
102
|
+
def __init__(self):
|
|
103
|
+
"""Initialize the instance."""
|
|
104
|
+
self.alarms = {}
|
|
105
|
+
self.alarms_skipped = {}
|
|
106
|
+
self._last_zone_used = None
|
|
107
|
+
self._last_alarm_list_version = None
|
|
108
|
+
self.last_uid = None
|
|
109
|
+
self.last_id = 0
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def last_alarm_list_version(self):
|
|
113
|
+
"""Return last seen alarm list version."""
|
|
114
|
+
return self._last_alarm_list_version
|
|
115
|
+
|
|
116
|
+
@last_alarm_list_version.setter
|
|
117
|
+
def last_alarm_list_version(self, alarm_list_version):
|
|
118
|
+
"""Store alarm list version and store UID/ID values."""
|
|
119
|
+
self.last_uid, last_id = alarm_list_version.split(":")
|
|
120
|
+
self.last_id = int(last_id)
|
|
121
|
+
self._last_alarm_list_version = alarm_list_version
|
|
122
|
+
|
|
123
|
+
def __iter__(self):
|
|
124
|
+
"""Return an interator for all alarms."""
|
|
125
|
+
yield from list(self.alarms.values())
|
|
126
|
+
|
|
127
|
+
def __len__(self):
|
|
128
|
+
"""Return the number of alarms."""
|
|
129
|
+
return len(self.alarms)
|
|
130
|
+
|
|
131
|
+
def __getitem__(self, alarm_id):
|
|
132
|
+
"""Return the alarm by ID."""
|
|
133
|
+
return self.alarms[alarm_id]
|
|
134
|
+
|
|
135
|
+
def get(self, alarm_id):
|
|
136
|
+
"""Return the alarm by ID or None."""
|
|
137
|
+
return self.alarms.get(alarm_id)
|
|
138
|
+
|
|
139
|
+
def update(self, zone=None):
|
|
140
|
+
"""Update all alarms and current alarm list version.
|
|
141
|
+
|
|
142
|
+
Raises:
|
|
143
|
+
SoCoException: If the 'CurrentAlarmListVersion' value is unexpected.
|
|
144
|
+
May occur if the provided zone is from a different household.
|
|
145
|
+
"""
|
|
146
|
+
if zone is None:
|
|
147
|
+
zone = self._last_zone_used or discovery.any_soco()
|
|
148
|
+
|
|
149
|
+
self._last_zone_used = zone
|
|
150
|
+
|
|
151
|
+
response = zone.alarmClock.ListAlarms()
|
|
152
|
+
current_alarm_list_version = response["CurrentAlarmListVersion"]
|
|
153
|
+
|
|
154
|
+
if self.last_alarm_list_version:
|
|
155
|
+
alarm_list_uid, alarm_list_id = current_alarm_list_version.split(":")
|
|
156
|
+
if self.last_uid != alarm_list_uid:
|
|
157
|
+
matching_zone = next(
|
|
158
|
+
(z for z in zone.all_zones if z.uid == alarm_list_uid), None
|
|
159
|
+
)
|
|
160
|
+
if not matching_zone:
|
|
161
|
+
raise SoCoException(
|
|
162
|
+
"Alarm list UID {} does not match {}".format(
|
|
163
|
+
current_alarm_list_version, self.last_alarm_list_version
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if int(alarm_list_id) <= self.last_id:
|
|
168
|
+
return
|
|
169
|
+
|
|
170
|
+
self.last_alarm_list_version = current_alarm_list_version
|
|
171
|
+
|
|
172
|
+
new_alarms = parse_alarm_payload(response, zone)
|
|
173
|
+
old_skipped = self.alarms_skipped
|
|
174
|
+
self.alarms_skipped = {}
|
|
175
|
+
# Update existing and create new Alarm instances
|
|
176
|
+
for alarm_id, kwargs in new_alarms.items():
|
|
177
|
+
existing_alarm = self.alarms.get(alarm_id) or old_skipped.get(alarm_id)
|
|
178
|
+
if existing_alarm:
|
|
179
|
+
existing_alarm.update(**kwargs)
|
|
180
|
+
if existing_alarm.zone is None:
|
|
181
|
+
self.alarms_skipped[alarm_id] = existing_alarm
|
|
182
|
+
else:
|
|
183
|
+
self.alarms[alarm_id] = existing_alarm
|
|
184
|
+
else:
|
|
185
|
+
new_alarm = Alarm(**kwargs)
|
|
186
|
+
new_alarm._alarm_id = alarm_id # pylint: disable=protected-access
|
|
187
|
+
if new_alarm.zone is None:
|
|
188
|
+
self.alarms_skipped[alarm_id] = new_alarm
|
|
189
|
+
else:
|
|
190
|
+
self.alarms[alarm_id] = new_alarm
|
|
191
|
+
|
|
192
|
+
# Prune alarms removed externally
|
|
193
|
+
for alarm_id in list(self.alarms):
|
|
194
|
+
if not new_alarms.get(alarm_id):
|
|
195
|
+
self.alarms.pop(alarm_id)
|
|
196
|
+
|
|
197
|
+
def update_skipped(self, zone):
|
|
198
|
+
"""Update the zone for any skipped alarms and move to main list if possible."""
|
|
199
|
+
for alarm_id, alarm in list(self.alarms_skipped.items()):
|
|
200
|
+
if zone.uid == alarm.room_uuid:
|
|
201
|
+
alarm.zone = zone
|
|
202
|
+
self.alarms[alarm_id] = alarm
|
|
203
|
+
self.alarms_skipped.pop(alarm_id)
|
|
204
|
+
|
|
205
|
+
def get_next_alarm_datetime(
|
|
206
|
+
self, from_datetime=None, include_disabled=False, zone_uid=None
|
|
207
|
+
):
|
|
208
|
+
"""Get the next alarm trigger datetime.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
from_datetime (datetime, optional): a datetime to reference next
|
|
212
|
+
alarms from. This argument filters by alarms on or after this
|
|
213
|
+
exact time. Since alarms do not store timezone information,
|
|
214
|
+
the output timezone will match this input argument. Defaults
|
|
215
|
+
to `datetime.now()`.
|
|
216
|
+
include_disabled (bool, optional): If `True` then disabled alarms
|
|
217
|
+
will be included in searching for the next alarm. Defaults to
|
|
218
|
+
`False`.
|
|
219
|
+
zone_uid (str, optional): If set the alarms will be filtered by
|
|
220
|
+
zone with this UID. Defaults to `None`.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
datetime: The next alarm trigger datetime or None if disabled
|
|
224
|
+
|
|
225
|
+
Note:
|
|
226
|
+
Alarms in `alarms_skipped` (whose speaker was not registered at
|
|
227
|
+
the time of the last `update()`) are not considered.
|
|
228
|
+
"""
|
|
229
|
+
if from_datetime is None:
|
|
230
|
+
from_datetime = datetime.now()
|
|
231
|
+
|
|
232
|
+
next_alarm_datetime = None
|
|
233
|
+
for alarm_id in self.alarms:
|
|
234
|
+
this_alarm = self.alarms.get(alarm_id)
|
|
235
|
+
if zone_uid is not None and this_alarm.zone.uid != zone_uid:
|
|
236
|
+
continue
|
|
237
|
+
this_next_datetime = this_alarm.get_next_alarm_datetime(
|
|
238
|
+
from_datetime, include_disabled
|
|
239
|
+
)
|
|
240
|
+
if (next_alarm_datetime is None) or (
|
|
241
|
+
this_next_datetime is not None
|
|
242
|
+
and this_next_datetime < next_alarm_datetime
|
|
243
|
+
):
|
|
244
|
+
next_alarm_datetime = this_next_datetime
|
|
245
|
+
return next_alarm_datetime
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class Alarm:
|
|
249
|
+
"""A class representing a Sonos Alarm.
|
|
250
|
+
|
|
251
|
+
Alarms may be created or updated and saved to, or removed from the Sonos
|
|
252
|
+
system. An alarm is not automatically saved. Call `save()` to do that.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
def __init__(
|
|
256
|
+
self,
|
|
257
|
+
zone,
|
|
258
|
+
start_time=None,
|
|
259
|
+
duration=None,
|
|
260
|
+
recurrence="DAILY",
|
|
261
|
+
enabled=True,
|
|
262
|
+
program_uri=None,
|
|
263
|
+
program_metadata="",
|
|
264
|
+
play_mode="NORMAL",
|
|
265
|
+
volume=20,
|
|
266
|
+
include_linked_zones=False,
|
|
267
|
+
room_uuid=None,
|
|
268
|
+
):
|
|
269
|
+
"""
|
|
270
|
+
Args:
|
|
271
|
+
zone (`SoCo`): The soco instance which will play the alarm.
|
|
272
|
+
start_time (datetime.time, optional): The alarm's start time.
|
|
273
|
+
Specify hours, minutes and seconds only. Defaults to the
|
|
274
|
+
current time.
|
|
275
|
+
duration (datetime.time, optional): The alarm's duration. Specify
|
|
276
|
+
hours, minutes and seconds only. May be `None` for unlimited
|
|
277
|
+
duration. Defaults to `None`.
|
|
278
|
+
recurrence (str, optional): A string representing how
|
|
279
|
+
often the alarm should be triggered. Can be ``DAILY``,
|
|
280
|
+
``ONCE``, ``WEEKDAYS``, ``WEEKENDS`` or of the form
|
|
281
|
+
``ON_DDDDDD`` where ``D`` is a number from 0-6 representing a
|
|
282
|
+
day of the week (Sunday is 0), e.g. ``ON_034`` meaning Sunday,
|
|
283
|
+
Wednesday and Thursday. Defaults to ``DAILY``.
|
|
284
|
+
enabled (bool, optional): `True` if alarm is enabled, `False`
|
|
285
|
+
otherwise. Defaults to `True`.
|
|
286
|
+
program_uri(str, optional): The uri to play. If `None`, the
|
|
287
|
+
built-in Sonos chime sound will be used. Defaults to `None`.
|
|
288
|
+
program_metadata (str, optional): The metadata associated with
|
|
289
|
+
'program_uri'. Defaults to ''.
|
|
290
|
+
play_mode(str, optional): The play mode for the alarm. Can be one
|
|
291
|
+
of ``NORMAL``, ``SHUFFLE_NOREPEAT``, ``SHUFFLE``,
|
|
292
|
+
``REPEAT_ALL``, ``REPEAT_ONE``, ``SHUFFLE_REPEAT_ONE``.
|
|
293
|
+
Defaults to ``NORMAL``.
|
|
294
|
+
volume (int, optional): The alarm's volume (0-100). Defaults to 20.
|
|
295
|
+
include_linked_zones (bool, optional): `True` if the alarm should
|
|
296
|
+
be played on the other speakers in the same group, `False`
|
|
297
|
+
otherwise. Defaults to `False`.
|
|
298
|
+
room_uuid (str, optional): The UUID of the room/speaker this alarm
|
|
299
|
+
belongs to. Set automatically when loading alarms from the
|
|
300
|
+
Sonos system. Defaults to `None`.
|
|
301
|
+
"""
|
|
302
|
+
|
|
303
|
+
self.zone = zone
|
|
304
|
+
if start_time is None:
|
|
305
|
+
start_time = datetime.now().time().replace(microsecond=0)
|
|
306
|
+
self.start_time = start_time
|
|
307
|
+
self.duration = duration
|
|
308
|
+
self.recurrence = recurrence
|
|
309
|
+
self.enabled = enabled
|
|
310
|
+
self.program_uri = program_uri
|
|
311
|
+
self.program_metadata = program_metadata
|
|
312
|
+
self.play_mode = play_mode
|
|
313
|
+
self.volume = volume
|
|
314
|
+
self.include_linked_zones = include_linked_zones
|
|
315
|
+
self.room_uuid = room_uuid
|
|
316
|
+
self._alarm_id = None
|
|
317
|
+
|
|
318
|
+
def __repr__(self):
|
|
319
|
+
middle = str(self.start_time.strftime(TIME_FORMAT))
|
|
320
|
+
return "<{} id:{}@{} at {}>".format(
|
|
321
|
+
self.__class__.__name__, self.alarm_id, middle, hex(id(self))
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def update(self, **kwargs):
|
|
325
|
+
"""Update an existing Alarm instance using the same arguments as __init__."""
|
|
326
|
+
for attr, value in kwargs.items():
|
|
327
|
+
if not hasattr(self, attr):
|
|
328
|
+
raise SoCoException(f"Alarm does not have attribute {attr}")
|
|
329
|
+
setattr(self, attr, value)
|
|
330
|
+
|
|
331
|
+
@property
|
|
332
|
+
def play_mode(self):
|
|
333
|
+
"""
|
|
334
|
+
`str`: The play mode for the alarm.
|
|
335
|
+
|
|
336
|
+
Can be one of ``NORMAL``, ``SHUFFLE_NOREPEAT``, ``SHUFFLE``,
|
|
337
|
+
``REPEAT_ALL``, ``REPEAT_ONE``, ``SHUFFLE_REPEAT_ONE``.
|
|
338
|
+
"""
|
|
339
|
+
return self._play_mode
|
|
340
|
+
|
|
341
|
+
@play_mode.setter
|
|
342
|
+
def play_mode(self, play_mode):
|
|
343
|
+
"""See `playmode`."""
|
|
344
|
+
play_mode = play_mode.upper()
|
|
345
|
+
if play_mode not in PLAY_MODES:
|
|
346
|
+
raise KeyError("'%s' is not a valid play mode" % play_mode)
|
|
347
|
+
self._play_mode = play_mode
|
|
348
|
+
|
|
349
|
+
@property
|
|
350
|
+
def volume(self):
|
|
351
|
+
"""`int`: The alarm's volume (0-100)."""
|
|
352
|
+
return self._volume
|
|
353
|
+
|
|
354
|
+
@volume.setter
|
|
355
|
+
def volume(self, volume):
|
|
356
|
+
"""See `volume`."""
|
|
357
|
+
# max 100
|
|
358
|
+
volume = int(volume)
|
|
359
|
+
self._volume = max(0, min(volume, 100)) # Coerce in range
|
|
360
|
+
|
|
361
|
+
@property
|
|
362
|
+
def recurrence(self):
|
|
363
|
+
"""`str`: How often the alarm should be triggered.
|
|
364
|
+
|
|
365
|
+
Can be ``DAILY``, ``ONCE``, ``WEEKDAYS``, ``WEEKENDS`` or of the form
|
|
366
|
+
``ON_DDDDDDD`` where ``D`` is a number from 0-7 representing a day of
|
|
367
|
+
the week (Sunday is 0), e.g. ``ON_034`` meaning Sunday, Wednesday and
|
|
368
|
+
Thursday.
|
|
369
|
+
"""
|
|
370
|
+
return self._recurrence
|
|
371
|
+
|
|
372
|
+
@recurrence.setter
|
|
373
|
+
def recurrence(self, recurrence):
|
|
374
|
+
"""See `recurrence`."""
|
|
375
|
+
if not is_valid_recurrence(recurrence):
|
|
376
|
+
raise KeyError("'%s' is not a valid recurrence value" % recurrence)
|
|
377
|
+
|
|
378
|
+
self._recurrence = recurrence
|
|
379
|
+
|
|
380
|
+
def save(self):
|
|
381
|
+
"""Save the alarm to the Sonos system.
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
str: The alarm ID, or `None` if no alarm was saved.
|
|
385
|
+
|
|
386
|
+
Raises:
|
|
387
|
+
~soco.exceptions.SoCoUPnPException: if the alarm cannot be created
|
|
388
|
+
because there is already an alarm for this room at the specified
|
|
389
|
+
time.
|
|
390
|
+
SoCoException: if `zone` is `None` (alarm was loaded for a speaker
|
|
391
|
+
that was not yet registered). Call `Alarms.update_skipped()`
|
|
392
|
+
with the zone once it is available before saving.
|
|
393
|
+
"""
|
|
394
|
+
if self.zone is None:
|
|
395
|
+
raise SoCoException(
|
|
396
|
+
"Cannot save alarm {}: zone is not set. "
|
|
397
|
+
"Call Alarms.update_skipped() with the zone first.".format(
|
|
398
|
+
self._alarm_id
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
args = [
|
|
402
|
+
("StartLocalTime", self.start_time.strftime(TIME_FORMAT)),
|
|
403
|
+
(
|
|
404
|
+
"Duration",
|
|
405
|
+
"" if self.duration is None else self.duration.strftime(TIME_FORMAT),
|
|
406
|
+
),
|
|
407
|
+
("Recurrence", self.recurrence),
|
|
408
|
+
("Enabled", "1" if self.enabled else "0"),
|
|
409
|
+
("RoomUUID", self.zone.uid),
|
|
410
|
+
(
|
|
411
|
+
"ProgramURI",
|
|
412
|
+
"x-rincon-buzzer:0" if self.program_uri is None else self.program_uri,
|
|
413
|
+
),
|
|
414
|
+
("ProgramMetaData", self.program_metadata),
|
|
415
|
+
("PlayMode", self.play_mode),
|
|
416
|
+
("Volume", self.volume),
|
|
417
|
+
("IncludeLinkedZones", "1" if self.include_linked_zones else "0"),
|
|
418
|
+
]
|
|
419
|
+
if self.alarm_id is None:
|
|
420
|
+
response = self.zone.alarmClock.CreateAlarm(args)
|
|
421
|
+
self._alarm_id = response["AssignedID"]
|
|
422
|
+
alarms = Alarms()
|
|
423
|
+
if alarms.last_id == int(self.alarm_id) - 1:
|
|
424
|
+
alarms.last_alarm_list_version = "{}:{}".format(
|
|
425
|
+
alarms.last_uid, self.alarm_id
|
|
426
|
+
)
|
|
427
|
+
alarms.alarms[self.alarm_id] = self
|
|
428
|
+
else:
|
|
429
|
+
# The alarm has been saved before. Update it instead.
|
|
430
|
+
args.insert(0, ("ID", self.alarm_id))
|
|
431
|
+
self.zone.alarmClock.UpdateAlarm(args)
|
|
432
|
+
return self.alarm_id
|
|
433
|
+
|
|
434
|
+
def remove(self):
|
|
435
|
+
"""Remove the alarm from the Sonos system.
|
|
436
|
+
|
|
437
|
+
There is no need to call `save`. The Python instance is not deleted,
|
|
438
|
+
and can be saved back to Sonos again if desired.
|
|
439
|
+
|
|
440
|
+
Returns:
|
|
441
|
+
bool: If the removal was sucessful.
|
|
442
|
+
"""
|
|
443
|
+
result = self.zone.alarmClock.DestroyAlarm([("ID", self.alarm_id)])
|
|
444
|
+
alarms = Alarms()
|
|
445
|
+
alarms.alarms.pop(self.alarm_id, None)
|
|
446
|
+
self._alarm_id = None
|
|
447
|
+
return result
|
|
448
|
+
|
|
449
|
+
@property
|
|
450
|
+
def alarm_id(self):
|
|
451
|
+
"""`str`: The ID of the alarm, or `None`."""
|
|
452
|
+
return self._alarm_id
|
|
453
|
+
|
|
454
|
+
def get_next_alarm_datetime(self, from_datetime=None, include_disabled=False):
|
|
455
|
+
"""Get the next alarm trigger datetime.
|
|
456
|
+
|
|
457
|
+
Args:
|
|
458
|
+
from_datetime (datetime, optional): a datetime to reference next
|
|
459
|
+
alarms from. This argument filters by alarms on or after this
|
|
460
|
+
exact time. Since alarms do not store timezone information,
|
|
461
|
+
the output timezone will match this input argument. Defaults
|
|
462
|
+
to `datetime.now()`.
|
|
463
|
+
include_disabled (bool, optional): If `True` then the next datetime
|
|
464
|
+
will be computed even if the alarm is disabled. Defaults to
|
|
465
|
+
`False`.
|
|
466
|
+
|
|
467
|
+
Returns:
|
|
468
|
+
datetime: The next alarm trigger datetime or None if disabled
|
|
469
|
+
"""
|
|
470
|
+
if not self.enabled and not include_disabled:
|
|
471
|
+
return None
|
|
472
|
+
|
|
473
|
+
if from_datetime is None:
|
|
474
|
+
from_datetime = datetime.now()
|
|
475
|
+
|
|
476
|
+
# Convert helper words to number recurrences
|
|
477
|
+
recurrence_on_str = RECURRENCE_KEYWORD_EQUIVALENT.get(
|
|
478
|
+
self.recurrence, self.recurrence
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
# For the purpose of finding the next alarm a "once" trigger that has
|
|
482
|
+
# yet to trigger is everyday (the next possible day)
|
|
483
|
+
if recurrence_on_str == RECURRENCE_KEYWORD_EQUIVALENT["ONCE"]:
|
|
484
|
+
recurrence_on_str = RECURRENCE_KEYWORD_EQUIVALENT["DAILY"]
|
|
485
|
+
|
|
486
|
+
# Trim the 'ON_' prefix, convert to int, remove duplicates
|
|
487
|
+
recurrence_set = set(map(int, recurrence_on_str[3:]))
|
|
488
|
+
|
|
489
|
+
# Convert Sonos weekdays to Python weekdays
|
|
490
|
+
# Sonos starts on Sunday, Python starts on Monday
|
|
491
|
+
if 0 in recurrence_set:
|
|
492
|
+
recurrence_set.remove(0)
|
|
493
|
+
recurrence_set.add(7)
|
|
494
|
+
recurrence_set = {x - 1 for x in recurrence_set}
|
|
495
|
+
|
|
496
|
+
# Begin search from next day if it would have already triggered today
|
|
497
|
+
offset = 0
|
|
498
|
+
if self.start_time <= from_datetime.time():
|
|
499
|
+
offset += 1
|
|
500
|
+
|
|
501
|
+
# Find first day
|
|
502
|
+
from_datetime_day = from_datetime.weekday()
|
|
503
|
+
offset_weekday = (from_datetime_day + offset) % 7
|
|
504
|
+
while offset_weekday not in recurrence_set:
|
|
505
|
+
offset += 1
|
|
506
|
+
offset_weekday = (from_datetime_day + offset) % 7
|
|
507
|
+
|
|
508
|
+
return datetime.combine(
|
|
509
|
+
from_datetime.date() + timedelta(days=offset),
|
|
510
|
+
self.start_time,
|
|
511
|
+
tzinfo=from_datetime.tzinfo,
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def get_alarms(zone=None):
|
|
516
|
+
"""Get a set of all alarms known to the Sonos system.
|
|
517
|
+
|
|
518
|
+
Args:
|
|
519
|
+
zone (soco.SoCo, optional): a SoCo instance to query. If None, a random
|
|
520
|
+
instance is used. Defaults to `None`.
|
|
521
|
+
|
|
522
|
+
Returns:
|
|
523
|
+
set: A set of `Alarm` instances
|
|
524
|
+
"""
|
|
525
|
+
alarms = Alarms()
|
|
526
|
+
alarms.update(zone)
|
|
527
|
+
return set(alarms.alarms.values())
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def remove_alarm_by_id(zone, alarm_id):
|
|
531
|
+
"""Remove an alarm from the Sonos system by its ID.
|
|
532
|
+
|
|
533
|
+
Args:
|
|
534
|
+
zone (`SoCo`): A SoCo instance, which can be any zone that belongs
|
|
535
|
+
to the Sonos system in which the required alarm is defined.
|
|
536
|
+
alarm_id (str): The ID of the alarm to be removed.
|
|
537
|
+
|
|
538
|
+
Returns:
|
|
539
|
+
bool: `True` if the alarm is found and removed, `False` otherwise.
|
|
540
|
+
"""
|
|
541
|
+
alarms = Alarms()
|
|
542
|
+
alarms.update(zone)
|
|
543
|
+
alarm = alarms.get(alarm_id)
|
|
544
|
+
if not alarm:
|
|
545
|
+
return False
|
|
546
|
+
return alarm.remove()
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def parse_alarm_payload(payload, zone):
|
|
550
|
+
"""Parse the XML payload response and return a dict of `Alarm` kwargs."""
|
|
551
|
+
alarm_list = payload["CurrentAlarmList"]
|
|
552
|
+
tree = XML.fromstring(alarm_list.encode("utf-8"))
|
|
553
|
+
|
|
554
|
+
# An alarm list looks like this:
|
|
555
|
+
# <Alarms>
|
|
556
|
+
# <Alarm ID="14" StartTime="07:00:00"
|
|
557
|
+
# Duration="02:00:00" Recurrence="DAILY" Enabled="1"
|
|
558
|
+
# RoomUUID="RINCON_000ZZZZZZ1400"
|
|
559
|
+
# ProgramURI="x-rincon-buzzer:0" ProgramMetaData=""
|
|
560
|
+
# PlayMode="SHUFFLE_NOREPEAT" Volume="25"
|
|
561
|
+
# IncludeLinkedZones="0"/>
|
|
562
|
+
# <Alarm ID="15" StartTime="07:00:00"
|
|
563
|
+
# Duration="02:00:00" Recurrence="DAILY" Enabled="1"
|
|
564
|
+
# RoomUUID="RINCON_000ZZZZZZ01400"
|
|
565
|
+
# ProgramURI="x-rincon-buzzer:0" ProgramMetaData=""
|
|
566
|
+
# PlayMode="SHUFFLE_NOREPEAT" Volume="25"
|
|
567
|
+
# IncludeLinkedZones="0"/>
|
|
568
|
+
# </Alarms>
|
|
569
|
+
|
|
570
|
+
alarms = tree.findall("Alarm")
|
|
571
|
+
alarm_args = {}
|
|
572
|
+
for alarm in alarms:
|
|
573
|
+
values = alarm.attrib
|
|
574
|
+
alarm_id = values["ID"]
|
|
575
|
+
|
|
576
|
+
alarm_zone = next(
|
|
577
|
+
(z for z in zone.all_zones if z.uid == values["RoomUUID"]), None
|
|
578
|
+
)
|
|
579
|
+
args = {
|
|
580
|
+
"zone": alarm_zone,
|
|
581
|
+
# StartTime not StartLocalTime which is used by CreateAlarm
|
|
582
|
+
"start_time": datetime.strptime(values["StartTime"], "%H:%M:%S").time(),
|
|
583
|
+
"duration": (
|
|
584
|
+
None
|
|
585
|
+
if values["Duration"] == ""
|
|
586
|
+
else datetime.strptime(values["Duration"], "%H:%M:%S").time()
|
|
587
|
+
),
|
|
588
|
+
"recurrence": values["Recurrence"],
|
|
589
|
+
"enabled": values["Enabled"] == "1",
|
|
590
|
+
"program_uri": (
|
|
591
|
+
None
|
|
592
|
+
if values["ProgramURI"] == "x-rincon-buzzer:0"
|
|
593
|
+
else values["ProgramURI"]
|
|
594
|
+
),
|
|
595
|
+
"program_metadata": values["ProgramMetaData"],
|
|
596
|
+
"play_mode": values["PlayMode"],
|
|
597
|
+
"volume": values["Volume"],
|
|
598
|
+
"include_linked_zones": values["IncludeLinkedZones"] == "1",
|
|
599
|
+
"room_uuid": values["RoomUUID"],
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
alarm_args[alarm_id] = args
|
|
603
|
+
return alarm_args
|