forked from panbachi/tankerkoenig-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tankerkoenig-card.js
163 lines (130 loc) · 5.06 KB
/
tankerkoenig-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
import {
LitElement,
html,
css,
property
} from "https://unpkg.com/[email protected]/lit-element.js?module";
class TankerkoenigCard extends LitElement {
static get properties() {
return {
hass: {},
config: {}
};
}
render() {
this.stations.sort((a, b) => {
let key = '';
if(a.diesel) {
key = 'diesel';
} else if(a.e5) {
key = 'e5';
} else if(a.e10) {
key = 'e10';
}
if(this.hass.states[a[key]].state === 'unknown' || this.hass.states[a[key]].state === 'unavailable') {
return 1;
}
if(this.hass.states[b[key]].state === 'unknown' || this.hass.states[b[key]].state === 'unavailable') {
return -1;
}
if(this.hass.states[a[key]].state > this.hass.states[b[key]].state) return 1;
if(this.hass.states[b[key]].state > this.hass.states[a[key]].state) return -1;
return 0;
});
let header = '';
if(this.show_header === true) {
header = this.config.name || 'Tankerkönig';
}
return html`<ha-card elevation="2" header="${header}">
<div class="container">
<table width="100%">
${this.stations.map((station) => {
if(!this.isOpen(station) && this.config.show_closed !== true) return;
return html`<tr>
<td class="logo"><img height="40" width="40" src="/local/gasstation_logos/${station.brand.toLowerCase()}.png"></td>
<td class="name">${station.name}</td>
${this.renderPrice(station, 'e5')}
${this.renderPrice(station, 'e10')}
${this.renderPrice(station, 'diesel')}
</tr>`;
})}
</table>
</div>
</ha-card>`;
}
getStationState(station) {
let state = null;
if(this.has.e5) {
state = this.hass.states[station.e5] || null;
} else if(this.has.e10) {
state = this.hass.states[station.e10] || null;
} else if(this.has.diesel) {
state = this.hass.states[station.diesel] || null;
}
return state;
}
isOpen(station) {
// Temporary fix, adjustments need to be made to support 2022.04
return true;
/*const state = this.getStationState(station);
if(state && state.attributes.is_open) {
return true;
}
return false; */
}
renderPrice(station, type) {
if(!this.has[type]) {
return;
}
const state = this.hass.states[station[type]] || null;
if(state && state.state != 'unknown' && state.state != 'unavailable' && this.isOpen(station)) {
return html`<td><ha-label-badge
label="${type.toUpperCase()}"
@click="${() => this.fireEvent('hass-more-info', station[type])}"
><span style="font-size: 75%;">${state.state}€</span></ha-label-badge></td>`;
} else {
return html`<td><ha-label-badge
icon="mdi:lock-outline"
label="${type.toUpperCase()}"
@click="${() => this.fireEvent('hass-more-info', station[type])}"
></ha-label-badge></td>`;
}
}
fireEvent(type, entityId, options = {}) {
const event = new Event(type, {
bubbles: options.bubbles || true,
cancelable: options.cancelable || true,
composed: options.composed || true,
});
event.detail = {entityId: entityId};
this.dispatchEvent(event);
}
setConfig(config) {
this.config = config;
if(this.config.show_header !== false) {
this.show_header = true;
} else {
this.show_header = false;
}
this.has = {
e5: this.config.show.indexOf('e5') !== -1,
e10: this.config.show.indexOf('e10') !== -1,
diesel: this.config.show.indexOf('diesel') !== -1,
};
this.stations = this.config.stations.slice();
}
getCardSize() {
return this.stations.length + 1;
}
static get styles() {
return css`
.container { padding: 0 16px 16px; }
td { text-align: center; padding-top: 10px; }
td.name { text-align: left; font-weight: bold; }
td.gasstation img { vertical-align: middle; }
ha-label-badge { font-size: 85%; }
.label-badge .value { font-size: 70%; }
`;
}
}
customElements.define('tankerkoenig-card', TankerkoenigCard);