forked from remkolems/lovelace-flower-card
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflower-card.js
197 lines (182 loc) · 5.73 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
import {FlowerData} from '/local/lovelace-flower-card/data/data.js';
customElements.whenDefined('card-tools').then(() => {
var cardTools = customElements.get('card-tools');
class FlowerCard extends cardTools.LitElement {
async setConfig(config) {
this.config = config;
}
static get styles() {
return cardTools.LitCSS`
ha-card {
margin-top: 32px;
}
.attributes {
white-space: nowrap;
padding: 8px;
}
.attribute ha-icon {
float: left;
margin-right: 4px;
}
.attribute {
display: inline-block;
width: 50%;
white-space: normal;
}
.header {
padding-top: 8px;
height: 72px;
}
.header > img {
border-radius: 50%;
width: 88px;
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;
}
.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: 10%;
}
.meter.green {
width: 50%;
}
.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);
}
.divider {
height: 1px;
background-color: #727272;
opacity: 0.25;
margin-left: 8px;
margin-right: 8px;
} .tooltip {
position: relative;
}
.tooltip:after {
opacity: 0;
visibility: hidden;
position: absolute;
content: attr(data-tooltip);
padding: 6px 10px;
top: 1.4em;
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);
}
.tooltip:hover:after, .tooltip:active:after {
display: block;
opacity: 1;
visibility: visible;
-webkit-transform: translateX(-50%) translateY(-200%);
transform: translateX(-50%) translateY(-200%);
}
`;
}
render() {
const species = this.config.species;
const Flower = FlowerData[species];
if(!this.stateObj)
return cardTools.LitHtml``;
const attribute = (icon, attr, min, max) => {
const unit = this.stateObj.attributes.unit_of_measurement_dict[attr];
const val = this.stateObj.attributes[attr];
const pct = 100*Math.max(0, Math.min(1, (val-min)/(max-min)));
return cardTools.LitHtml`
<div class="attribute tooltip"
data-tooltip="${val + " "+ unit + " | " + min + " ~ " + max + " " + unit}"
@click="${() => cardTools.moreInfo(this.stateObj.attributes.sensors[attr])}">
<ha-icon .icon="${icon}"></ha-icon>
<div class="meter red">
<span
class="${val < min || val > max ? 'bad' : 'good'}"
style="width: 100%;"
></span>
</div>
<div class="meter green">
<span
class="${val > max ? 'bad' : 'good'}"
style="width:${pct}%;"
></span>
</div>
<div class="meter red">
<span
class="bad"
style="width:${val > max ? 100 : 0}%;"
></span>
</div>
</div>
`;
// ${val} (${min}-${max})
}
return cardTools.LitHtml`
<ha-card>
<div class="header"
@click="${() => cardTools.moreInfo(this.stateObj.entity_id)}"
>
<img src="/local/lovelace-flower-card/data/Images/${this.config.species}.jpg">
<span id="name"> ${this.stateObj.attributes.friendly_name} </span>
<span id="species"> ${Flower[0]} </span>
</div>
<div class="divider"></div>
<div class="attributes">
${attribute('mdi:thermometer', 'temperature', Flower[4], Flower[5])}
${attribute('mdi:white-balance-sunny', 'brightness', Flower[2], Flower[3])}
</div>
<div class="attributes">
${attribute('mdi:water-percent', 'moisture', Flower[6], Flower[7])}
${attribute('mdi:leaf', 'conductivity', Flower[8], Flower[9])}
</div>
</ha-card>
`;
}
set hass(hass) {
this._hass = hass;
this.stateObj = hass.states[this.config.entity];
this.requestUpdate();
}
}
customElements.define('flower-card', FlowerCard);
});
window.setTimeout(() => {
if(customElements.get('card-tools')) return;
customElements.define('flower-card', class extends HTMLElement{
setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");}
});
}, 2000);