-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
model.js
executable file
·346 lines (285 loc) · 7.48 KB
/
model.js
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
import { PROGRESS_PROPS, MEDIA_INFO } from './const';
export default class MediaPlayerObject {
constructor(hass, config, entity) {
this.hass = hass || {};
this.config = config || {};
this.entity = entity || {};
this.state = entity.state;
this.attr = entity.attributes;
this.idle = config.idle_view ? this.idleView : false;
this.active = this.isActive;
}
get id() {
return this.entity.entity_id;
}
get icon() {
return this.attr.icon;
}
get isPaused() {
return this.state === 'paused';
}
get isPlaying() {
return this.state === 'playing';
}
get isIdle() {
return this.state === 'idle';
}
get isStandby() {
return this.state === 'standby';
}
get isUnavailable() {
return this.state === 'unavailable';
}
get isOff() {
return this.state === 'off';
}
get isActive() {
return (!this.isOff
&& !this.isUnavailable
&& !this.idle) || false;
}
get shuffle() {
return this.attr.shuffle || false;
}
get content() {
return this.attr.media_content_type || 'none';
}
get mediaDuration() {
return this.attr.media_duration || 0;
}
get updatedAt() {
return this.attr.media_position_updated_at || 0;
}
get position() {
return this.attr.media_position || 0;
}
get name() {
return this.attr.friendly_name || '';
}
get groupCount() {
return this.group.length;
}
get isGrouped() {
return this.group.length > 1;
}
get group() {
const groupName = `${this.config.speaker_group.platform}_group`;
return this.attr[groupName] || [];
}
get master() {
return this.group[0] || this.config.entity;
}
get isMaster() {
return this.master === this.config.entity;
}
get sources() {
return this.attr.source_list || [];
}
get source() {
return this.attr.source || '';
}
get soundModes() {
return this.attr.sound_mode_list || [];
}
get soundMode() {
return this.attr.sound_mode || '';
}
get muted() {
return this.attr.is_volume_muted || false;
}
get vol() {
return this.attr.volume_level || 0;
}
get picture() {
return this.attr.entity_picture_local || this.attr.entity_picture;
}
get hasArtwork() {
return (this.picture
&& this.config.artwork !== 'none'
&& this.active
&& !this.idle);
}
get mediaInfo() {
return MEDIA_INFO.map(item => ({
text: this.attr[item.attr],
prefix: '',
...item,
})).filter(item => item.text);
}
get hasProgress() {
return !this.config.hide.progress
&& !this.idle
&& PROGRESS_PROPS.every(prop => prop in this.attr);
}
get progress() {
return this.position + (Date.now() - new Date(this.updatedAt).getTime()) / 1000.0;
}
get idleView() {
const idle = this.config.idle_view;
if (idle.when_idle && this.isIdle
|| idle.when_standby && this.isStandby
|| idle.when_paused && this.isPaused)
return true;
// TODO: remove?
if (!this.updatedAt || !idle.after || this.isPlaying)
return false;
return this.checkIdleAfter(idle.after);
}
get trackIdle() {
return (
this.active
&& !this.isPlaying
&& this.updatedAt
&& this.config.idle_view
&& this.config.idle_view.after
);
}
checkIdleAfter(time) {
const diff = (Date.now() - new Date(this.updatedAt).getTime()) / 1000;
this.idle = diff > time * 60;
this.active = this.isActive;
return this.idle;
}
get supportsShuffle() {
return !(typeof this.attr.shuffle === 'undefined');
}
get supportsMute() {
return !(typeof this.attr.is_volume_muted === 'undefined');
}
getAttribute(attribute) {
return this.attr[attribute] || '';
}
get artwork() {
return `url(${this.attr.entity_picture_local ? this.hass.hassUrl(this.picture) : this.picture})`;
}
toggle(e) {
if (this.config.toggle_power)
return this.callService(e, 'toggle');
if (this.isOff)
return this.callService(e, 'turn_on');
else
this.callService(e, 'turn_off');
}
toggleMute(e) {
if (this.config.speaker_group.sync_volume) {
this.group.forEach((entity) => {
this.callService(e, 'volume_mute', {
entity_id: entity,
is_volume_muted: !this.muted,
});
});
} else {
this.callService(e, 'volume_mute', { is_volume_muted: !this.muted });
}
}
toggleShuffle(e) {
this.callService(e, 'shuffle_set', { shuffle: !this.shuffle });
}
setSource(e, source) {
this.callService(e, 'select_source', { source });
}
setMedia(e, opts) {
this.callService(e, 'play_media', { ...opts });
}
playPause(e) {
this.callService(e, 'media_play_pause');
}
playStop(e) {
if (!this.isPlaying)
this.callService(e, 'media_play');
else
this.callService(e, 'media_stop');
}
setSoundMode(e, name) {
this.callService(e, 'select_sound_mode', { sound_mode: name });
}
next(e) {
this.callService(e, 'media_next_track');
}
prev(e) {
this.callService(e, 'media_previous_track');
}
stop(e) {
this.callService(e, 'media_stop');
}
volumeUp(e) {
this.callService(e, 'volume_up');
}
volumeDown(e) {
this.callService(e, 'volume_down');
}
seek(e, pos) {
this.callService(e, 'media_seek', { seek_position: pos });
}
setVolume(e, vol) {
if (this.config.speaker_group.sync_volume) {
this.group.forEach((entity) => {
const conf = this.config.speaker_group.entities.find(entry => (entry.entity_id === entity))
|| {};
let offsetVol = vol;
if (conf.volume_offset) {
offsetVol += (conf.volume_offset / 100);
if (offsetVol > 1) offsetVol = 1;
if (offsetVol < 0) offsetVol = 0;
}
this.callService(e, 'volume_set', {
entity_id: entity,
volume_level: offsetVol,
});
});
} else {
this.callService(e, 'volume_set', {
entity_id: this.config.entity,
volume_level: vol,
});
}
}
handleGroupChange(e, entity, checked) {
const { platform } = this.config.speaker_group;
const options = { entity_id: entity };
if (checked) {
options.master = this.config.entity;
if (platform === 'bluesound') {
return this.callService(e, `${platform}_JOIN`, options);
}
if (platform === 'soundtouch') {
const service = this.isGrouped ? 'ADD_ZONE_SLAVE' : 'CREATE_ZONE';
return this.handleSoundtouch(e, service, entity);
}
this.callService(e, 'join', options, platform);
} else {
if (platform === 'bluesound') {
return this.callService(e, `${platform}_UNJOIN`, options);
}
if (platform === 'soundtouch') {
return this.handleSoundtouch(e, 'REMOVE_ZONE_SLAVE', entity);
}
this.callService(e, 'unjoin', options, platform);
}
}
handleSoundtouch(e, service, entity) {
return this.callService(e, service, {
master: this.master,
slaves: entity,
}, 'soundtouch', true);
}
toggleScript(e, id, data = {}) {
this.callService(e, id.split('.').pop(), {
...data,
}, 'script');
}
toggleService(e, id, data = {}) {
e.stopPropagation();
const [domain, service] = id.split('.');
this.hass.callService(domain, service, {
...data,
});
}
callService(e, service, inOptions, domain = 'media_player', omit = false) {
e.stopPropagation();
this.hass.callService(domain, service, {
...(!omit && { entity_id: this.config.entity }),
...inOptions,
});
}
}