forked from fineemb/lovelace-remote-card
-
Notifications
You must be signed in to change notification settings - Fork 6
/
remote-card.js
497 lines (487 loc) · 15.6 KB
/
remote-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
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
class RemoteCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
set hass(hass) {
this._hass = hass;
this._entity = this.config.entity;
if (['not_home', 'off'].includes(this._hass.states[this._entity].state)) {
if (this.hacard && !this.hacard.classList.contains('not_home')) {
this.hacard.classList.add('not_home')
}
}
}
// 自定义默认配置
static getStubConfig() {
const defaultEntity = 'media_player.xiao_mi_dian_shi'
const defaultRemote = 'remote.xiao_mi_dian_shi'
return {
vibrate: true,
entity: defaultEntity,
circle: {
ok: {
service: 'remote.send_command',
data: {
command: 'enter',
entity_id: defaultRemote
}
},
up: {
service: 'remote.send_command',
data: {
command: 'up',
entity_id: defaultRemote
}
},
down: {
service: 'remote.send_command',
data: {
command: 'down',
entity_id: defaultRemote
}
}, left: {
service: 'remote.send_command',
data: {
command: 'left',
entity_id: defaultRemote
}
}, right: {
service: 'remote.send_command',
data: {
command: 'right',
entity_id: defaultRemote
}
}
},
right_buttons: [
{
icon: 'mdi:power',
service: 'remote.send_command',
data: {
command: 'power',
entity_id: defaultRemote
},
},
{
icon: 'mdi:keyboard-return',
service: 'remote.send_command',
data: {
command: 'back',
entity_id: defaultRemote
}
},
{
icon: 'mdi:home',
service: 'remote.send_command',
data: {
command: 'home',
entity_id: defaultRemote
}
},
{
icon: 'mdi:menu',
service: 'remote.send_command',
data: {
command: 'menu',
entity_id: defaultRemote
}
},
{
icon: 'mdi:volume-minus',
service: 'remote.send_command',
data: {
command: 'volumedown',
entity_id: defaultRemote
}
},
{
icon: 'mdi:volume-plus',
service: 'remote.send_command',
data: {
command: 'volumeup',
entity_id: defaultRemote
}
},
],
bottom_buttons: [
{
icon: 'mdi:music-box',
service: 'media_player.select_source',
data: {
source: 'QQ音乐',
entity_id: defaultEntity
},
},
{
icon: 'mdi:movie-open-settings',
service: 'media_player.select_source',
data: {
source: '银河奇异果',
entity_id: defaultEntity
},
},
{
icon: 'mdi:cat',
service: 'media_player.select_source',
data: {
source: 'CIBN酷喵',
entity_id: defaultEntity
},
},
{
icon: 'mdi:cloud',
service: 'media_player.select_source',
data: {
source: '云视听极光',
entity_id: defaultEntity
},
},
{
icon: 'mdi:kodi',
service: 'media_player.select_source',
data: {
source: 'Kodi',
entity_id: defaultEntity
},
},
]
}
}
setConfig(config) {
if (!config.entity) {
throw new Error('你需要定义一个实体');
}
this.config = deepClone(config);
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const style = document.createElement('style');
style.textContent = this._cssData();
root.appendChild(style);
this.hacard = document.createElement('ha-card');
this.hacard.className = 'f-ha-card';
this.hacard.innerHTML = this._htmlData();
root.appendChild(this.hacard);
const $ = this.hacard.querySelector.bind(this.hacard);
// 环形按钮
Object.keys(this.config.circle).forEach(key => {
const ele = $(`#l${key}`)
let value = this.config.circle[key]
ele.onclick = () => {
if (typeof value === 'string') {
this.selectMode(value)
} else {
this.selectMode(value.service, value.data)
}
}
})
// 左侧按钮
if (this.config.right_buttons) {
this.config.right_buttons.forEach(function (button) {
let buttonBox = document.createElement('paper-button');
buttonBox.innerHTML = `
<div class="lbicon">
<ha-icon class="ha-icon" data-state="on" icon="`+ button.icon + `"></ha-icon>
</div>
`;
buttonBox.addEventListener('click', (e) => {
if ('entity' in button) {
this.selectMode(button.entity)
} else {
this.selectMode(button.service, button.data)
}
}, false);
this.hacard.querySelector("#right_buttons").appendChild(buttonBox)
}, this)
}
// 底部按钮
if (this.config.bottom_buttons) {
this.config.bottom_buttons.forEach(function (button) {
let buttonBox = document.createElement('paper-button');
buttonBox.innerHTML = `
<div class="lbicon">
<ha-icon class="ha-icon" data-state="on" icon="`+ button.icon + `"></ha-icon>
</div>
`;
buttonBox.addEventListener('click', (e) => {
if ('entity' in button) {
this.selectMode(button.entity)
} else {
this.selectMode(button.service, button.data)
}
}, false);
this.hacard.querySelector("#bottom_buttons").appendChild(buttonBox)
}, this)
}
// 底部文本输入框
// if(this.config.)
const input = this.hacard.querySelector('#bottom_input input')
input.onkeydown = (event)=>{
if(event.keyCode == 13){
const text = input.value.trim()
console.log(text)
input.value = ''
}
}
}
selectMode(service_name, data = {}) {
var arr = service_name.split('.');
var domain = arr[0];
var service = arr[1];
this._hass.callService(domain, service, data)
// 震动
if (this.config.vibrate && navigator.vibrate) {
navigator.vibrate(50)
}
}
_htmlData() {
var html = `
<div id="remote" class="remote_f">
<div class="box">
<div class="scale">
<div class="button-group">
<div class="outter-circle">
<div class="inner-parts up">
<div class="iconbox">
<div class="ficon"></div>
</div>
<div id="lup" class="tap up"></div>
</div>
<div class="inner-parts right">
<div class="iconbox">
<div class="ficon"></div>
</div>
<div id="lright" class="tap right"></div>
</div>
<div class="inner-parts left">
<div class="iconbox">
<div class="ficon"></div>
</div>
<div id="lleft" class="tap left"></div>
</div>
<div class="inner-parts down">
<div class="iconbox">
<div class="ficon"></div>
</div>
<div id="ldown" class="tap down"></div>
</div>
<div id="lok" class="inner-circle ok">
</div>
</div>
</div>
</div>
</div>
<div class="boxb">
<div id="right_buttons">
</div>
</div>
</div>
<div id="bottom_buttons"></div>
<div id="bottom_input">
<input type="text" />
</div>
`
return html;
}
_cssData() {
var css = `
#remote{
display:flex;
flex-wrap: wrap;
justify-content: center;
}
.f-ha-card{
padding: 1pc;
background: var(--paper-card-background-color);
--box-shadow:2px 2px 5px rgba(0, 0, 0, 0.3);
--button-shadow-color:#00bcd4;
--state-color-off: gray;
}
.box {
padding: 5px;
overflow: hidden;
flex:1.25;
display: flex;
align-items: center;
min-width: 165px;
}
.boxb {
flex:1;
min-width: 145px;
}
.scale {
width: 100%;
padding-bottom: 100%;
height: 0;
position: relative;
}
.button-group {
width: 100%;
height: 100%;
position: absolute;
}
.outter-circle {
position: relative;
width: 100%;
height: 100%;
transform-origin: center;
transform: rotate(45deg);
}
.inner-parts {
float: left;
width: 49.5%;
height:49.5%;
background-color: var(--card-color-off);
opacity: 7.5;
box-sizing: border-box;
border: 1px #ffffff17 solid;
box-shadow: var(--box-shadow) ;
}
.up{
margin:0 0.5% 0.5% 0;
border-top-left-radius: 100%;
}
.right{
margin:0 0 0.5% 0.5%;
border-top-right-radius: 100%;
}
.left{
margin:0.5% 0.5% 0 0 ;
border-bottom-left-radius: 100%;
}
.down{
margin:0.5% 0 0 0.5% ;
border-bottom-right-radius: 100%;
}
.inner-circle {
position: absolute;
margin-top: 28%;
margin-left: 28%;
width: 44%;
height:44%;
border-radius: 100%;
background-image: var(--card-color-off);
background: var(--paper-card-background-color);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3) ;
box-sizing: border-box;
border: 1px #ffffff17 solid;
}
.inner-circle:active{
background-image: var(--card-color-off);
box-shadow: 0px 0px 5px var(--accent-color) inset;
}
.rotate {
display: inline-block;
transform: rotate(-45deg);
width: 100%;
height:100%;
line-height: 90px;
}
.iconbox{
position: relative;
display: block;
width: 5%;
color: #FFFFFF;
height: 5%;
margin: 47%;
}
.ficon{
width: 100%;
color: #FFFFFF;
height: 100%;
vertical-align: middle;
border-radius: 50%;
background-size: cover;
background-color: var(--accent-color);
text-align: center;
box-shadow: 1px 1px 6px rgba(0, 0, 0, 0.6);
}
.tap{
position: relative;
width: 100%;
height: 100%;
top: -100%;
left: 0;
}
.tap:active{
box-shadow: 2px 2px 5px var(--accent-color);
}
#right_buttons,
#bottom_buttons {
display: flex;
flex-wrap: wrap;
align-content: center;
min-height: 100%;
justify-content: center;
}
paper-button {
text-align: center;
padding: 5px;
border-radius: 50%;
margin: 10px;
color: var(--accent-color);
box-shadow: var(--box-shadow);
box-sizing: border-box;
border: 1px #ffffff17 solid;
}
paper-button:active{
box-shadow: 0 0 5px var(--accent-color);
}
.lbicon {
cursor: pointer;
position: relative;
display: inline-block;
width: 40px;
color: var(--accent-color);
height: 40px;
text-align: center;
background-size: cover;
line-height: 40px;
vertical-align: middle;
border-radius: 50%;
}
.not_home .lbicon {
color: var(--state-color-off);
}
.not_home .ficon {
background-color: var(--state-color-off);
}
#bottom_input{ text-align: center;}
#bottom_input input{
width: 80%;
border-radius: 10px;
border: 1px solid #ddd;
padding: 10px;
margin-top: 10px;
}
`
return css;
}
getCardSize() {
return 1;
}
}
function deepClone(value) {
if (!(!!value && typeof value == 'object')) {
return value;
}
if (Object.prototype.toString.call(value) == '[object Date]') {
return new Date(value.getTime());
}
if (Array.isArray(value)) {
return value.map(deepClone);
}
var result = {};
Object.keys(value).forEach(
function (key) { result[key] = deepClone(value[key]); });
return result;
}
customElements.define('lovelace-remote-card', RemoteCard);
// 添加预览
window.customCards = window.customCards || [];
window.customCards.push({
type: "lovelace-remote-card",
name: "遥控器面板",
preview: true,
description: "遥控器卡片"
});