-
Notifications
You must be signed in to change notification settings - Fork 4
/
translink-card.js
103 lines (89 loc) · 3.53 KB
/
translink-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
class TranslinkCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
const card = document.createElement('ha-card');
card.header = 'Next Buses';
this.content = document.createElement('div');
this.content.style.padding = '0 16px 16px';
card.appendChild(this.content);
const style = document.createElement('style');
style.textContent = `
.bus_times {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-gap: 5px;
width: 100%;
margin-bottom: 20px;
}
.bus_time_icon{
grid-column: span 3;
text-align: center;
}
.bus_time{
grid-column: span 3;
text-align: center;
margin-top: 4px;
}
.bus_times_header{
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-gap: 5px;
width: 100%;
margin-bottom: 7px;
}
.bus_times_header_route{
grid-column: span 3;
text-align: center;
}
.bus_times_header_countdown{
grid-column: span 9;
text-align: right;
margin-right: 5%;
}
`;
this.appendChild(style);
this.appendChild(card);
}
const entityId = this.config.entity;
const state = hass.states[entityId];
//console.log(this.config.entities);
//console.log(hass.states);
const stateStr = state ? state.state : 'unavailable';
var tmpcontent = "";
this.config.entities.forEach(function (item, index) {
//console.log(hass.states[item["entity"]]);
//console.log(item, index);
if(hass.states[item["entity"]]){
if(hass.states[item["entity"]].attributes){
if(hass.states[item["entity"]].attributes.next_bus_countdown){
tmpcontent = tmpcontent + `
<div class="bus_times_header">
<div class="bus_times_header_route">Route ${hass.states[item["entity"]].attributes.route_number}</div>
<div class="bus_times_header_countdown">next in ${hass.states[item["entity"]].attributes.next_bus_countdown} min</div>
</div>
<div class="bus_times">
<div class="bus_time_icon"><ha-icon icon="mdi:bus"></ha-icon></div>
<div class="bus_time">${hass.states[item["entity"]].attributes.buses_1}</div>
<div class="bus_time">${hass.states[item["entity"]].attributes.buses_1}</div>
<div class="bus_time">${hass.states[item["entity"]].attributes.buses_1}</div>
</div>
`;
}
}
}
});
this.content.innerHTML = tmpcontent;
}
setConfig(config) {
if (!config.entities) {
throw new Error("You need to define entities");
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 3;
}
}
customElements.define('translink-card', TranslinkCard);