forked from lukx/home-assistant-jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjukebox-card.js
401 lines (337 loc) · 12.7 KB
/
jukebox-card.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
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
console.info(`%c HOME-ASSISTANT-JUKEBOX %c v2021_11_09_v2`, 'color: orange; font-weight: bold; background: black', 'color: white; font-weight: bold; background: dimgray');
class JukeboxCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
this._hassObservers = [];
this.appendChild(getStyle());
const card = document.createElement('ha-card');
this.content = document.createElement('div');
card.appendChild(this.content);
this.appendChild(card);
this.content.appendChild(this.buildSpeakerSwitches(hass));
this.content.appendChild(this.buildVolumeSlider());
this.content.appendChild(this.buildSongDisplay());
this.content.appendChild(this.buildStationList());
}
this._hass = hass;
this._hassObservers.forEach(listener => listener(hass));
}
get hass() {
return this._hass;
}
buildSpeakerSwitches(hass) {
this._tabs = document.createElement('paper-tabs');
this._tabs.setAttribute('scrollable', true);
this._tabs.addEventListener('iron-activate', (e) => this.onSpeakerSelect(e.detail.item.entityId));
this.config.entities.forEach(entityId => {
if (!hass.states[entityId]) {
console.log('Jukebox: No State for entity', entityId);
return;
}
this._tabs.appendChild(this.buildSpeakerSwitch(entityId, hass));
});
// automatically activate the first speaker that's playing
const firstPlayingSpeakerIndex = this.findFirstPlayingIndex(hass);
this._selectedSpeaker = this.config.entities[firstPlayingSpeakerIndex];
this._tabs.setAttribute('selected', firstPlayingSpeakerIndex);
return this._tabs;
}
buildStationList() {
this._stationButtons = [];
const stationList = document.createElement('div');
stationList.classList.add('station-list');
this.config.links.forEach(linkCfg => {
const stationButton = this.buildStationSwitch(linkCfg.name, linkCfg.url, linkCfg.logo, linkCfg.song)
this._stationButtons.push(stationButton);
stationList.appendChild(stationButton);
});
// make sure the update method is notified of a change
this._hassObservers.push(this.updateStationSwitchStates.bind(this));
return stationList;
}
buildSongDisplay() {
this._song = document.createElement('div');
this._song.className = 'song-content';
this._hassObservers.push(this.updateSongState.bind(this));
return this._song;
}
buildVolumeSlider() {
const volumeContainer = document.createElement('div');
volumeContainer.className = 'volume center horizontal layout';
const muteButton = document.createElement('ha-icon-button');
muteButton.icon = 'hass:volume-high';
muteButton.isMute = false;
muteButton.addEventListener('click', this.onMuteUnmute.bind(this));
const muteButtonIcon = document.createElement('ha-icon');
muteButtonIcon.icon = 'hass:volume-high';
muteButton.appendChild(muteButtonIcon);
const slider = document.createElement('ha-slider');
slider.min = 0;
slider.max = 100;
slider.addEventListener('change', this.onChangeVolumeSlider.bind(this));
slider.className = 'flex';
const stopButton = document.createElement('ha-icon-button')
stopButton.icon = 'hass:stop';
stopButton.setAttribute('disabled', true);
stopButton.addEventListener('click', this.onStop.bind(this));
const stopButtonIcon = document.createElement('ha-icon')
stopButtonIcon.icon = 'hass:stop';
stopButton.appendChild(stopButtonIcon);
const powerButton = document.createElement('ha-icon-button')
powerButton.icon = 'hass:power';
powerButton.setAttribute('disabled', true);
powerButton.addEventListener('click', this.onPower.bind(this));
const powerButtonIcon = document.createElement('ha-icon')
powerButtonIcon.icon = 'hass:power';
powerButton.appendChild(powerButtonIcon);
this._hassObservers.push(hass => {
if (!this._selectedSpeaker || !hass.states[this._selectedSpeaker]) {
return;
}
const speakerState = hass.states[this._selectedSpeaker].attributes;
// no speaker level? then hide mute button and volume
if (!speakerState.hasOwnProperty('volume_level')) {
slider.setAttribute('hidden', true);
stopButton.setAttribute('hidden', true);
powerButton.setAttribute('hidden', true);
this._song.setAttribute('hidden', true);
} else {
slider.removeAttribute('hidden');
stopButton.removeAttribute('hidden');
powerButton.removeAttribute('hidden');
this._song.removeAttribute('hidden');
}
if (!speakerState.hasOwnProperty('is_volume_muted')) {
muteButton.setAttribute('hidden', true);
} else {
muteButton.removeAttribute('hidden');
}
if (hass.states[this._selectedSpeaker].state === 'playing') {
stopButton.removeAttribute('disabled');
powerButton.removeAttribute('disabled');
} else {
stopButton.setAttribute('disabled', true);
}
slider.value = speakerState.volume_level ? speakerState.volume_level * 100 : 0;
if (speakerState.is_volume_muted && !slider.disabled) {
slider.disabled = true;
muteButton.icon = 'hass:volume-off';
muteButton.isMute = true;
} else if (!speakerState.is_volume_muted && slider.disabled) {
slider.disabled = false;
muteButton.icon = 'hass:volume-high';
muteButton.isMute = false;
}
});
volumeContainer.appendChild(muteButton);
volumeContainer.appendChild(slider);
volumeContainer.appendChild(stopButton);
volumeContainer.appendChild(powerButton);
return volumeContainer;
}
onSpeakerSelect(entityId) {
this._selectedSpeaker = entityId;
this._hassObservers.forEach(listener => listener(this.hass));
}
onChangeVolumeSlider(e) {
const volPercentage = parseFloat(e.currentTarget.value);
const vol = (volPercentage > 0 ? volPercentage / 100 : 0);
this.setVolume(vol);
}
onMuteUnmute(e) {
this.hass.callService('media_player', 'volume_mute', {
entity_id: this._selectedSpeaker,
is_volume_muted: !e.currentTarget.isMute
});
}
onStop(e) {
this.hass.callService('media_player', 'media_stop', {
entity_id: this._selectedSpeaker
});
delete this._songEntity;
this._song.innerHTML = '';
}
onPower(e) {
this.hass.callService('media_player', 'turn_off', {
entity_id: this._selectedSpeaker
});
delete this._songEntity;
this._song.innerHTML = '';
}
updateStationSwitchStates(hass) {
let playingUrl = null;
const selectedSpeaker = this._selectedSpeaker;
if (hass.states[selectedSpeaker] && hass.states[selectedSpeaker].state === 'playing'){
playingUrl = hass.states[selectedSpeaker].attributes.media_content_id;
}
this._stationButtons.forEach(stationSwitch =>
{
if (stationSwitch.hasAttribute('raised') && stationSwitch.stationUrl !== playingUrl)
{
stationSwitch.removeAttribute('raised');
return;
}
if (!stationSwitch.hasAttribute('raised') && stationSwitch.stationUrl === playingUrl)
{
stationSwitch.setAttribute('raised', true);
// Show song entity state for selected station if available
if (stationSwitch.stationSong)
this._songEntity = stationSwitch.stationSong;
}
})
}
updateSongState(hass)
{
if (this._songEntity)
{
if (this._song.innerHTML != hass.states[this._songEntity].state)
{
this._song.innerHTML = hass.states[this._songEntity].state;
}
} else
{
this._song.innerHTML = '';
}
}
buildStationSwitch(name, url, logo, song) {
const btn = document.createElement('mwc-button');
btn.stationUrl = url;
btn.stationName = name;
btn.stationLogo = logo;
btn.stationSong = song;
btn.className = 'juke-toggle';
btn.innerText = name;
btn.addEventListener('click', this.onStationSelect.bind(this));
return btn;
}
onStationSelect(e) {
delete this._songEntity;
this._song.innerHTML = '';
this.hass.callService('media_player', 'play_media', {
entity_id: this._selectedSpeaker,
media_content_id: e.currentTarget.stationUrl,
media_content_type: 'music',
extra: {
metadata: {
stream_type: "LIVE",
metadataType: 3,
title: e.currentTarget.stationName,
artist: "Live Radio",
images: [
{ url: e.currentTarget.stationLogo }
]
}
}
});
// Force play for 10 seconds every half a second
var that = this;
var i = 0;
for (i = 500; i <= 5000; i=i+500)
{
setTimeout(function() {
that.hass.callService('media_player', 'media_play', {
entity_id: that._selectedSpeaker
}); }, i);
}
}
setVolume(value) {
this.hass.callService('media_player', 'volume_set', {
entity_id: this._selectedSpeaker,
volume_level: value
});
}
/***
* returns the numeric index of the first entity in a "Playing" state, or 0 (first index).
*
* @param hass
* @returns {number}
* @private
*/
findFirstPlayingIndex(hass) {
return Math.max(0, this.config.entities.findIndex(entityId => {
return hass.states[entityId] && hass.states[entityId].state === 'playing';
}));
}
buildSpeakerSwitch(entityId, hass) {
const entity = hass.states[entityId];
const btn = document.createElement('paper-tab');
btn.entityId = entityId;
btn.innerText = hass.states[entityId].attributes.friendly_name;
return btn;
}
setConfig(config) {
if (!config.entities) {
throw new Error('You need to define your media player entities');
}
this.config = config;
}
getCardSize() {
return 3;
}
}
function pauseBrowser(millis)
{
var date = Date.now();
var curDate = null;
do {
curDate = Date.now();
} while (curDate-date < millis);
}
function getStyle() {
const frag = document.createDocumentFragment();
const included = document.createElement('style');
included.setAttribute('include', 'iron-flex iron-flex-alignment');
const ownStyle = document.createElement('style');
ownStyle.innerHTML = `
.layout.horizontal, .layout.vertical {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
.layout.horizontal {
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
}
.layout.center, .layout.center-center {
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.flex {
ms-flex: 1 1 0.000000001px;
-webkit-flex: 1;
flex: 1;
-webkit-flex-basis: 0.000000001px;
flex-basis: 0.000000001px;
}
[hidden] {
display: none !important;
}
.volume {
padding: 10px 20px;
}
.song-content {
text-align: center;
color: green;
font-weight: bold;
}
mwc-button.juke-toggle {
--mdc-theme-primary: var(--primary-text-color);
}
mwc-button.juke-toggle[raised] {
--mdc-theme-primary: var(--primary-color);
background-color: var(--primary-color);
color: var(--text-primary-color);
}
paper-tabs {
background-color: var(--primary-color);
color: var(--text-primary-color);
--paper-tabs-selection-bar-color: var(--text-primary-color, #FFF);
}
`;
frag.appendChild(included);
frag.appendChild(ownStyle);
return frag;
}
customElements.define('jukebox-card', JukeboxCard);