-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmodels.py
More file actions
1859 lines (1737 loc) Β· 61.7 KB
/
Copy pathmodels.py
File metadata and controls
1859 lines (1737 loc) Β· 61.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# AmpliPi Home Audio
# Copyright (C) 2022 MicroNova LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""AmpliPi Data Models
Encourages reuse of datastructures across AmpliPi
"""
# type handling, fastapi leverages type checking for performance and easy docs
from functools import lru_cache
from typing import List, Dict, Optional, Union, Set
from types import SimpleNamespace
from enum import Enum
from pathlib import Path
# pylint: disable=no-name-in-module
from pydantic import BaseSettings, BaseModel, Field
# pylint: disable=too-few-public-methods
# pylint: disable=missing-class-docstring
MIN_VOL_F = 0.0
""" Min volume for slider bar. Will be mapped to dB. """
MAX_VOL_F = 1.0
""" Max volume for slider bar. Will be mapped to dB. """
MIN_VOL_F_OVERFLOW = MIN_VOL_F - MAX_VOL_F
"""Min overflow for volume sliders, set to be the full range of vol_f below zero"""
MAX_VOL_F_OVERFLOW = MAX_VOL_F - MIN_VOL_F
"""Max overflow for volume sliders, set to be the full range of vol_f above zero"""
MIN_VOL_DB = -80
""" Min volume in dB. -80 is special and is actually -90 dB (mute). """
MAX_VOL_DB = 0
""" Max volume in dB. """
MIN_DB_RANGE = 20
""" Smallest allowed difference between a zone's vol_max and vol_min """
MAX_SOURCES = 4
""" Max audio sources """
SOURCE_DISCONNECTED = -1
""" Indicate no source connection, simulated in SW by muting zone for now """
ZONE_OFF = -2
"""
Indicate that a zone is considered off for the purpose of external interfaces such as home assistant
ON = source_id != ZONE_OFF
OFF = source_id == ZONE_OFF
To turn off: set to ZONE_OFF
To turn on: set to SOURCE_DISCONNECTED or any valid source_id
"""
def pcnt2Vol(pcnt: float) -> int:
""" Convert a percent to volume in dB """
assert MIN_VOL_F <= pcnt <= MAX_VOL_F
return round(pcnt * (MAX_VOL_DB - MIN_VOL_DB) + MIN_VOL_DB)
class fields(SimpleNamespace):
""" AmpliPi's field types """
ID = Field(description='Unique identifier')
Name = Field(description='Friendly name')
SourceId = Field(ge=ZONE_OFF, le=MAX_SOURCES - 1,
description='id of the connected source, or -1 for no connection, or -2 for reflecting STATE_OFF in third party interfaces such as home assistant')
ZoneId = Field(ge=0, le=35)
Mute = Field(description='Set to true if output is muted')
Volume = Field(ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Output volume in dB')
VolumeF = Field(ge=MIN_VOL_F, le=MAX_VOL_F,
description='Output volume as a floating-point scalar from 0.0 to 1.0 representing MIN_VOL_DB to MAX_VOL_DB')
VolumeDeltaF = Field(description='Adjustment to output volume as a floating-point scalar representing the distance between the current and goal volume. Can be anything, but is coerced to never exceed |MAX_VOL_F * 2|')
VolumeMin = Field(ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Min output volume in dB')
VolumeMax = Field(ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Max output volume in dB')
GroupMute = Field(description='Set to true if output is all zones muted')
GroupVolume = Field(ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Average output volume')
GroupVolumeF = Field(ge=MIN_VOL_F, le=MAX_VOL_F, description='Average output volume as a floating-point number')
Disabled = Field(description='Set to true if not connected to a speaker')
Zones = Field(description='Set of zone ids belonging to a group')
Groups = Field(description='List of group ids')
AudioInput = Field('', description="""Connected audio source
* Digital or Analog Stream ('stream=SID') where SID is the ID of the connected stream (rca inputs are now just the RCA stream type)
* Nothing ('') behind the scenes this is muxed to a digital output
""")
Port = Field(description='Port used by LMS server for metadata collection', default=9000)
class fields_w_default(SimpleNamespace):
""" AmpliPi's field types that need a default value
These are needed because there is ambiguity where an optional field has a default value
"""
# TODO: less duplication
SourceId = Field(default=0, ge=ZONE_OFF, le=MAX_SOURCES - 1,
description='id of the connected source, or -1 for no connection, or -2 for reflecting STATE_OFF in third party interfaces such as home assistant')
Mute = Field(default=True, description='Set to true if output is muted')
Volume = Field(default=MIN_VOL_DB, ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Output volume in dB')
VolumeF = Field(default=MIN_VOL_F, ge=MIN_VOL_F, le=MAX_VOL_F,
description='Output volume as a floating-point scalar from 0.0 to 1.0 representing MIN_VOL_DB to MAX_VOL_DB')
VolumeFOverflow = Field(default=0.0, ge=MIN_VOL_F_OVERFLOW, le=MAX_VOL_F_OVERFLOW,
description='Output volume as a floating-point scalar that has a range equal to MIN_VOL_F - MAX_VOL_F in both directions from zero, and is used to keep track of the relative distance between two or more zone volumes when they would otherwise have to exceed their VOL_F range')
VolumeMin = Field(default=MIN_VOL_DB, ge=MIN_VOL_DB, le=MAX_VOL_DB,
description='Min output volume in dB')
VolumeMax = Field(default=MAX_VOL_DB, ge=MIN_VOL_DB, le=MAX_VOL_DB,
description='Max output volume in dB')
GroupMute = Field(default=True, description='Set to true if output is all zones muted')
GroupVolume = Field(default=MIN_VOL_F, ge=MIN_VOL_F, le=MAX_VOL_F, description='Average output volume')
GroupVolumeF = Field(default=MIN_VOL_F, ge=MIN_VOL_F, le=MAX_VOL_F,
description='Average output volume as a floating-point number')
Disabled = Field(default=False, description='Set to true if not connected to a speaker')
class Base(BaseModel):
""" Base class for AmpliPi Models
id: Per type unique id generated on instance creation
id is always returned by all API calls.
It is optional so this calls can be abstract enough to use for creation and returned state
name: Associated name, not intended to be unique
"""
id: Optional[int] = fields.ID
name: str = fields.Name
class BaseUpdate(BaseModel):
""" Base class for updates to AmpliPi models
name: Associated name, updated if necessary
"""
name: Optional[str] = fields.Name
class PandoraRating(Enum):
# "rating" is a pandora-specific enum
# decode where text is common wording and ("text") is the pandora/pianobar terminology
# 0 default
# 1 liked ("loved")
# 2 disliked ("banned")
# 3 shelved ("tired")
DEFAULT = 0
LIKED = 1
DISLIKED = 2
SHELVED = 3
class SourceInfo(BaseModel):
name: str
state: str # paused, playing, stopped, unknown, loading ???
type: Optional[str]
artist: Optional[str]
track: Optional[str]
album: Optional[str]
station: Optional[str] # name of radio station
img_url: Optional[str]
supported_cmds: List[str] = []
rating: Optional[PandoraRating] # Only used for pandora
temporary: Optional[str] # Only used for file players
class Source(Base):
""" An audio source """
input: str = fields.AudioInput
info: Optional[SourceInfo] = Field(
description='Additional info about the current audio playing from the stream (generated during playback)')
def get_stream(self) -> Optional[int]:
""" Get a source's connected stream if any """
try:
sinput = str(self.input)
if sinput.startswith('stream='):
return int(sinput.split('=')[1])
return None
except ValueError:
return None
def as_update(self) -> 'SourceUpdate':
""" Convert to SourceUpdate """
update = self.dict()
update.pop('id')
return SourceUpdate.parse_obj(update)
class Config:
schema_extra = {
'examples': {
'stream connected': {
'value': {
'id': 1,
'name': '1',
'input': 'stream=1004',
'info': {
'album': 'Far (Deluxe Version)',
'artist': 'Regina Spektor',
'img_url': 'http://mediaserver-cont-dc6-1-v4v6.pandora.com/images/public/int/2/1/5/4/093624974512_500W_500H.jpg',
'station': 'Regina Spektor Radio',
'track': 'Eet',
'state': 'playing',
'type': 'pandora',
}
}
},
'nothing connected': {
'value': {
'id': 2,
'name': '2',
'input': '',
'info': {
'img_url': 'static/imgs/disconnected.png',
'state': 'stopped',
}
}
},
'rca connected': {
'value': {
'id': 3,
'name': '3',
'input': 'stream=999',
'info': {
'img_url': 'static/imgs/rca_inputs.svg',
'state': 'unknown',
'type': 'rca',
}
}
},
}
}
class SourceUpdate(BaseUpdate):
""" Partial reconfiguration of an audio Source """
input: Optional[str] = fields.AudioInput
class Config:
schema_extra = {
'examples': {
'Update Input to RCA Input 2': {
'value': {'input': 'stream=997'}
},
'Update name': {
'value': {'name': 'J2'}
},
'Update Input to Matt and Kim Radio': {
'value': {'input': 'stream=1004'}
},
}
}
class BrowsableItem(BaseModel):
""" An item that can be browsed """
id: str # id for this item that is unique within it's stream
name: str # name of the item
playable: bool # can this item be played
parent: bool # is this item a parent item, e.g. can it's children be browsed
img: Optional[str] = None # url to an image for this item
class BrowsableItemResponse(BaseModel):
items: List[BrowsableItem]
class Config:
schema_extra = {
'examples': {
'Pandora stream': {
'value': {
'items': [
{
'id': '0',
'name': 'Blink-182 Radio',
'playable': True,
'parent': False
},
{
'id': '1',
'name': 'Cake Radio',
'playable': True,
'parent': False
},
{
'id': '2',
'name': 'Chiptune Radio',
'playable': True,
'parent': False
},
{
'id': '3',
'name': 'Glitch Hop Radio',
'playable': True,
'parent': False
}
]
}
}
}
}
class SourceUpdateWithId(SourceUpdate):
""" Partial reconfiguration of a specific audio Source """
id: int = Field(ge=0, le=MAX_SOURCES - 1)
def as_update(self) -> SourceUpdate:
""" Convert to SourceUpdate """
update = self.dict()
update.pop('id')
return SourceUpdate.parse_obj(update)
class Zone(Base):
""" Audio output to a stereo pair of speakers, typically belonging to a room """
source_id: int = fields_w_default.SourceId
mute: bool = fields_w_default.Mute
vol: int = fields_w_default.Volume
vol_f: float = fields_w_default.VolumeF
vol_f_overflow: float = fields_w_default.VolumeFOverflow
vol_min: int = fields_w_default.VolumeMin
vol_max: int = fields_w_default.VolumeMax
disabled: bool = fields_w_default.Disabled
def as_update(self) -> 'ZoneUpdate':
""" Convert to ZoneUpdate """
update = self.dict()
update.pop('id')
return ZoneUpdate.parse_obj(update)
class Config:
schema_extra = {
'examples': {
'Living Room': {
'value': {
'name': 'Living Room',
'source_id': 1,
'mute': False,
'vol': pcnt2Vol(0.69),
'vol_f': 0.69,
'vol_min': MIN_VOL_DB,
'vol_max': MAX_VOL_DB,
'disabled': False,
}
},
'Dining Room': {
'value': {
'name': 'Dining Room',
'source_id': 2,
'mute': True,
'vol': pcnt2Vol(0.19),
'vol_f': 0.19,
'vol_min': int(0.1 * (MAX_VOL_DB + MIN_VOL_DB)),
'vol_max': int(0.8 * (MAX_VOL_DB + MIN_VOL_DB)),
'disabled': False,
}
},
}
}
class ZoneUpdate(BaseUpdate):
""" Reconfiguration of a Zone """
source_id: Optional[int] = fields.SourceId
mute: Optional[bool] = fields.Mute
vol: Optional[int] = fields.Volume
vol_f: Optional[float] = fields.VolumeF
vol_delta_f: Optional[float] = fields.VolumeDeltaF
vol_min: Optional[int] = fields.VolumeMin
vol_max: Optional[int] = fields.VolumeMax
disabled: Optional[bool] = fields.Disabled
class Config:
schema_extra = {
'examples': {
'Change name': {
'value': {
'name':
'Bedroom'
}
},
'Change audio source': {
'value': {
'source_id': 3
}
},
'Decrease volume relative to min/max volume by 10 percent': {
'value': {
'vol_delta_f': -0.1
}
},
'Increase volume relative to min/max volume by 10 percent': {
'value': {
'vol_delta_f': 0.1
}
},
'Change volume relative to min/max volume': {
'value': {
'vol': 0.44
}
},
'Change volume in absolute decibels': {
'value': {
'vol': pcnt2Vol(0.44)
}
},
'Mute': {
'value': {
'mute': True
}
},
'Change max volume': {
'value': {
'vol_max': int(0.8 * MAX_VOL_DB)
}
}
},
}
class ZoneUpdateWithId(ZoneUpdate):
""" Reconfiguration of a specific Zone """
id: int = fields.ZoneId
def as_update(self) -> ZoneUpdate:
""" Convert to ZoneUpdate """
update = self.dict()
update.pop('id')
return ZoneUpdate.parse_obj(update)
class MultiZoneUpdate(BaseModel):
""" Reconfiguration of multiple zones specified by zone_ids and group_ids """
zones: Optional[List[int]] = fields.Zones
groups: Optional[List[int]] = fields.Groups
update: ZoneUpdate
class Config:
schema_extra = {
'examples': {
'Connect all zones to source 1': {
'value': {
'zones': [0, 1, 2, 3, 4, 5],
'update': {'source_id': 0}
}
},
'Change the relative volume on all zones': {
'value': {
'zones': [0, 1, 2, 3, 4, 5],
'update': {'vol_f': 0.5, "mute": False}
}
},
'Decrease volume relative to min/max volume by 10 percent on first 3 zones': {
'value': {
'zones': [0, 1, 2],
'update': {'vol_delta_f': -0.1}
}
},
'Increase volume relative to min/max volume by 10 percent on zones 3, 4, and 5': {
'value': {
'zones': [3, 4, 5],
'update': {'vol_delta_f': 0.1}
}
},
},
}
class Group(Base):
""" A group of zones that can share the same audio input and be controlled as a group ie. Upstairs.
Volume, mute, and source_id fields are aggregates of the member zones."""
source_id: Optional[int] = fields.SourceId
zones: List[int] = fields.Zones # should be a set, but JSON doesn't have native sets
mute: Optional[bool] = fields.GroupMute
vol_delta: Optional[int] = fields.GroupVolume
vol_f: Optional[float] = fields.GroupVolumeF
def as_update(self) -> 'GroupUpdate':
""" Convert to GroupUpdate """
update = self.dict()
update.pop('id')
return GroupUpdate.parse_obj(update)
class Config:
schema_extra = {
'creation_examples': {
'Upstairs Group': {
'value': {
'name': 'Upstairs',
'zones': [1, 2, 3, 4, 5]
}
},
'Downstairs Group': {
'value': {
'name': 'Downstairs',
'zones': [6, 7, 8, 9]
}
}
},
'examples': {
'Upstairs Group': {
'value': {
'id': 101,
'name': 'Upstairs',
'zones': [1, 2, 3, 4, 5],
'vol_delta': pcnt2Vol(0.19),
'vol_f': 0.19,
}
},
'Downstairs Group': {
'value': {
'id': 102,
'name': 'Downstairs',
'zones': [6, 7, 8, 9],
'vol_delta': pcnt2Vol(0.63),
'vol_f': 0.63,
}
}
},
}
class GroupUpdate(BaseUpdate):
""" Reconfiguration of a Group """
source_id: Optional[int] = fields.SourceId
zones: Optional[List[int]] = fields.Zones
mute: Optional[bool] = fields.GroupMute
vol_delta: Optional[int] = fields.GroupVolume
vol_f: Optional[float] = fields.GroupVolumeF
class Config:
schema_extra = {
'examples': {
'Rezone group': {
'value': {
'name': 'Upstairs',
'zones': [3, 4, 5]
}
},
'Change name': {
'value': {
'name': 'Upstairs'
}
},
'Change audio source': {
'value': {
'source_id': 3
}
},
"Set volume relative to each zone's min/max volume": {
'value': {
'vol_f': 0.44
}
},
'Set volume of each zone in absolute decibels': {
'value': {
'vol_delta': pcnt2Vol(0.44)
}
},
'Mute': {
'value': {
'mute': True
}
}
},
}
class GroupUpdateWithId(GroupUpdate):
""" Reconfiguration of a specific Group """
id: int
def as_update(self) -> GroupUpdate:
""" Convert to GroupUpdate """
update = self.dict()
update.pop('id')
return GroupUpdate.parse_obj(update)
class Stream(Base):
""" Digital stream such as Pandora, AirPlay or Spotify """
type: str = Field(description="""stream type
* pandora
* airplay
* dlna
* internetradio
* spotify
* plexamp
* aux
* file
* fmradio
* lms
* bluetooth
* rca
""")
# TODO: how to support different stream types
user: Optional[str] = Field(description='User login')
password: Optional[str] = Field(description='Password')
station: Optional[str] = Field(description='Radio station identifier')
url: Optional[str] = Field(description='Stream url, used for internetradio and file')
logo: Optional[str] = Field(description='Icon/Logo url, used for internetradio')
freq: Optional[str] = Field(description='FM Frequency (MHz), used for fmradio')
client_id: Optional[str] = Field(description='Plexamp client_id, becomes "identifier" in server.json')
token: Optional[str] = Field(description='Plexamp token for server.json')
server: Optional[str] = Field(description='Server url')
index: Optional[int] = Field(description='RCA index')
disabled: Optional[bool] = Field(
description="Soft disable use of this stream. It won't be shown as a selectable option")
ap2: Optional[bool] = Field(description='Is Airplay stream AirPlay2?')
port: Optional[int] = Field(description='Port used by LMS server for metadata listening')
browsable: Optional[bool] = Field(description='Can this stream be browsed?')
temporary: Optional[bool] = Field(description='Will this stream be removed once it is fully disconnected from all sources?')
has_pause: Optional[bool] = Field(description='This stream can be paused, only used on FilePlayers')
# add examples for each type of stream
class Config:
schema_extra = {
'creation_examples': {
'Add Beatles Internet Radio Station': {
'value': {
'logo': 'http://www.beatlesradio.com/content/images/thumbs/0000587.gif',
'name': 'Beatles Radio',
'type': 'internetradio',
'url': 'http://www.beatlesradio.com:8000/stream/1/'
}
},
'Add Classical KING Internet Radio Station': {
'value': {
'logo': 'https://i.iheart.com/v3/re/assets/images/7bcfd87a-de3e-47d0-b896-be0ed38c9d74.png',
'name': 'Classical KING FM 98.1',
'type': 'internetradio',
'url': 'http://classicalking.streamguys1.com/king-fm-aac-iheart'
}
},
'Add Generic DLNA': {
'value': {
'name': 'Replace this text with a name you like!',
'type': 'dlna'
}
},
'Add Groove Salad Internet Radio Station': {
'value': {
'logo': 'https://somafm.com/img3/groovesalad-200.jpg',
'name': 'Groove Salad',
'type': 'internetradio',
'url': 'http://ice2.somafm.com/groovesalad-16-aac'
}
},
'Add KEXP Internet Radio Station': {
'value': {
'logo': 'https://i.iheart.com/v3/re/new_assets/cc4e0a17-5233-4e4b-9b6b-7799904f78ea',
'name': 'KEXP '
'90.3',
'type': 'internetradio',
'url': 'http://live-aacplus-64.kexp.org/kexp64.aac'
}
},
'Add Matt and Kim Pandora Station': {
'value': {
'name': 'Matt and Kim Radio',
'password': 's79sDDkjf',
'station': '4473713754798410236',
'type': 'pandora',
'user': 'test@micro-nova.com',
'browsable': True
}
},
'Add Spotify Connect': {
'value': {
'name': 'AmpliPi',
'type': 'spotify'
}
},
'Add AirPlay': {
'value': {
'name': 'AmpliPi',
'type': 'airplay',
'ap2': True
}
},
"Play single file or announcement": {
'value': {
'name': 'Play NASA Announcement',
'type': 'fileplayer',
'url': 'https://www.nasa.gov/wp-content/uploads/2015/01/640150main_Go20at20Throttle20Up.mp3'
}
},
'Add FM Radio Station': {
'value': {
'name': 'WXYZ',
'type': 'fmradio',
'freq': '100.1',
'logo': 'static/imgs/fmradio.png'
}
},
'Add LMS Client connected specifically to amplipi': {
'value': {
'name': 'Test',
'server': 'localhost',
'type': 'lms',
}
},
'Add LMS Client': {
'value': {
'name': 'Family',
'type': 'lms',
}
},
'Add LMS Client connected specifically to mylmsserver': {
'value': {
'name': 'Family',
'type': 'lms',
'server': 'mylmsserver',
},
},
'Add LMS Client connected specifically to mylmsserver with port specified': {
'value': {
'name': 'Family',
'type': 'lms',
'server': 'mylmsserver',
'port': 9000
},
}
},
'examples': {
'Regina Spektor Radio': {
'value': {
'id': 90890,
'name': 'Regina Spektor Radio',
'password': '',
'station': '4473713754798410236',
'status': 'connected',
'type': 'pandora',
'user': 'example1@micro-nova.com',
'browsable': True
}
},
'Matt and Kim Radio (disconnected)': {
'value': {
'id': 90891,
'info': {'details': 'No info available'},
'name': 'Matt and Kim Radio',
'password': '',
'station': '4610303469018478727',
'status': 'disconnected',
'type': 'pandora',
'user': 'example2@micro-nova.com',
'browsable': True
}
},
'AirPlay (connected)': {
'value': {
'id': 44590,
'info': {'details': 'No info available'},
'name': "Jason's iPhone",
'status': 'connected',
'type': 'airplay',
'browsable': False
}
},
'AirPlay (disconnected)': {
'value': {
'id': 4894,
'info': {'details': 'No info available'},
'name': 'Rnay',
'status': 'disconnected',
'type': 'airplay',
'browsable': False
}
},
}
}
@lru_cache(1)
def optional_stream_fields() -> Set:
""" Extra fields that can be preset in a stream """
model = Stream(id=0, name='', type='fake').dict()
return {k for k, v in model.items() if v is None}
class StreamUpdate(BaseUpdate):
""" Reconfiguration of a Stream """
# TODO: how to support different stream types
user: Optional[str]
password: Optional[str]
station: Optional[str]
url: Optional[str]
logo: Optional[str]
freq: Optional[str]
server: Optional[str]
ap2: Optional[bool] = Field(description='Is Airplay stream AirPlay2?')
disabled: Optional[bool] = Field(
description="Soft disable use of this stream. It won't be shown as a selectable option")
port: Optional[int] = Field(description='Port used by LMS server for metadata listening')
temporary: Optional[bool]
timeout: Optional[str]
has_pause: Optional[bool]
class Config:
schema_extra = {
'examples': {
'Change account info': {
'value': {
'password': 'sd9sk3k30',
'user': 'test@micro-nova.com'
}
},
'Change name': {
'value': {
'name': 'Matt and Kim Radio'
}
},
'Change pandora radio station': {
'value': {
'station': '0982034049300'
}
},
'Upgrade groove salad stream quality': {
'value': {
'url': 'http://ice2.somafm.com/groovesalad-64-aac'
}
}
},
}
class StreamCommand(str, Enum):
PLAY = 'play'
PAUSE = 'pause'
NEXT = 'next'
PREV = 'prev'
STOP = 'stop'
LOVE = 'love'
BAN = 'ban'
SHELVE = 'shelve'
ACTIVATE = 'activate'
DEACTIVATE = 'deactivate'
RESTART = 'restart'
class PresetState(BaseModel):
""" A set of partial configuration changes to make to sources, zones, and groups """
sources: Optional[List[SourceUpdateWithId]]
zones: Optional[List[ZoneUpdateWithId]]
groups: Optional[List[GroupUpdateWithId]]
class Command(BaseModel):
""" A command to execute on a stream """
stream_id: int = Field(description="Stream to execute the command on")
cmd: str = Field(description="Command to execute")
class Preset(Base):
""" A partial controller configuration the can be loaded on demand.
In addition to most of the configuration found in Status, this can contain commands as well that configure the state of different streaming services.
"""
state: Optional[PresetState]
commands: Optional[List[Command]]
last_used: Union[int, None] = None
class Config:
schema_extra = {
'creation_examples': {
'Add Mute All': {
'value': {
'name': 'Mute All',
'state': {
'zones': [
{'id': 0, 'mute': True},
{'id': 1, 'mute': True},
{'id': 2, 'mute': True},
{'id': 3, 'mute': True},
{'id': 4, 'mute': True},
{'id': 5, 'mute': True}
]
}
}
}
},
'examples': {
'Mute All': {
'value': {
'id': 10000,
'name': 'Mute All',
'state': {
'zones': [
{'id': 0, 'mute': True},
{'id': 1, 'mute': True},
{'id': 2, 'mute': True},
{'id': 3, 'mute': True},
{'id': 4, 'mute': True},
{'id': 5, 'mute': True}
]
}
}
}
}
}
class PresetUpdate(BaseUpdate):
""" Changes to a current preset
The contents of state and commands will be completely replaced if populated.
Merging old and new updates seems too complicated and error prone.
"""
state: Optional[PresetState]
commands: Optional[List[Command]]
class Config:
schema_extra = {
'examples': {
'Only mute some': {
'value': {
'name': 'Mute Some',
'state': {
'zones': [
{'id': 0, 'mute': True},
{'id': 1, 'mute': True},
{'id': 2, 'mute': True},
{'id': 5, 'mute': True}
]
}
}
}
}
}
class BrowserSelection(BaseModel):
item: str = Field(description="Identifier of piece of media in browser to play")
class Config:
schema_extra = {
'examples': {
'Select the Music Directory': {
'value': {
'item': '/media/USBStick/Music'
}
}
}
}
class Announcement(BaseModel):
""" A PA-like Announcement
IF no zones or groups are specified, all available zones are used
"""
media: str = Field(description="URL to media to play as the announcement")
vol: Optional[int] = Field(default=None, ge=MIN_VOL_DB, le=MAX_VOL_DB,
description='Output volume in dB, overrides vol_f')
vol_f: float = Field(default=0.5, ge=MIN_VOL_F, le=MAX_VOL_F, description="Output Volume (float)")
source_id: int = Field(default=3, ge=0, le=MAX_SOURCES - 1, description='Source to announce with')
zones: Optional[List[int]] = fields.Zones
groups: Optional[List[int]] = fields.Groups
class Config:
schema_extra = {
'examples': {
'Make NASA Announcement': {
'value': {
'media': 'https://www.nasa.gov/wp-content/uploads/2015/01/640150main_Go20at20Throttle20Up.mp3',
}
}
}
}
class PlayMedia(BaseModel):
""" Plays media on a specified source.
Will return an error if there is no source specified.
"""
media: str = Field(description="URL to media to play")
vol: Optional[int] = Field(default=None, ge=MIN_VOL_DB, le=MAX_VOL_DB,
description='Output volume in dB, overrides vol_f')
vol_f: float = Field(default=None, ge=MIN_VOL_F, le=MAX_VOL_F, description="Output Volume (float)")
source_id: int = Field(default=None, ge=0, le=MAX_SOURCES - 1, description='Source to play media with')
class Config:
schema_extra = {
'examples': {
'Play The Entertainer by Scott Joplin, Arranged by Kevin MacLeod': {
'value': {