-
Notifications
You must be signed in to change notification settings - Fork 0
/
light-card.js
74 lines (63 loc) · 2.28 KB
/
light-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
class LightCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.delay;
}
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define an entity');
}
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const card = document.createElement('ha-card');
const content = document.createElement('paper-button');
card.appendChild(content);
const bulbIcon = document.createElement('ha-icon');
bulbIcon.style.display = 'block';
bulbIcon.style.margin = 'auto';
const cardConfig = Object.assign({}, config);
bulbIcon.style.width = `${cardConfig.size ? cardConfig.size : "40%"}`;
bulbIcon.style.height = `${cardConfig.size ? cardConfig.size : "40%"}`;
if (!cardConfig.bulb_icon){
cardConfig.bulb_icon = 'mdi:lightbulb';
}
bulbIcon.icon = cardConfig.bulb_icon;
content.addEventListener('click', event => {
this._fire('hass-more-info', { entityId: cardConfig.entity });
});
content.appendChild(bulbIcon);
root.appendChild(card);
this._config = cardConfig;
}
set hass(hass) {
const entityId = this._config.entity;
const state = hass.states[entityId];
const stateStr = state ? state.state : 'unavailable';
if (state.attributes.rgb_color) {
this.shadowRoot.children[0].children[0].children[0].style.color = `rgb(${state.attributes.rgb_color.join(',')})`;
} else if (state.state === 'on') {
this.shadowRoot.children[0].children[0].children[0].style.color = `rgb(255, 218, 109)`;
}
if (state.state === 'off') {
this.shadowRoot.children[0].children[0].children[0].style.color = "var(--disabled-text-color)";
}
}
_fire(type, detail, options) {
const node = this.shadowRoot;
options = options || {};
detail = (detail === null || detail === undefined) ? {} : detail;
const event = new Event(type, {
bubbles: options.bubbles === undefined ? true : options.bubbles,
cancelable: Boolean(options.cancelable),
composed: options.composed === undefined ? true : options.composed
});
event.detail = detail;
node.dispatchEvent(event);
return event;
}
getCardSize() {
return 3;
}
}
customElements.define('light-card', LightCard);