|
|
@@ -1,201 +0,0 @@
|
|
1
|
|
-class GaugeCard extends HTMLElement {
|
|
2
|
|
- constructor() {
|
|
3
|
|
- super();
|
|
4
|
|
- this.attachShadow({ mode: 'open' });
|
|
5
|
|
- }
|
|
6
|
|
- setConfig(config) {
|
|
7
|
|
- if (!config.entity) {
|
|
8
|
|
- throw new Error('Please define an entity');
|
|
9
|
|
- }
|
|
10
|
|
-
|
|
11
|
|
- const root = this.shadowRoot;
|
|
12
|
|
- if (root.lastChild) root.removeChild(root.lastChild);
|
|
13
|
|
-
|
|
14
|
|
- const cardConfig = Object.assign({}, config);
|
|
15
|
|
- if (!cardConfig.scale) cardConfig.scale = "50px";
|
|
16
|
|
- if (!cardConfig.min) cardConfig.min = 0;
|
|
17
|
|
- if (!cardConfig.max) cardConfig.max = 100;
|
|
18
|
|
-
|
|
19
|
|
- const entityParts = this._splitEntityAndAttribute(cardConfig.entity);
|
|
20
|
|
- cardConfig.entity = entityParts.entity;
|
|
21
|
|
- if (entityParts.attribute) cardConfig.attribute = entityParts.attribute;
|
|
22
|
|
-
|
|
23
|
|
- const card = document.createElement('ha-card');
|
|
24
|
|
- const shadow = card.attachShadow({ mode: 'open' });
|
|
25
|
|
- const content = document.createElement('div');
|
|
26
|
|
- const style = document.createElement('style');
|
|
27
|
|
- style.textContent = `
|
|
28
|
|
- ha-card {
|
|
29
|
|
- --base-unit: ${cardConfig.scale};
|
|
30
|
|
- height: calc(var(--base-unit)*3);
|
|
31
|
|
- position: relative;
|
|
32
|
|
- }
|
|
33
|
|
- .container{
|
|
34
|
|
- width: calc(var(--base-unit) * 4);
|
|
35
|
|
- height: calc(var(--base-unit) * 2);
|
|
36
|
|
- position: absolute;
|
|
37
|
|
- top: calc(var(--base-unit)*1.5);
|
|
38
|
|
- left: 50%;
|
|
39
|
|
- overflow: hidden;
|
|
40
|
|
- text-align: center;
|
|
41
|
|
- transform: translate(-50%, -50%);
|
|
42
|
|
- }
|
|
43
|
|
- .gauge-a{
|
|
44
|
|
- z-index: 1;
|
|
45
|
|
- position: absolute;
|
|
46
|
|
- background-color: var(--primary-background-color);
|
|
47
|
|
- width: calc(var(--base-unit) * 4);
|
|
48
|
|
- height: calc(var(--base-unit) * 2);
|
|
49
|
|
- top: 0%;
|
|
50
|
|
- border-radius:calc(var(--base-unit) * 2.5) calc(var(--base-unit) * 2.5) 0px 0px ;
|
|
51
|
|
- }
|
|
52
|
|
- .gauge-b{
|
|
53
|
|
- z-index: 3;
|
|
54
|
|
- position: absolute;
|
|
55
|
|
- background-color: var(--paper-card-background-color);
|
|
56
|
|
- width: calc(var(--base-unit) * 2.5);
|
|
57
|
|
- height: calc(var(--base-unit) * 1.25);
|
|
58
|
|
- top: calc(var(--base-unit) * 0.75);
|
|
59
|
|
- margin-left: calc(var(--base-unit) * 0.75);
|
|
60
|
|
- margin-right: auto;
|
|
61
|
|
- border-radius: calc(var(--base-unit) * 2.5) calc(var(--base-unit) * 2.5) 0px 0px ;
|
|
62
|
|
- }
|
|
63
|
|
- .gauge-c{
|
|
64
|
|
- z-index: 2;
|
|
65
|
|
- position: absolute;
|
|
66
|
|
- background-color: var(--label-badge-yellow);
|
|
67
|
|
- width: calc(var(--base-unit) * 4);
|
|
68
|
|
- height: calc(var(--base-unit) * 2);
|
|
69
|
|
- top: calc(var(--base-unit) * 2);
|
|
70
|
|
- margin-left: auto;
|
|
71
|
|
- margin-right: auto;
|
|
72
|
|
- border-radius: 0px 0px calc(var(--base-unit) * 2) calc(var(--base-unit) * 2) ;
|
|
73
|
|
- transform-origin: center top;
|
|
74
|
|
- transition: all 1.3s ease-in-out;
|
|
75
|
|
- }
|
|
76
|
|
- .gauge-data{
|
|
77
|
|
- z-index: 4;
|
|
78
|
|
- color: var(--primary-text-color);
|
|
79
|
|
- line-height: calc(var(--base-unit) * 0.3);
|
|
80
|
|
- position: absolute;
|
|
81
|
|
- width: calc(var(--base-unit) * 4);
|
|
82
|
|
- height: calc(var(--base-unit) * 2.1);
|
|
83
|
|
- top: calc(var(--base-unit) * 1.2);
|
|
84
|
|
- margin-left: auto;
|
|
85
|
|
- margin-right: auto;
|
|
86
|
|
- transition: all 1s ease-out;
|
|
87
|
|
- }
|
|
88
|
|
- .gauge-data #percent{
|
|
89
|
|
- font-size: calc(var(--base-unit) * 0.55);
|
|
90
|
|
- }
|
|
91
|
|
- .gauge-data #title{
|
|
92
|
|
- padding-top: calc(var(--base-unit) * 0.15);
|
|
93
|
|
- font-size: calc(var(--base-unit) * 0.30);
|
|
94
|
|
- }
|
|
95
|
|
- `;
|
|
96
|
|
- content.innerHTML = `
|
|
97
|
|
- <div class="container">
|
|
98
|
|
- <div class="gauge-a"></div>
|
|
99
|
|
- <div class="gauge-b"></div>
|
|
100
|
|
- <div class="gauge-c" id="gauge"></div>
|
|
101
|
|
- <div class="gauge-data"><div id="percent"></div><div id="title"></div></div>
|
|
102
|
|
- </div>
|
|
103
|
|
- `;
|
|
104
|
|
- card.appendChild(content);
|
|
105
|
|
- card.appendChild(style);
|
|
106
|
|
- card.addEventListener('click', event => {
|
|
107
|
|
- this._fire('hass-more-info', { entityId: cardConfig.entity });
|
|
108
|
|
- });
|
|
109
|
|
- root.appendChild(card);
|
|
110
|
|
- this._config = cardConfig;
|
|
111
|
|
- }
|
|
112
|
|
-
|
|
113
|
|
- _splitEntityAndAttribute(entity) {
|
|
114
|
|
- let parts = entity.split('.');
|
|
115
|
|
- if (parts.length < 3) {
|
|
116
|
|
- return { entity: entity };
|
|
117
|
|
- }
|
|
118
|
|
-
|
|
119
|
|
- return { attribute: parts.pop(), entity: parts.join('.') };
|
|
120
|
|
- }
|
|
121
|
|
-
|
|
122
|
|
- _fire(type, detail, options) {
|
|
123
|
|
- const node = this.shadowRoot;
|
|
124
|
|
- options = options || {};
|
|
125
|
|
- detail = (detail === null || detail === undefined) ? {} : detail;
|
|
126
|
|
- const event = new Event(type, {
|
|
127
|
|
- bubbles: options.bubbles === undefined ? true : options.bubbles,
|
|
128
|
|
- cancelable: Boolean(options.cancelable),
|
|
129
|
|
- composed: options.composed === undefined ? true : options.composed
|
|
130
|
|
- });
|
|
131
|
|
- event.detail = detail;
|
|
132
|
|
- node.dispatchEvent(event);
|
|
133
|
|
- return event;
|
|
134
|
|
- }
|
|
135
|
|
-
|
|
136
|
|
- _translateTurn(value, config) {
|
|
137
|
|
- return 5 * (value - config.min) / (config.max - config.min)
|
|
138
|
|
- }
|
|
139
|
|
-
|
|
140
|
|
- _computeSeverity(stateValue, sections) {
|
|
141
|
|
- let numberValue = Number(stateValue);
|
|
142
|
|
- const severityMap = {
|
|
143
|
|
- red: "var(--label-badge-red)",
|
|
144
|
|
- green: "var(--label-badge-green)",
|
|
145
|
|
- amber: "var(--label-badge-yellow)",
|
|
146
|
|
- normal: "var(--label-badge-blue)",
|
|
147
|
|
- }
|
|
148
|
|
- if (!sections) return severityMap["normal"];
|
|
149
|
|
- let sortable = [];
|
|
150
|
|
- for (let severity in sections) {
|
|
151
|
|
- sortable.push([severity, sections[severity]]);
|
|
152
|
|
- }
|
|
153
|
|
- sortable.sort((a, b) => { return a[1] - b[1] });
|
|
154
|
|
-
|
|
155
|
|
- if (numberValue >= sortable[0][1] && numberValue < sortable[1][1]) {
|
|
156
|
|
- return severityMap[sortable[0][0]]
|
|
157
|
|
- }
|
|
158
|
|
- if (numberValue >= sortable[1][1] && numberValue < sortable[2][1]) {
|
|
159
|
|
- return severityMap[sortable[1][0]]
|
|
160
|
|
- }
|
|
161
|
|
- if (numberValue >= sortable[2][1]) {
|
|
162
|
|
- return severityMap[sortable[2][0]]
|
|
163
|
|
- }
|
|
164
|
|
- return severityMap["normal"];
|
|
165
|
|
- }
|
|
166
|
|
-
|
|
167
|
|
- _getEntityStateValue(entity, attribute) {
|
|
168
|
|
- if (!attribute) {
|
|
169
|
|
- return entity.state;
|
|
170
|
|
- }
|
|
171
|
|
-
|
|
172
|
|
- return entity.attributes[attribute];
|
|
173
|
|
- }
|
|
174
|
|
-
|
|
175
|
|
- set hass(hass) {
|
|
176
|
|
- const config = this._config;
|
|
177
|
|
- const entityState = this._getEntityStateValue(hass.states[config.entity], config.attribute);
|
|
178
|
|
- let measurement = "";
|
|
179
|
|
- if (config.measurement == null)
|
|
180
|
|
- measurement = hass.states[config.entity].attributes.unit_of_measurement;
|
|
181
|
|
- else
|
|
182
|
|
- measurement = config.measurement;
|
|
183
|
|
-
|
|
184
|
|
- const root = this.shadowRoot;
|
|
185
|
|
- if (entityState !== this._entityState) {
|
|
186
|
|
- root.getElementById("percent").textContent = `${entityState} ${measurement}`;
|
|
187
|
|
- root.getElementById("title").textContent = config.title;
|
|
188
|
|
- const turn = this._translateTurn(entityState, config) / 10;
|
|
189
|
|
- root.getElementById("gauge").style.transform = `rotate(${turn}turn)`;
|
|
190
|
|
- root.getElementById("gauge").style.backgroundColor = this._computeSeverity(entityState, config.severity);
|
|
191
|
|
- this._entityState = entityState;
|
|
192
|
|
- }
|
|
193
|
|
- root.lastChild.hass = hass;
|
|
194
|
|
- }
|
|
195
|
|
-
|
|
196
|
|
- getCardSize() {
|
|
197
|
|
- return 1;
|
|
198
|
|
- }
|
|
199
|
|
-}
|
|
200
|
|
-
|
|
201
|
|
-customElements.define('gauge-card', GaugeCard);
|