forked from tagcashdev/hatc-gauge-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatc-gauge-card-ext.js
419 lines (381 loc) · 17.1 KB
/
hatc-gauge-card-ext.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
var LitElement = LitElement || Object.getPrototypeOf(customElements.get("ha-panel-lovelace"));
var html = LitElement.prototype.html;
var css = LitElement.prototype.css;
function isObject(val) {
return val instanceof Object;
}
function calcPercent(sValue, sMin, sMax) {
var result;
if (sValue >= 0) {
result = sValue / sMax * 100;
} else {
result = sValue / sMin * 100;
}
result = Math.trunc(result);
if (result >= 75) {
return result - 1;
}
return result;
}
function calcStatePercent(sValue, sMax) {
if (isNaN(sValue)) {
return '';
}
var result = sValue / sMax * 100;
result = Math.trunc(result);
return result;
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function handleClick(node, hass, config, entityId) {
let e;
if (!config) {
return;
}
// eslint-disable-next-line default-case
switch (config.action) {
case 'more-info': {
e = new Event('hass-more-info', { composed: true });
e.detail = {
entityId: config.entity || entityId,
};
node.dispatchEvent(e);
break;
}
case 'navigate': {
if (!config.navigation_path) return;
window.history.pushState(null, '', config.navigation_path);
e = new Event('location-changed', { composed: true });
e.detail = { replace: false };
window.dispatchEvent(e);
break;
}
case 'call-service': {
if (!config.service) return;
const [domain, service] = config.service.split('.', 2);
const serviceData = { ...config.service_data };
hass.callService(domain, service, serviceData);
break;
}
case 'url': {
if (!config.url) return;
window.location.href = config.url;
}
case 'toggle': {
hass.callService(config.service, "toggle", {
entity_id: config.entity || entityId
});
}
}
}
class HatcGaugeCardExt extends LitElement {
static get properties() {
return {
hass: {},
config: {}
};
}
static getConfigElement() {
console.log('getConfigElement');
}
static getStubConfig() {
return { entity: "sun.sun" }
}
// Whenever the state changes, a new `hass` object is set. Use this to
// update your content.
render() {
const hassEntity = this.hass.states[this.config.entity];
if (hassEntity) {
var showTitleEntity = true;
var showStateEntity = true;
var showIconEntity = true;
var showUnitOfMeasurmentEntity = true;
var icon;
var textstateColor;
if (typeof hassEntity.attributes.icon !== 'undefined') {
icon = hassEntity.attributes.icon;
} else {
// icon par défaut selon device_class si l'attribut icon est vide
if (typeof hassEntity.attributes.device_class !== 'undefined') {
switch (hassEntity.attributes.device_class) {
case 'power':
icon = 'mdi:flash';
break;
case 'temperature':
icon = 'mdi:thermometer';
break;
}
} else {
// icon par défaut selon le prefix si l'attribut et device_class est vide
if (hassEntity.entity_id.startsWith('light')) {
icon = 'mdi:lightbulb';
}
}
}
// Card config
var c = {}; var cCard = (typeof this.config.card !== 'undefined') ? this.config.card : '';
if (isObject(cCard)) {
c['height'] = (typeof cCard['height'] !== 'undefined') ? "height:" + cCard['height'] + "; " : '';
}
// Title config
var h = {}; var hTitle = (typeof this.config.title !== 'undefined') ? this.config.title : hassEntity.attributes.friendly_name;
if (isObject(hTitle)) {
h['fontsize'] = (typeof hTitle['font-size'] !== 'undefined') ? "--mdc-icon-size:" + hTitle['font-size'] + "; font-size:" + hTitle['font-size'] + "; " : '';
if (typeof hTitle['text-align'] !== 'undefined') {
if (hTitle['text-align'] == 'left') {
h['textalign'] = "justify-content: flex-start";
} else if (hTitle['text-align'] == 'right') {
h['textalign'] = "justify-content: flex-end";
} else {
h['textalign'] = "justify-content: center";
}
}
h['color'] = (typeof hTitle['text-color'] !== 'undefined') ? hTitle['text-color'] : '';
h['iconcolor'] = (typeof hTitle['icon-color'] !== 'undefined') ? hTitle['icon-color'] : '';
h['name'] = (typeof hTitle.name !== 'undefined') ? hTitle.name : hassEntity.attributes.friendly_name;
h['icon'] = (typeof hTitle.icon !== 'undefined') ? hTitle.icon : icon;
} else {
h['name'] = hTitle;
if (hTitle === '' || hTitle === false || hTitle === 'hide') {
h['icon'] = hTitle;
}
}
var heTitle = showTitleEntity ? hassEntity.attributes.friendly_name : '';
var heUnitOfMeasurement = showUnitOfMeasurmentEntity ? hassEntity.entity_id.startsWith('light') && (typeof hassEntity.attributes.brightness == 'number') ? '%' : hassEntity.attributes.unit_of_measurement : '';
var heState = showStateEntity ? hassEntity.entity_id.startsWith('light') ? calcStatePercent(hassEntity.attributes.brightness, 254) : hassEntity.state : '';
var heIcon = showIconEntity ? (typeof h.icon !== 'undefined' ? h.icon : icon) : '';
// Gauge config
var g = {}; var hGauge = (typeof this.config.gauge !== 'undefined') ? this.config.gauge : '';
if (isObject(hGauge)) {
g['textstatecolor'] = (typeof hGauge['text-color'] !== 'undefined') ? hGauge['text-color'] : '';
g['iconcolor'] = (typeof hGauge['icon-color'] !== 'undefined') ? hGauge['icon-color'] : g.textstateColor;
g['fontsize'] = (typeof hGauge['font-size'] !== 'undefined') ? hGauge['font-size'] : '22px';
g['iconsize'] = (typeof hGauge['icon-size'] !== 'undefined') ? hGauge['icon-size'] : g.fontsize;
g['friendlyname'] = (typeof hGauge['friendly_name'] !== 'undefined') ? hGauge['friendly_name'] : heTitle;
g['unitofmeasurement'] = (typeof hGauge['unit_of_measurement'] !== 'undefined') ? hGauge['unit_of_measurement'] : heUnitOfMeasurement;
g['maxvalue'] = (typeof hGauge['max_value'] !== 'undefined') ? hGauge['max_value'] : '100';
g['minvalue'] = (typeof hGauge['min_value'] !== 'undefined') ? hGauge['min_value'] : '-100';
g['state'] = (typeof hGauge['state'] !== 'undefined') ? hGauge['state'] : true;
console.log("heIcon", heIcon);
g['icon'] = (typeof hGauge['icon'] !== 'undefined') ? hGauge['icon'] : (heIcon !== '' && heIcon !== false && heIcon !== 'hide') ? heIcon : icon;
// Severity config
if (typeof this.config.gauge.severity !== 'undefined') {
this.config.gauge.severity.map(s => {
if (typeof s.form === 'undefined' && typeof s.to === 'undefined' && typeof s.color !== 'undefined') {
textstateColor = s.color;
if (typeof s.icon !== 'undefined') {
g.icon = s.icon;
}
}
if (parseFloat(heState) >= s.from && parseFloat(heState) <= s.to) {
textstateColor = s.color;
if (typeof s.icon !== 'undefined') {
g.icon = s.icon;
}
}
});
if (g['textstatecolor'] == "severity" || g['textstatecolor'] == "") {
g['textstatecolor'] = textstateColor;
}
if (h.iconcolor == "severity") {
h.iconcolor = textstateColor;
}
if (h.color == "severity") {
h.color = textstateColor;
}
if (h.icon == "severity") {
heIcon = g.icon;
}
} else {
textstateColor = 'white';
}
} else {
g['textstatecolor'] = '';
g['iconcolor'] = '';
g['fontsize'] = '22px';
g['iconsize'] = g.fontsize;
g['friendlyname'] = heTitle;
g['unitofmeasurement'] = heUnitOfMeasurement;
g['maxvalue'] = '100';
g['minvalue'] = '0';
g['state'] = true;
g['icon'] = (heIcon !== '' && heIcon !== false && heIcon !== 'hide') ? heIcon : icon;
textstateColor = 'white';
}
var heTextStateColor = g['textstatecolor'];
var hePathStrokeColor = textstateColor;
if (hassEntity.entity_id.startsWith('light')) {
hePathStrokeColor = heTextStateColor = 'rgb(' + hassEntity.attributes.rgb_color + ')';
}
var uID = makeid(2);
//hePathStrokeColor = 'url(#MyGradient'+uID+')';
var hE = {
"heTitle": (g.friendlyname !== '' && g.friendlyname !== false && g.friendlyname !== 'hide') ? g.friendlyname : '',
"heState": heState,
"heUnitOfMeasurement": (g.unitofmeasurement !== '' && g.unitofmeasurement !== false && g.unitofmeasurement !== 'hide') ? g.unitofmeasurement : '',
"heIcon": heIcon,
"hePathStrokeColor": hePathStrokeColor,
"heTextStateColor": heTextStateColor,
}
var hIconHTML = (hE.heIcon !== '' && hE.heIcon !== false && hE.heIcon !== 'hide') ? html`<ha-icon style="${h.fontsize} color:${h.iconcolor};" .icon="${hE.heIcon}"></ha-icon>` : '';
var hNameHTML = (h.name !== '' && h.name !== false && h.name !== 'hide') ? html`<div style="${h.fontsize} color:${h.color};" class="name">${h.name}</div>` : '';
var hEntitiesHeight = (h.name !== '' && h.name !== false && h.name !== 'hide') ? 'calc(100% - 40px)' : '100%';
var gStateHTML = (g.state !== '' && g.state !== false && g.state !== 'hide') ? html`${hE.heState} ${hE.heUnitOfMeasurement}` : '';
var gIconHTML = (g.icon !== '' && g.icon !== false && g.icon !== 'hide') ? html`<ha-icon style="--mdc-icon-size: ${g.iconsize}; color:${g.iconcolor};" .icon="${g.icon}"></ha-icon>` : '';
var percent = calcPercent(hE.heState, g.minvalue, g.maxvalue);
var transform = hE.heState < 0 ? "transform: rotateY(180deg);" : "";
var gaugeHTML = html`
<svg viewBox="0 0 36 36" style="max-width: 100%; max-height: 100%; ${transform}">
<path style="fill: none; stroke: var(--primary-background-color); stroke-width: 2.0;"
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" />
<rect x="0" y="0" width="1" height="4" fill="var(--primary-background-color)" transform="rotate(180 9.25 1.5)" />
<path style="stroke: ${hE.hePathStrokeColor}; fill: none; stroke-width: 2.8; stroke-linecap: round; animation: progress 1s ease-out forwards;"
stroke-dasharray="${percent}, 100"
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" />
</svg>
`;
if (percent > 100) {
return html`
<div class="maxValueExceeded">There seems to be a problem, please add a min_value or max_value!</div>
`;
} else {
return html`
<ha-card class="HatcGaugeCardExt">
<div class="box" style="${c.height}" @click=${e => this._handlePopup(e)}>
<div class="header" style="${h.textalign}">
${hIconHTML}
${hNameHTML}
</div>
<div class="entities" style="height: ${hEntitiesHeight};">
<div class="outer">
<div class="inner" style="color: ${hE.heTextStateColor}; font-size: ${g.fontsize};">
${gIconHTML}
<div class="datas">
${gStateHTML}
</div>
</div>
</div>
${gaugeHTML}
</div>
</div>
</ha-card>
`;
}
} else {
return html`
<div class="not-found">The entity "${this.config.entity}" was not found.</div>
`;
}
}
// The user supplied configuration. Throw an exception and Home Assistant
// will render an error card.
setConfig(config) {
if (!config.entity) {
throw new Error('Please add an "entity" in your configuration');
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return this.config.entities.length + 1;
}
_toggle(state, service) {
this.hass.callService(service, "toggle", {
entity_id: state.entity_id
});
}
_handlePopup(e) {
var tap_action = this.config.tap_action || {};
if (this.config.entity) {
if (typeof this.config.tap_action === 'undefined') {
tap_action = {
action: "more-info"
}
} else {
if (typeof this.config.tap_action.service === 'undefined') {
tap_action = {
service: "homeassistant",
...tap_action
}
}
}
e.stopPropagation();
handleClick(this, this.hass, tap_action, this.config.entity);
}
}
/* _handleEntities(e, entity) {
var ent = entity || {};
if (!ent['tap_action']) {
ent = {
tap_action: {
action: "more-info",
...ent
}
}
}
e.stopPropagation();
handleClick(this, this.hass, this.config.tap_action, false);
} */
static get styles() {
return css`
:root, .HatcGaugeCardExt *{
--mdc-icon-size: 16px;
--card-padding: 8px;
}
.HatcGaugeCardExt .box{
padding: var(--card-padding);
}
.HatcGaugeCardExt .box div.name{
color: var(--secondary-text-color);
line-height: 40px;
font-weight: 500;
font-size: 16px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.HatcGaugeCardExt .box .header{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-content: center;
justify-content: flex-start;
align-items: center;
}
.HatcGaugeCardExt .box .entities{
position: relative;
}
.HatcGaugeCardExt .box .entities .outer .inner{
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
align-content: center;
position: absolute;
top: 0;
left: 0;
font-size: 22px;
}
.HatcGaugeCardExt .box .entities svg{
display: block;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
text-align: center;
position: relative;
height: 100%;
}
`;
}
}
customElements.define('hatc-gauge-card-ext', HatcGaugeCardExt);