-
Notifications
You must be signed in to change notification settings - Fork 4
/
heatzy-pilote-card.js
318 lines (279 loc) · 8.76 KB
/
heatzy-pilote-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
import {
LitElement,
html,
css
} from "https://unpkg.com/lit-element@2.0.1/lit-element.js?module";
const translation = {
en: {
modes: {
"none": "Off",
"away": "Away",
"eco": "Eco",
"comfort": "Comfort"
},
hvacmodes: {
"auto": "Sched",
"off": "Off",
"heat": "On"
}
},
fr: {
modes: {
"none": "Off",
"away": "Hors-gel",
"eco": "Eco",
"comfort": "Confort"
},
hvacmodes: {
"auto": "Prog",
"off": "Off",
"heat": "On"
}
}
};
const MODES = [ // https://cdn.materialdesignicons.com/7.1.96/
{ name: "none", icon: "mdi:minus-circle", style: "heat_selected_none" },
{ name: "comfort", icon: "mdi:white-balance-sunny", style: "heat_selected_comfort" },
{ name: "eco", icon: "mdi:weather-night", style: "heat_selected_eco" },
{ name: "away", icon: "mdi:snowflake", style: "heat_selected_away" }
];
const HVACMODES = [ // https://cdn.materialdesignicons.com/7.1.96/
{ name: "off", icon: "mdi:minus-circle", style: "heat_selected_none" },
{ name: "comfort", icon: "mdi:white-balance-sunny", style: "heat_selected_comfort" },
{ name: "eco", icon: "mdi:weather-night", style: "heat_selected_eco" },
{ name: "away", icon: "mdi:snowflake", style: "heat_selected_away" }
];
// heat modes for darkMode
const MODES_dark = MODES.map(({ ...x }) => { x.style = x.style + "_dark"; return x })
const HVACMODES_dark = HVACMODES.map(({ ...x }) => { x.style = x.style + "_dark"; return x })
class HeatzyPiloteCard extends LitElement {
render() {
return html`
<ha-card>
${this._getTitle()}
${this._getWarnings()}
<div class="content has-header">
${this._getContent()}
</div>
</ha-card>
`;
}
_getTitle() {
if (this.config.title) {
return html`<h2 class="card-header">${this.config.title}</h2>`;
}
return html``;
}
_getContent() {
return this.config.elements.map(elt => {
const darkMode = this.hass.themes.darkMode;
const ent = elt.entity;
const name = elt.friendly_name ? elt.friendly_name : this._inferName(ent);
const temp = this._getTemperature(elt.temp_sensor, 1);
const stateObj = this.hass.states[ent];
const hvac_mode = stateObj.state // off, auto, heat
const hvac_action = stateObj.attributes.hvac_action; // off, heating
const preset_mode = stateObj.attributes.preset_mode; // (null), comfort, eco, away
const preset_mode_tr = this._getPresetModeTranslation(preset_mode);
const hvac_mode_tr = this._getHvacModeTranslation(hvac_mode);
let translation;
let modeList;
let modeSelected;
if (hvac_mode != 'off') {
translation = hvac_mode_tr;
modeList = darkMode ? MODES_dark : MODES;
modeSelected = preset_mode;
} else {
translation = preset_mode_tr;
modeList = darkMode ? HVACMODES_dark : HVACMODES;
modeSelected = hvac_mode;
};
return stateObj ?
html`<div class="state">
<h4 class="heat_name">${name} ${temp}</h4>
<span class="heat_icon_list">
${translation} ${this._getIconList(modeList, modeSelected, stateObj.entity_id)}
</span>
</div>`:
html`<div class="not-found">Entity '${ent}' not found.</div>`;
});
}
_getIconList(modes_list, mode_selected, entity_id) {
return modes_list.map(x => {
const darkMode = this.hass.themes.darkMode;
x.heat_class = x.name == mode_selected ? 'heat_selected' : '';
const classSelected = x.name == mode_selected ? x.style : x.heat_class;
const classDark = darkMode ? "heat_icon_dark" : "heat_icon";
const xx = html`<ha-icon class="${classDark} ${classSelected}" icon="${x.icon}"
@click="${e => this._handleClick(entity_id, x.name)}"></ha-icon>`;
return (xx);
});
}
_getPresetModeTranslation(preset_mode) {
const language = this.config.language;
return translation[language].modes[preset_mode];
}
_getHvacModeTranslation(hvac_mode) {
const language = this.config.language;
return translation[language].hvacmodes[hvac_mode];
}
_inferName(x) {
x = x.replace("climate.", "");
return x.charAt(0).toUpperCase() + x.slice(1);
}
_getTemperature(sensor, digits) {
const temp = this.hass.states[sensor];
if (sensor != undefined && temp == undefined) { //sensor in unknown
return (html`(?°C)`);
}
if (sensor) {
const temperature = parseFloat(temp.state).toFixed(digits);
return (html`(${temperature}${temp.attributes.unit_of_measurement})`);
}
return (html``);
}
_handleClick(entity_id, clicked_mode) {
if (clicked_mode == 'none') {
this.hass.callService('climate', 'set_hvac_mode', {
entity_id: entity_id,
hvac_mode: 'off'
});
} else {
this.hass.callService('climate', 'set_preset_mode', {
entity_id: entity_id,
preset_mode: clicked_mode
});
}
}
_getWarnings() {
const elts = this.config.elements
//check: entity is a climate with a "preset mode" attribute / ajouté si hvac_action off
for (let i = 0; i < elts.length; i++) {
const elt = elts[i];
const entity = this.hass.states[elt.entity];
if (entity.attributes.hvac_action != 'off') {
if (entity.attributes.preset_mode == undefined) {
const name = elt.friendly_name ? html`("${elt.friendly_name}") ` : html``;
return html`<hui-warning>Entity "${elt.entity}" of element #${i + 1} ${name}is not a climate entity with a "preset_mode" attribute</hui-warning>`;
};
}
}
//check: temperature sensor is not unknown
for (let i = 0; i < elts.length; i++) {
const elt = elts[i];
const temp = this.hass.states[elt.temp_sensor];
if (elt.temp_sensor != undefined && temp == undefined) {
const name = elt.friendly_name ? html`("${elt.friendly_name}") ` : html``;
return html`<hui-warning>Sensor "${elt.temp_sensor}" of element #${i + 1} ${name}is unknown</hui-warning>`;
}
}
return html``;
}
setConfig(config) {
//elements must be defined
if (!config.elements) {
throw new Error("You need to define elements");
}
//each element must have an entity
for (let i = 0; i < config.elements.length; i++) {
const elt = config.elements[i];
if (!elt.entity) {
throw new Error("You need to define a climate entity for element #" + i);
}
}
//translation is supported
if (config.language == undefined) {
config.language = "en"
} else if (translation[config.language] == undefined) {
throw new Error(`Supported languages are only ["en", "fr"]. "${config.language}" is not a supported language.`);
}
this.config = config;
}
getCardSize() {
return this.config.elements.length + 1;
}
// CSS styles for light/dark modes
static get styles() {
return css`
.state {
display: flex;
justify-content: space-between;
padding: 8px;
align-items: center;
}
.state h1 {
margin:0;
margin-left:25px;
}
.state h2 {
margin:0;
margin-left:25px;
}
.state h3 {
margin:0;
margin-left:15px;
}
.state h4 {
margin:0;
margin-left:15px;
}
.heat_icon_list {
float:right;
}
.card-header {
padding-bottom: 5px;
}
.has-header {
padding-top: 0px;
}
.content {
padding: 16px;
padding-top: 0;
}
.heat_selected{
color: green !important;
}
.heat_selected_none{
color: #003333 !important;
}
.heat_selected_away{
color: #3366CC !important;
}
.heat_selected_eco{
color: #003333 !important;
}
.heat_selected_comfort{
color: #CC3333 !important;
}
.heat_selected_none_dark{
color: #cecece !important;
}
.heat_selected_away_dark{
color: #488FC2 !important;
}
.heat_selected_eco_dark{
color: #0F9D58 !important;
}
.heat_selected_comfort_dark{
color: #CC3333 !important;
}
.heat_icon{
color: #CCCCCC;
cursor: pointer;
margin-left: 10px;
}
.heat_icon_dark{
color: #6F6F6F;
cursor: pointer;
margin-left: 10px;
}
`;
}
static get properties() {
return {
hass: {},
config: {}
};
}
}
customElements.define('heatzy-pilote-card', HeatzyPiloteCard);