forked from giachello/netradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netradio-card.js
316 lines (262 loc) · 10.5 KB
/
netradio-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
// Netradio UI component. Configuration: you can list as many media_player entities as you need, so you can control multiple speakers. The netradio component needs to be installed as well.
//
// type: "custom:netradio-card"
// entities:
// - media_player.my_player
//
// Needs the netradio component to be loaded and configured in configuration.yaml
//
class NetradioCard 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(hass));
this.content.appendChild(this.buildStationList(hass));
}
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('Netradio Card: 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(hass) {
this._stationButtons = [];
const stationList = document.createElement('div');
stationList.classList.add('station-list');
hass.states["netradio.netradio"].attributes.radios.forEach(linkCfg => {
const stationButton = this.buildStationSwitch(linkCfg.name, linkCfg.url, linkCfg.icon);
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;
}
buildVolumeSlider(hass) {
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 mbIcon = document.createElement('ha-icon');
mbIcon.icon = 'hass:volume-high';
muteButton.appendChild(mbIcon);
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 sbIcon = document.createElement('ha-icon');
sbIcon.icon = 'hass:stop';
stopButton.appendChild(sbIcon);
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)
} else {
slider.removeAttribute('hidden');
stopButton.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');
} 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;
muteButton.firstElementChild.icon = 'hass:volume-off';
} else if (!speakerState.is_volume_muted && slider.disabled) {
slider.disabled = false;
muteButton.icon = 'hass:volume-high';
muteButton.isMute = false;
muteButton.firstElementChild.icon = 'hass:volume-high';
}
});
volumeContainer.appendChild(muteButton);
volumeContainer.appendChild(slider);
volumeContainer.appendChild(stopButton);
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
});
}
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);
}
})
}
buildStationSwitch(name, url, icon) {
const btn = document.createElement('mwc-button');
btn.stationUrl = url;
btn.className = 'juke-toggle';
if (icon === undefined) {
btn.innerText = name;
} else
btn.innerHTML = '<img src="' + icon + '" height="32" />';;
btn.addEventListener('click', this.onStationSelect.bind(this));
return btn;
}
onStationSelect(e) {
this.hass.callService('media_player', 'play_media', {
entity_id: this._selectedSpeaker,
media_content_id: e.currentTarget.stationUrl,
media_content_type: 'music'
});
}
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 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;
}
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('netradio-card', NetradioCard);