|
|
@@ -1,249 +0,0 @@
|
|
1
|
|
-import {
|
|
2
|
|
- LitElement, html
|
|
3
|
|
-} from 'https://unpkg.com/@polymer/lit-element@^0.5.2/lit-element.js?module';
|
|
4
|
|
-
|
|
5
|
|
-class CircleSensorCard extends LitElement {
|
|
6
|
|
- static get properties() {
|
|
7
|
|
- return {
|
|
8
|
|
- hass: Object,
|
|
9
|
|
- config: Object,
|
|
10
|
|
- state: Object,
|
|
11
|
|
- dashArray: String
|
|
12
|
|
- }
|
|
13
|
|
- }
|
|
14
|
|
-
|
|
15
|
|
- _render({ state, dashArray, config }) {
|
|
16
|
|
- return html`
|
|
17
|
|
- <style>
|
|
18
|
|
- :host {
|
|
19
|
|
- cursor: pointer;
|
|
20
|
|
- }
|
|
21
|
|
-
|
|
22
|
|
- .container {
|
|
23
|
|
- position: relative;
|
|
24
|
|
- height: 100%;
|
|
25
|
|
- display: flex;
|
|
26
|
|
- flex-direction: column;
|
|
27
|
|
- }
|
|
28
|
|
-
|
|
29
|
|
- .labelContainer {
|
|
30
|
|
- position: absolute;
|
|
31
|
|
- top: 0;
|
|
32
|
|
- left: 0;
|
|
33
|
|
- width: 100%;
|
|
34
|
|
- height: 100%;
|
|
35
|
|
- display: flex;
|
|
36
|
|
- flex-direction: column;
|
|
37
|
|
- align-items: center;
|
|
38
|
|
- justify-content: center;
|
|
39
|
|
- }
|
|
40
|
|
-
|
|
41
|
|
- #label {
|
|
42
|
|
- display: flex;
|
|
43
|
|
- line-height: 1;
|
|
44
|
|
- }
|
|
45
|
|
-
|
|
46
|
|
- #label.bold {
|
|
47
|
|
- font-weight: bold;
|
|
48
|
|
- }
|
|
49
|
|
-
|
|
50
|
|
- #label, #name {
|
|
51
|
|
- margin: 1% 0;
|
|
52
|
|
- }
|
|
53
|
|
-
|
|
54
|
|
- .text, #name {
|
|
55
|
|
- font-size: 100%;
|
|
56
|
|
- }
|
|
57
|
|
-
|
|
58
|
|
- .unit {
|
|
59
|
|
- font-size: 75%;
|
|
60
|
|
- }
|
|
61
|
|
-
|
|
62
|
|
- </style>
|
|
63
|
|
- <div class="container" id="container" on-click="${() => this._click()}">
|
|
64
|
|
- <svg viewbox="0 0 200 200" id="svg">
|
|
65
|
|
- <circle id="circle" cx="50%" cy="50%" r="45%"
|
|
66
|
|
- fill$="${config.fill || 'rgba(255, 255, 255, .75)'}"
|
|
67
|
|
- stroke$="${config.stroke_color || '#03a9f4'}"
|
|
68
|
|
- stroke-dasharray$="${dashArray}"
|
|
69
|
|
- stroke-width$="${config.stroke_width || 6}"
|
|
70
|
|
- transform="rotate(-90 100 100)"/>
|
|
71
|
|
- </svg>
|
|
72
|
|
- <span class="labelContainer">
|
|
73
|
|
- ${config.name != null ? html`<span id="name">${config.name}</span>` : ''}
|
|
74
|
|
- <span id="label" class$="${!!config.name ? 'bold' : ''}">
|
|
75
|
|
- <span class="text">
|
|
76
|
|
- ${config.attribute ? state.attributes[config.attribute] : state.state}
|
|
77
|
|
- </span>
|
|
78
|
|
- <span class="unit">
|
|
79
|
|
- ${config.show_max
|
|
80
|
|
- ? html` / ${config.attribute_max ? state.attributes[config.attribute_max] : config.max}`
|
|
81
|
|
- : (config.units ? config.units : state.attributes.unit_of_measurement)}
|
|
82
|
|
- </span>
|
|
83
|
|
- </span>
|
|
84
|
|
- </span>
|
|
85
|
|
- </div>
|
|
86
|
|
- `;
|
|
87
|
|
- }
|
|
88
|
|
-
|
|
89
|
|
- _createRoot() {
|
|
90
|
|
- const shadow = this.attachShadow({ mode: 'open' })
|
|
91
|
|
- if (!this.config.show_card) {
|
|
92
|
|
- return shadow;
|
|
93
|
|
- }
|
|
94
|
|
- const card = document.createElement('ha-card');
|
|
95
|
|
- shadow.appendChild(card);
|
|
96
|
|
- return card;
|
|
97
|
|
- }
|
|
98
|
|
-
|
|
99
|
|
- _didRender() {
|
|
100
|
|
- this.circle = this._root.querySelector('#circle');
|
|
101
|
|
- if (this.config) {
|
|
102
|
|
- this._updateConfig();
|
|
103
|
|
- }
|
|
104
|
|
- }
|
|
105
|
|
-
|
|
106
|
|
- setConfig(config) {
|
|
107
|
|
- if (!config.entity) {
|
|
108
|
|
- throw Error('No entity defined')
|
|
109
|
|
- }
|
|
110
|
|
- this.config = config;
|
|
111
|
|
- if (this.circle) {
|
|
112
|
|
- this._updateConfig();
|
|
113
|
|
- }
|
|
114
|
|
- }
|
|
115
|
|
-
|
|
116
|
|
- getCardSize() {
|
|
117
|
|
- return 3;
|
|
118
|
|
- }
|
|
119
|
|
-
|
|
120
|
|
- _updateConfig() {
|
|
121
|
|
- const container = this._root.querySelector('.labelContainer');
|
|
122
|
|
- container.style.color = 'var(--primary-text-color)';
|
|
123
|
|
-
|
|
124
|
|
- if (this.config.font_style) {
|
|
125
|
|
- Object.keys(this.config.font_style).forEach((prop) => {
|
|
126
|
|
- container.style.setProperty(prop, this.config.font_style[prop]);
|
|
127
|
|
- });
|
|
128
|
|
- }
|
|
129
|
|
- }
|
|
130
|
|
-
|
|
131
|
|
- set hass(hass) {
|
|
132
|
|
- this.state = hass.states[this.config.entity];
|
|
133
|
|
-
|
|
134
|
|
- if (this.config.attribute) {
|
|
135
|
|
- if (!this.state.attributes[this.config.attribute] ||
|
|
136
|
|
- isNaN(this.state.attributes[this.config.attribute])) {
|
|
137
|
|
- console.error(`Attribute [${this.config.attribute}] is not a number`);
|
|
138
|
|
- return;
|
|
139
|
|
- }
|
|
140
|
|
- } else {
|
|
141
|
|
- if (!this.state || isNaN(this.state.state)) {
|
|
142
|
|
- console.error(`State is not a number`);
|
|
143
|
|
- return;
|
|
144
|
|
- }
|
|
145
|
|
- }
|
|
146
|
|
-
|
|
147
|
|
- const state = this.config.attribute
|
|
148
|
|
- ? this.state.attributes[this.config.attribute]
|
|
149
|
|
- : this.state.state;
|
|
150
|
|
- const r = 200 * .45;
|
|
151
|
|
- const min = this.config.min || 0;
|
|
152
|
|
- const max = this.config.attribute_max
|
|
153
|
|
- ? this.state.attributes[this.config.attribute_max]
|
|
154
|
|
- : (this.config.max || 100);
|
|
155
|
|
- const val = this._calculateValueBetween(min, max, state);
|
|
156
|
|
- const score = val * 2 * Math.PI * r;
|
|
157
|
|
- const total = 10 * r;
|
|
158
|
|
- this.dashArray = `${score} ${total}`;
|
|
159
|
|
-
|
|
160
|
|
- let colorStops = {};
|
|
161
|
|
- colorStops[min] = this.config.stroke_color || '#03a9f4';
|
|
162
|
|
- if (this.config.color_stops) {
|
|
163
|
|
- Object.keys(this.config.color_stops).forEach((key) => {
|
|
164
|
|
- colorStops[key] = this.config.color_stops[key];
|
|
165
|
|
- });
|
|
166
|
|
- }
|
|
167
|
|
-
|
|
168
|
|
- if (this.circle) {
|
|
169
|
|
- const stroke = this._calculateStrokeColor(state, colorStops);
|
|
170
|
|
- this.circle.setAttribute('stroke', stroke);
|
|
171
|
|
- }
|
|
172
|
|
- }
|
|
173
|
|
-
|
|
174
|
|
- _click() {
|
|
175
|
|
- this._fire('hass-more-info', { entityId: this.config.entity });
|
|
176
|
|
- }
|
|
177
|
|
-
|
|
178
|
|
- _calculateStrokeColor(state, stops) {
|
|
179
|
|
- const sortedStops = Object.keys(stops).map(n => Number(n)).sort((a, b) => a - b);
|
|
180
|
|
- let start, end, val;
|
|
181
|
|
- const l = sortedStops.length;
|
|
182
|
|
- if (state <= sortedStops[0]) {
|
|
183
|
|
- return stops[sortedStops[0]];
|
|
184
|
|
- } else if (state >= sortedStops[l - 1]) {
|
|
185
|
|
- return stops[sortedStops[l - 1]];
|
|
186
|
|
- } else {
|
|
187
|
|
- for (let i = 0; i < l - 1; i++) {
|
|
188
|
|
- const s1 = sortedStops[i];
|
|
189
|
|
- const s2 = sortedStops[i + 1];
|
|
190
|
|
- if (state >= s1 && state < s2) {
|
|
191
|
|
- [start, end] = [stops[s1], stops[s2]];
|
|
192
|
|
- if (!this.config.gradient) {
|
|
193
|
|
- return start;
|
|
194
|
|
- }
|
|
195
|
|
- val = this._calculateValueBetween(s1, s2, state);
|
|
196
|
|
- break;
|
|
197
|
|
- }
|
|
198
|
|
- }
|
|
199
|
|
- }
|
|
200
|
|
- return this._getGradientValue(start, end, val);
|
|
201
|
|
- }
|
|
202
|
|
-
|
|
203
|
|
- _calculateValueBetween(start, end, val) {
|
|
204
|
|
- return (val - start) / (end - start);
|
|
205
|
|
- }
|
|
206
|
|
-
|
|
207
|
|
- _getGradientValue(colorA, colorB, val) {
|
|
208
|
|
- const v1 = 1 - val;
|
|
209
|
|
- const v2 = val;
|
|
210
|
|
- const decA = this._hexColorToDecimal(colorA);
|
|
211
|
|
- const decB = this._hexColorToDecimal(colorB);
|
|
212
|
|
- const rDec = Math.floor((decA[0] * v1) + (decB[0] * v2));
|
|
213
|
|
- const gDec = Math.floor((decA[1] * v1) + (decB[1] * v2));
|
|
214
|
|
- const bDec = Math.floor((decA[2] * v1) + (decB[2] * v2));
|
|
215
|
|
- const rHex = this._padZero(rDec.toString(16));
|
|
216
|
|
- const gHex = this._padZero(gDec.toString(16));
|
|
217
|
|
- const bHex = this._padZero(bDec.toString(16));
|
|
218
|
|
- return `#${rHex}${gHex}${bHex}`;
|
|
219
|
|
- }
|
|
220
|
|
-
|
|
221
|
|
- _hexColorToDecimal(color) {
|
|
222
|
|
- let c = color.substr(1);
|
|
223
|
|
- if (c.length === 3) {
|
|
224
|
|
- c = `${c[0]}${c[0]}${c[1]}${c[1]}${c[2]}${c[2]}`;
|
|
225
|
|
- }
|
|
226
|
|
-
|
|
227
|
|
- const [r, g, b] = c.match(/.{2}/g);
|
|
228
|
|
- return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)];
|
|
229
|
|
- }
|
|
230
|
|
-
|
|
231
|
|
- _padZero(val) {
|
|
232
|
|
- if (val.length < 2) {
|
|
233
|
|
- val = `0${val}`;
|
|
234
|
|
- }
|
|
235
|
|
- return val.substr(0, 2);
|
|
236
|
|
- }
|
|
237
|
|
-
|
|
238
|
|
- _fire(type, detail) {
|
|
239
|
|
- const event = new Event(type, {
|
|
240
|
|
- bubbles: true,
|
|
241
|
|
- cancelable: false,
|
|
242
|
|
- composed: true
|
|
243
|
|
- });
|
|
244
|
|
- event.detail = detail || {};
|
|
245
|
|
- this.shadowRoot.dispatchEvent(event);
|
|
246
|
|
- return event;
|
|
247
|
|
- }
|
|
248
|
|
-}
|
|
249
|
|
-customElements.define('circle-sensor-card', CircleSensorCard);
|