forked from Olen/lovelace-flower-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflower-card.js
544 lines (514 loc) · 17.2 KB
/
flower-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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import {
LitElement,
html,
css,
} from "https://unpkg.com/[email protected]/lit-element.js?module";
import {unsafeHTML} from 'https://unpkg.com/lit-html@latest/directives/unsafe-html.js?module';
export const fireEvent = (node, type, detail) => {
detail = detail === null || detail === undefined ? {} : detail;
const event = new Event(type, {
bubbles: true,
cancelable: false,
composed: true
});
event.detail = detail;
node.dispatchEvent(event);
return event;
};
const default_show_bars = [
"moisture",
"conductivity",
"temperature",
"illuminance",
"humidity",
"dli",
];
const missingImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaWRZTWlkIG1lZXQiIGZvY3VzYWJsZT0iZmFsc2UiIHJvbGU9ImltZyIgYXJpYS1oaWRkZW49InRydWUiIHZpZXdCb3g9IjAgMCAyNCAyNCI+CiAgICAgIDxnPgogICAgICA8IS0tP2xpdCQ0MTM0MjMxNjkkLS0+PHBhdGggZD0iTTMsMTNBOSw5IDAgMCwwIDEyLDIyQzEyLDE3IDcuOTcsMTMgMywxM00xMiw1LjVBMi41LDIuNSAwIDAsMSAxNC41LDhBMi41LDIuNSAwIDAsMSAxMiwxMC41QTIuNSwyLjUgMCAwLDEgOS41LDhBMi41LDIuNSAwIDAsMSAxMiw1LjVNNS42LDEwLjI1QTIuNSwyLjUgMCAwLDAgOC4xLDEyLjc1QzguNjMsMTIuNzUgOS4xMiwxMi41OCA5LjUsMTIuMzFDOS41LDEyLjM3IDkuNSwxMi40MyA5LjUsMTIuNUEyLjUsMi41IDAgMCwwIDEyLDE1QTIuNSwyLjUgMCAwLDAgMTQuNSwxMi41QzE0LjUsMTIuNDMgMTQuNSwxMi4zNyAxNC41LDEyLjMxQzE0Ljg4LDEyLjU4IDE1LjM3LDEyLjc1IDE1LjksMTIuNzVDMTcuMjgsMTIuNzUgMTguNCwxMS42MyAxOC40LDEwLjI1QzE4LjQsOS4yNSAxNy44MSw4LjQgMTYuOTcsOEMxNy44MSw3LjYgMTguNCw2Ljc0IDE4LjQsNS43NUMxOC40LDQuMzcgMTcuMjgsMy4yNSAxNS45LDMuMjVDMTUuMzcsMy4yNSAxNC44OCwzLjQxIDE0LjUsMy42OUMxNC41LDMuNjMgMTQuNSwzLjU2IDE0LjUsMy41QTIuNSwyLjUgMCAwLDAgMTIsMUEyLjUsMi41IDAgMCwwIDkuNSwzLjVDOS41LDMuNTYgOS41LDMuNjMgOS41LDMuNjlDOS4xMiwzLjQxIDguNjMsMy4yNSA4LjEsMy4yNUEyLjUsMi41IDAgMCwwIDUuNiw1Ljc1QzUuNiw2Ljc0IDYuMTksNy42IDcuMDMsOEM2LjE5LDguNCA1LjYsOS4yNSA1LjYsMTAuMjVNMTIsMjJBOSw5IDAgMCwwIDIxLDEzQzE2LDEzIDEyLDE3IDEyLDIyWiI+PC9wYXRoPgogICAgICA8L2c+Cjwvc3ZnPgo=";
class FlowerCard extends LitElement {
static getConfigElement() {
return document.createElement("flower-card-editor");
}
static getStubConfig(ha) {
const supportedEntities = Object.values(ha.states).filter(
(entity) => entity.entity_id.indexOf('plant.') === 0
);
const entity = supportedEntities.length > 0 ? supportedEntities[0].entity_id : 'plant.my_plant';
return {
entity: entity,
battery_sensor: "sensor.myflower_battery",
water_automation: null,
show_bars: default_show_bars
}
}
// The user supplied configuration. Throw an exception and Home Assistant
// will render an error card.
setConfig(config) {
if (!config.entity) {
throw new Error("You need to define an entity");
}
this.config = config;
}
set hass(hass) {
this._hass = hass;
this.stateObj = hass.states[this.config.entity];
if (!this.prev_fetch) {
this.prev_fetch = 0;
}
// Only fetch once every second at max. HA is flooeded with websocket requests
if (Date.now() > this.prev_fetch + 1000) {
this.prev_fetch = Date.now();
this.get_data(hass).then(() => {
this.requestUpdate();
});
}
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 5;
}
// Use websocket to get plant-data
async get_data(hass) {
try {
this.plantinfo = await hass.callWS({
type: "plant/get_info",
entity_id: this.config.entity,
});
} catch (err) {}
}
moreInfo(entityId = this.config.entity) {
fireEvent(
this,
'hass-more-info',
{
entityId,
},
{
bubbles: false,
composed: true,
}
);
}
runWaterAutomation() {
this._hass.callService("automation", "trigger", {
entity_id: this.config.water_automation,
});
}
static get styles() {
return css`
ha-card {
margin-top: 32px;
}
.attributes {
white-space: nowrap;
padding: 8px;
}
.attribute ha-icon {
vertical-align: middle;
display: inline-grid;
}
.attribute {
display: inline-block;
width: 50%;
vertical-align: middle;
white-space: nowrap;
}
#battery, #water {
float: right;
margin-right: 16px;
margin-top: -15px;
}
.header {
padding-top: 8px;
height: 72px;
}
.attribute .header {
height: auto;
}
.header > img {
border-radius: 50%;
width: 88px;
height: 88px;
object-fit: cover;
margin-left: 16px;
margin-right: 16px;
margin-top: -32px;
float: left;
box-shadow: var( --ha-card-box-shadow, 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2) );
}
.header > #name {
font-weight: bold;
width: 100%;
margin-top: 16px;
text-transform: capitalize;
display: block;
}
#name ha-icon {
color: rgb(240, 163, 163);
}
.header > #species {
text-transform: capitalize;
color: #8c96a5;
display: block;
}
.meter {
height: 8px;
background-color: #f1f1f1;
border-radius: 2px;
display: inline-grid;
overflow: hidden;
}
.meter.red {
width: 5%;
}
.meter.green {
width: 40%;
}
.meter > span {
grid-row: 1;
grid-column: 1;
height: 100%;
}
.meter > .good {
background-color: rgba(43,194,83,1);
}
.meter > .bad {
background-color: rgba(240,163,163);
}
.meter > .unavailable {
background-color: rgba(158,158,158,1);
}
.divider {
height: 1px;
background-color: #727272;
opacity: 0.25;
margin-left: 8px;
margin-right: 8px;
}
.tooltip {
position: relative;
}
.tooltip .tip {
opacity: 0;
visibility: hidden;
position: absolute;
padding: 6px 10px;
top: 3.3em;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-180%);
transform: translateX(-50%) translateY(-180%);
background: grey;
color: white;
white-space: nowrap;
z-index: 2;
border-radius: 2px;
transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), -webkit-transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1);
transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1);
transition: opacity 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1), -webkit-transform 0.2s cubic-bezier(0.64, 0.09, 0.08, 1);
}
.battery.tooltip .tip {
top: 2em;
}
.tooltip:hover .tip, .tooltip:active .tip {
display: block;
opacity: 1;
visibility: visible;
-webkit-transform: translateX(-50%) translateY(-200%);
transform: translateX(-50%) translateY(-200%);
}
@media (max-width: 600px) {
.header > .unit {
display: none;
}
}
`;
}
render() {
if (
!this.config ||
!this._hass
) {
// console.log(this.config);
// console.log(this.stateObj);
// console.log(this._hass);
return null;
}
// console.log(this.config);
// console.log(this._hass);
// const stateObj = this.hass.states[this.config.entity];
if (!this.stateObj) {
return html`
<hui-warning>
Entity not available: ${this.config.entity}
</hui-warning>
`;
}
const species = this.stateObj.attributes.species;
var icons = {};
var uom = {};
var uomt = {};
var limits = {};
var curr = {};
var sensors = {};
var displayed = [];
var monitored = this.config.show_bars || default_show_bars;
const battery_sensor = this.config.battery_sensor || null;
const water_automation = this.config.water_automation || null;
if (this.plantinfo && this.plantinfo["result"]) {
const result = this.plantinfo["result"];
for (var i = 0; i < monitored.length; i++) {
let elem = monitored[i];
if (result[elem]) {
limits["max_" + elem] = result[elem].max;
limits["min_" + elem] = result[elem].min;
curr[elem] = result[elem].current;
icons[elem] = result[elem].icon;
sensors[elem] = result[elem].sensor;
uomt[elem] = result[elem].unit_of_measurement;
uom[elem] = result[elem].unit_of_measurement;
if (elem == "dli") {
uomt["dli"] = "mol/d⋅m²";
uom["dli"] = '<math style="display: inline-grid;" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mfrac><mrow><mn>mol</mn></mrow><mrow><mn>d</mn><mn>⋅</mn><msup><mn>m</mn><mn>2</mn></msup></mrow></mfrac></mrow></math>';
}
displayed.push(elem);
}
}
}
const attribute = (attr) => {
const min = parseFloat(limits["min_" + attr]);
const max = parseFloat(limits["max_" + attr]);
const unit = uom[attr];
// console.log(html`${unsafeHTML(unit)}`);
const unitTooltip = uomt[attr];
const icon = icons[attr] || "mdi:help-circle-outline";
var val = parseFloat(curr[attr]);
if (isNaN(val)) {
var aval = false;
var pct = 0;
val = "";
} else {
var aval = true;
var pct = 100 * Math.max(0, Math.min(1, (val - min) / (max - min)));
}
var toolTipText = aval ? attr + ": " + val + " " + unitTooltip + '<br>(' + min + " ~ " + max +" " + unitTooltip + ")"
: this._hass.localize('state.default.unavailable');
return html`
<div class="attribute tooltip" @click="${() => this.moreInfo(sensors[attr])}">
<div class="tip" style="text-align:center;">${unsafeHTML(toolTipText)}</div>
<ha-icon .icon="${icon}"></ha-icon>
<div class="meter red">
<span class="${
aval ? (val < min || val > max ? "bad" : "good") : "unavailable"
}" style="width: 100%;"></span>
</div>
<div class="meter green">
<span class="${
aval ? (val > max ? "bad" : "good") : "unavailable"
}" style="width:${aval ? pct : "0"}%;"></span>
</div>
<div class="meter red">
<span class="bad" style="width:${
aval ? (val > max ? 100 : 0) : "0"
}%;"></span>
</div>
<span class="header"><span class="value">${val}</span> <span class="unit">${unsafeHTML(unit)}</span></span>
</div>
`;
};
const water = () => {
if (water_automation) {
var icon = "mdi:watering-can";
var water_color = "green";
var value = "Water this plant";
return html`
<div class="water tooltip">
<div class="tip" style="text-align:center;">${value}</div>
<ha-icon .icon="${icon}" style="color: ${water_color}"></ha-icon>
</div>
`;
} else {
return html``;
}
};
const battery = () => {
if (battery_sensor) {
if (this._hass.states[battery_sensor]) {
var value = this._hass.states[battery_sensor].state + '%';
switch (true) {
case this._hass.states[battery_sensor].state > 90:
var icon = "mdi:battery";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 80:
var icon = "mdi:battery-90";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 70:
var icon = "mdi:battery-80";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 60:
var icon = "mdi:battery-70";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 50:
var icon = "mdi:battery-60";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 40:
var icon = "mdi:battery-50";
var battery_color = "green";
break;
case this._hass.states[battery_sensor].state > 30:
var icon = "mdi:battery-40";
var battery_color = "orange";
break;
case this._hass.states[battery_sensor].state > 20:
var icon = "mdi:battery-30";
var battery_color = "orange";
break;
case this._hass.states[battery_sensor].state > 10:
var icon = "mdi:battery-20";
var battery_color = "red";
break;
case this._hass.states[battery_sensor].state > 0:
var icon = "mdi:battery-10";
var battery_color = "red";
break;
case this._hass.states[battery_sensor].state == 0:
var icon = "mdi:battery-alert-variant-outline";
var battery_color = "red";
break;
default:
var icon = "mdi:battery-off-outline";
var battery_color = "rgba(158,158,158,1)";
var value = this._hass.localize('state.default.unavailable');
break;
}
} else {
var icon = "mdi:battery-off-outline";
var battery_color = "rgba(158,158,158,1)";
var value = this._hass.localize('state.default.unavailable');
}
return html`
<div class="battery tooltip">
<div class="tip" style="text-align:center;">${value}</div>
<ha-icon .icon="${icon}" style="color: ${battery_color}"></ha-icon>
</div>
`;
} else {
return html``;
}
};
return html`
<ha-card>
<div class="header">
<img src="${
this.stateObj.attributes.entity_picture
? this.stateObj.attributes.entity_picture
: missingImage
}" @click="${() =>
this.moreInfo(this.stateObj.entity_id)}">
<span id="name" @click="${() =>
this.moreInfo(this.stateObj.entity_id)}"> ${this.stateObj.attributes.friendly_name
} <ha-icon .icon="mdi:${this.stateObj.state.toLowerCase() == "problem"
? "alert-circle-outline"
: ""
}"></ha-icon>
</span>
<span id="battery">${battery()}</span>
<span id="water" @click="${() =>
this.runWaterAutomation()}">${water()}</span>
<span id="species">${species} </span>
</div>
<div class="divider"></div>
<div class="attributes">
${displayed[0] == undefined ? void 0 : attribute(displayed[0])}
${displayed[1] == undefined ? void 0 : attribute(displayed[1])}
</div>
<div class="attributes">
${displayed[2] == undefined ? void 0 : attribute(displayed[2])}
${displayed[3] == undefined ? void 0 : attribute(displayed[3])}
</div>
<div class="attributes">
${displayed[4] == undefined ? void 0 : attribute(displayed[4])}
${displayed[5] == undefined ? void 0 : attribute(displayed[5])}
</div>
</ha-card>
`;
}
}
class ContentCardEditor extends LitElement {
static get properties() {
return {
hass: {},
_config: {},
};
}
setConfig(config) {
this._config = config;
}
set hass(hass) {
this._hass = hass;
}
_valueChanged(ev) {
// console.log("ValueChanged");
// console.log(ev);
if (!this._config || !this._hass) {
return;
}
const _config = Object.assign({}, this._config);
_config.entity = ev.detail.value.entity;
_config.battery_sensor = ev.detail.value.battery_sensor;
_config.show_bars = ev.detail.value.show_bars;
this._config = _config;
const event = new CustomEvent("config-changed", {
detail: { config: _config },
bubbles: true,
composed: true,
});
this.dispatchEvent(event);
}
render() {
// console.log("Render");
// console.log(this._config);
if (!this._hass || !this._config) {
return html``;
}
if (!this._config.hasOwnProperty('show_bars')) {
// Enable all bars by default
this._config.show_bars = default_show_bars;
}
return html`
<ha-form
.hass=${this._hass}
.data=${this._config}
.schema=${[
{name: "entity", selector: { entity: { domain: "plant" } }},
{name: "battery_sensor", selector: { entity: { device_class: "battery" } }},
{name: "water_automation", selector: { entity: { domain: "automation" } }},
{name: "show_bars", selector: { select: { multiple: true, mode: "list", options: [
{label: "Moisture", value: "moisture"},
{label: "Conductivity", value: "conductivity"},
{label: "Temperature", value: "temperature"},
{label: "Illuminance", value: "illuminance"},
{label: "Humidity", value: "humidity"},
{label: "Daily Light Integral", value: "dli"}
]}
}}
]}
.computeLabel=${this._computeLabel}
@value-changed=${this._valueChanged}
></ha-form>
`;
}
}
customElements.define("flower-card", FlowerCard);
customElements.define("flower-card-editor", ContentCardEditor);
window.customCards = window.customCards || [];
window.customCards.push({
type: "flower-card",
name: "Flower Card",
preview: true, // Optional - defaults to false
description: "Custom flower card for https://github.com/Olen/homeassistant-plant", // Optional
});