-
Notifications
You must be signed in to change notification settings - Fork 2
/
swiss-public-transport-card.js
247 lines (223 loc) · 6.08 KB
/
swiss-public-transport-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
var LitElement =
LitElement ||
Object.getPrototypeOf(customElements.get("home-assistant-main"));
var html = LitElement.prototype.html;
const css = LitElement.prototype.css;
class SwissPublicTransportCard extends LitElement {
static get is() {
return "swiss-public-transport-card";
}
render() {
if (!this._config || !this.hass) {
return html``;
}
const state = this.hass.states[this._config.entity];
if (!state) {
return html`
<hui-warning
>${this.hass.localize(
"ui.panel.lovelace.warning.entity_not_found",
"entity",
this._config.entity
)}</hui-warning
>
`;
}
this._update_departures(state);
return html`
<ha-card id="hacard">
<div class="card-header">
${state.attributes.friendly_name}
${this._config.show_last_changed
? html`
<div class="name">
${this.last_changed}
</div>`
: html``
}
</div>
<table>
<tbody id="departuretable">
${this.departures.map(
departure => html`
<tr>
<td class="shrink" style="text-align:left;">
<span
class="line ${departure.category}"
title="${departure.exactname}"
>${departure.linename}</span
>
</td>
<td
class="shrink ${departure.delayed}"
style="text-align:right;"
data-departure="${departure.departure}"
>
${departure.departure_time}
</td>
<td class="expand ${departure.delayed}">${departure.destination}</td>
<td
class="shrink ${departure.delayed}"
style="text-align:right;"
>
${departure.delayed?html`(delayed by ${departure.delay}′)`:html``} ${departure.eta}
</td>
<td
class="shrink ${departure.delayed}"
style="text-align:right;"
>
${departure.platform}
</td>
</tr>
`
)}
</tbody>
</table>
</ha-card>
`;
}
static get properties() {
return {
header: { type: String },
hass: {
type: Object
},
departures: {
type: Object
},
_config: {
type: Object
},
showConfig: Boolean
};
}
_update_departures(state) {
var departures = [];
const now = moment();
this.last_changed = moment.duration(moment(state.last_changed).diff(now)).humanize(true);
const departure_countdown = 60 * (this._config.departure_countdown === undefined ? 15 : 0);
if (!state.attributes["departures"]) {
this.departures = [];
return;
}
for (const journey of state.attributes["departures"]) {
const destination = journey["to"];
const exactname = journey["name"];
const category = journey["category"];
const linename =
category +
(journey["number"].startsWith(category) ? "" : journey["number"]);
const delay = journey["delay"];
const delayed = delay > 0 ? "delayed" : "";
const departure = journey["departure"];
// Format departure time in 24h format.
const time = moment(departure).format("HH:mm");
const totalseconds = Math.floor(moment.duration(moment(now).diff(departure)).asSeconds()) - (delayed ? delay * 60 : 0);
var eta = undefined;
if (totalseconds > 0)
continue;
const absoluttotalseconds = Math.abs(totalseconds);
if (absoluttotalseconds < departure_countdown)
{
const minutes = Math.floor(absoluttotalseconds / 60);
const seconds = absoluttotalseconds % 60;
eta = "in ";
eta += minutes + "′";
if (this._config.show_seconds)
eta += seconds + "″";
}
departures.push({
linename: linename,
exactname: exactname,
departure_time: time,
departure: departure,
destination: destination,
category: category,
delay: delay,
delayed: delayed,
eta: eta,
platform: journey["platform"]
});
}
this.departures = departures;
}
setConfig(config) {
if (!config.entity) {
throw new Error("You need to define an entity");
}
this._config = config;
this.departures = [];
}
getCardSize() {
return 1;
}
firstUpdated(changedProperties) {
if (this._config.show_seconds)
setInterval(() => this.requestUpdate(), 1000);
else
setInterval(() => this.requestUpdate(), 10000);
}
static get styles() {
return css`
.name {
line-height: normal;
font-size: 16px;
color: var(--secondary-text-color);
}
table {
width: 100%;
padding: 6px 14px;
}
td {
padding: 3px 3px;
}
td.shrink {
white-space: nowrap;
}
td.expand {
width: 99%;
}
td.delayed {
color: #f00;
}
span.line {
font-weight: bold;
font-size: 0.9em;
padding: 3px 8px 2px 8px;
background-color: #888;
margin-right: 0.7em;
}
span.S {
color: #fff;
border: solid 1px #ccc;
background-color: #2d327d;
}
span.RE {
color: #fff;
border: solid 1px #ccc;
background-color: #eb0000;
}
span.IR {
color: #fff;
border: solid 1px #ccc;
background-color: #eb0000;
}
span.IC {
color: #fff;
border: solid 1px #ccc;
background-color: #eb0000;
}
span.T {
color: #fff;
border: solid 1px #ccc;
background-color: #009d3a;
}
span.B {
color: #000;
border: solid 1px #ccc;
background-color: #fff;
}
`;
}
}
customElements.define("swiss-public-transport-card", SwissPublicTransportCard);