forked from custom-cards/rmv-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmv-card.js
113 lines (104 loc) · 3.1 KB
/
rmv-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
class RmvCard extends HTMLElement {
set hass (hass) {
const entityId = this.config.entity
const state = hass.states[entityId]
const name = this.config.name ? this.config.name : state.attributes['friendly_name']
if (!this.content) {
const card = document.createElement('ha-card')
card.header = name
this.content = document.createElement('div')
const style = document.createElement('style')
style.textContent = `
table {
width: 100%;
padding: 6px 14px;
}
td {
padding: 3px 0px;
}
td.shrink {
white-space:nowrap;
}
td.expand {
width: 99%
}
span.line {
font-weight: bold;
font-size:0.9em;
padding:3px 8px 2px 8px;
color: #fff;
background-color: #888;
margin-right:0.7em;
}
span.S {
background-color: #009252;
}
span.U-Bahn {
background-color: #0067ad;
}
span.Tram {
background-color: #eb6810;
}
span.Bus {
background-color: #a3047a;
}
span.Bahn {
background-color: #000000;
}
`
card.appendChild(style)
card.appendChild(this.content)
this.appendChild(card)
}
var tablehtml = `
<table>
`
const now = new Date()
const utc = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(),
now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds())
const next = {
'line': state.attributes['line'],
'product': state.attributes['product'],
'destination': state.attributes['destination'],
'departure_time': state.attributes['departure_time'],
'direction': state.attributes['direction']
}
const journeys = [next].concat(state.attributes['next_departures'])
var numIter = 0;
for (const journey of journeys) {
numIter++;
if(numIter > 4) // limit to 4 rows max
break;
var destination = journey['destination']
if (typeof journey['destination'] === 'undefined') {
destination = journey['direction']
}
const linename = journey['line']
const product = journey['product']
const jtime = new Date(journey['departure_time'])
//const time = parseInt((jtime.getTime() - utc) / 60000)
const time = `${(jtime.getHours() < 10 ? '0' : '') + jtime.getHours()}:${(jtime.getMinutes() < 10 ? '0' : '') + jtime.getMinutes()}`
tablehtml += `
<tr>
<td class="shrink" style="text-align:center;"><span class="line ${product} ${linename}">${linename}</span></td>
<td class="expand">${destination}</td>
<td class="shrink" style="text-align:right;">${time}</td>
</tr>
`
}
tablehtml += `
</table>
`
this.content.innerHTML = tablehtml
}
setConfig (config) {
if (!config.entity) {
throw new Error('You need to define an entity')
}
this.config = config
}
getCardSize () {
return 1
}
}
customElements.define('rmv-card', RmvCard)