forked from tcarlsen/lovelace-light-with-profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light-with-profiles.js
155 lines (134 loc) · 4.05 KB
/
light-with-profiles.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
import { LitElement, html, css } from "https://unpkg.com/[email protected]/lit-element.js?module";
class LightWithProfiles extends LitElement {
static get properties() {
return {
hass: {},
config: {}
};
}
constructor() {
super();
this.lightProfiles = {};
}
render() {
return html`
<ha-card>
${this.config.title ? html`
<div class="card-header">
<div class="name">${this.config.title}</div>
</div>
` : ''}
<div class="card-content entities">
${this.config.entities.map(ent => {
const stateObj = this.hass.states[ent.entity];
return stateObj
? html`
<span class="label">${stateObj.attributes.friendly_name}</span>
<div class="profiles">
${ent.profiles ? ent.profiles.map(profile => {
return html`
<ha-icon class="profile-icon"
?active="${this.profileClass(stateObj, profile.name)}"
.icon="${profile.icon}"
.title="${profile.name}"
@click="${() => this.turnOnProfile(ent.entity, profile.name)}"
></ha-icon>
`;
}) : ''}
</div>
<paper-toggle-button
?checked="${stateObj.state === 'on'}"
@click="${() => this.toggleLight(ent.entity)}"
></paper-toggle-button>
`
: 'Entity not found!';
})}
</div>
</ha-card>
`;
}
toggleLight(entity) {
this.hass.callService("homeassistant", "toggle", {
entity_id: entity
});
}
turnOnProfile(entity, pro) {
this.hass.callService('light', 'turn_on', {
'entity_id': entity,
'profile': pro
});
}
profileClass(stateObj, profile) {
if (stateObj.attributes.xy_color && stateObj.attributes.brightness) {
const activeProfile = `${stateObj.attributes.xy_color.toString()},${stateObj.attributes.brightness.toString()}`;
if (activeProfile === this.lightProfiles[profile]) {
return true;
}
}
return false;
}
setConfig(config) {
if (!config.entities) {
throw new Error("You need to define entities");
}
const ll = this.getLovelace();
if (ll.config && ll.config.light_profiles) {
this.lightProfiles = ll.config.light_profiles;
}
this.config = config;
}
getCardSize() {
return this.config.entities.length + 1;
}
static get styles() {
return css`
.entities {
display: grid;
grid-template-columns: auto auto 46px;
gap: 16px 10px;
margin-top: 8px;
}
.label {
font-size: 1.2rem;
font-weight: 500;
}
.profiles {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-around;
align-content: stretch;
align-items: flex-start;
}
.profile-icon {
cursor: pointer;
fill: var(--disabled-text-color);
}
.profile-icon[active] {
fill: var(--primary-color);
}
paper-toggle-button {
cursor: pointer;
}
`;
}
// https://github.com/custom-cards/custom-card-helpers/blob/master/src/get-lovelace.ts
getLovelace() {
let root = document.querySelector('home-assistant');
root = root && root.shadowRoot;
root = root && root.querySelector('home-assistant-main');
root = root && root.shadowRoot;
root = root && root.querySelector('app-drawer-layout partial-panel-resolver');
root = root && root.shadowRoot || root;
root = root && root.querySelector('ha-panel-lovelace');
root = root && root.shadowRoot;
root = root && root.querySelector('hui-root');
if (root) {
const ll = root.lovelace;
ll.current_view = root.___curView;
return ll;
}
return null;
}
}
customElements.define('light-with-profiles', LightWithProfiles);