-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenweathermaphistory.js
137 lines (125 loc) · 3.59 KB
/
openweathermaphistory.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
class OpenWeathMapHistoryCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
_getAttributes(hass, p_entity) {
function _getAttribute(p_attribute) {
if(hass.states[p_entity].attributes[p_attribute]) {
return hass.states[p_entity].attributes[p_attribute];
} else {
return '-';
}
}
const attributes = new Map();
attributes.set (`rain`,{
name: `Rain`,
value0: _getAttribute('day0rain'),
value1: _getAttribute('day1rain'),
value2: _getAttribute('day2rain'),
value3: _getAttribute('day3rain'),
value4: _getAttribute('day4rain'),
})
attributes.set (`min`,{
name: `Min Temp`,
value0: _getAttribute('day0min'),
value1: _getAttribute('day1min'),
value2: _getAttribute('day2min'),
value3: _getAttribute('day3min'),
value4: _getAttribute('day4min'),
})
attributes.set (`max`,{
name: `Max Temp`,
value0: _getAttribute('day0max'),
value1: _getAttribute('day1max'),
value2: _getAttribute('day2max'),
value3: _getAttribute('day3max'),
value4: _getAttribute('day4max'),
})
return Array.from(attributes.values());
}
setConfig(config) {
if (!config.entity_id ) {
throw new Error('Please define entity_id');
}
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const cardConfig = Object.assign({}, config);
const card = document.createElement('ha-card');
this.card = card
card.header = config.title;
const content = document.createElement('div');
const style = document.createElement('style');
style.textContent = `
table {
width: 100%;
padding: 16px;
}
thead th {
text-align: left;
}
tbody tr:nth-child(odd) {
background-color: var(--paper-card-background-color);
}
tbody tr:nth-child(even) {
background-color: var(--secondary-background-color);
}
`;
content.innerHTML = `
<ha-card>
<table>
<thead>
<tr>
<th>${'Day '}</th>
<th>${'last 24hr'}</th>
<th>${'2'}</th>
<th>${'3'}</th>
<th>${'4'}</th>
<th>${'5'}</th>
</tr>
</thead>
<tbody id='attributes'>
</tbody>
</table>
</ha-card>
`;
card.appendChild(style);
card.appendChild(content);
root.appendChild(card);
this._config = cardConfig;
}
_updateContent(element, attributes) {
element.innerHTML = `
<tr>
${attributes.map((attribute) => `
<tr>
<td>${attribute.name}</td>
<td>${attribute.value0}</td>
<td>${attribute.value1}</td>
<td>${attribute.value2}</td>
<td>${attribute.value3}</td>
<td>${attribute.value4}</td>
</tr>
`).join('')}
`;
}
set hass(hass) {
const config = this._config;
const root = this.shadowRoot;
let attributes = this._getAttributes(hass, config.entity_id);
this._updateContent(root.getElementById('attributes'), attributes);
const state = hass.states[config.entity_id].state
this.card.header = "Adjustment Factor: " + state;
}
getCardSize() {
return 1;
}
}
customElements.define('open-weather-map-history-card', OpenWeathMapHistoryCard);
window.customCards = window.customCards || [];
window.customCards.push({
type: "open-weather-map-history-card",
name: "open-weather-map-history-card",
preview: true, // Optional - defaults to false
description: "Custom card companion to Open Weather Map History Custom Component" // Optional
});