-
Notifications
You must be signed in to change notification settings - Fork 1
/
pollenprognose-card.js
171 lines (153 loc) · 4.47 KB
/
pollenprognose-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
const LitElement = customElements.get('home-assistant-main')
? Object.getPrototypeOf(customElements.get('home-assistant-main'))
: Object.getPrototypeOf(customElements.get('hui-view'));
const html = LitElement.prototype.html;
class PollenPrognoseCard extends LitElement {
setConfig(config) {
if (!config.allergens) {
throw new Error('Please define allergens (list)');
}
if (!config.region_desc) {
throw new Error('Please define region_desc');
}
if (!config.region_id) {
throw new Error('Please define region_id');
}
if (config.threshold && (typeof(config.threshold) != 'number')) {
throw new Error('Threshold must be a number')
}
this.config = config;
}
render(){
if(this.sensors.length < 1) {
console.log("No sensor data not rendering card.")
return;
}
return html
`
${this._renderMinimalStyle()}
${this._renderMinimalCard()}
`
}
_renderMinimalCard(){
return html
`
<ha-card>
${this.config.title == null || this.config.title == true ?
html`
<div class="header">
<div class="name">
${this.header}
</div>
</div>`
: ""
}
<div class="flex-container">
${this.sensors.map(sensor => html`
<div class="sensor">
<p>${sensor.allergen}</p>
<img title="${sensor.forecast.attributes.state_today_desc}" src="/local/pollen_img/${sensor.allergen.toLowerCase()}_${!sensor.forecast.state ? 0 : sensor.forecast.state}.svg"/>
${this.config.forecast == true
? html`<img class="forecast" title="${sensor.forecast.attributes.state_tomorrow_desc}" src="/local/pollen_img/${sensor.allergen.toLowerCase()}_${!sensor.forecast.attributes.state_tomorrow ? 0 : sensor.forecast.attributes.state_tomorrow}.svg"/>`
: ""}
${this.config.show_state == true || this.config.show_state == null
? html`<p>${sensor.forecast.state == "unknown" ? sensor.forecast.state : sensor.forecast.attributes.state_today_desc}</p>`
: ""}
</div>
`)}
</div>
</ha-card>
`
}
_renderMinimalStyle() {
return html
`
<style>
ha-card {
padding: 16px;
}
.header {
padding: 0;
@apply --paper-font-headline;
line-height: 40px;
color: var(--primary-text-color);
padding: 4px 0 12px;
}
td {
padding: 3px;
text-align: center;
}
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: space-evenly;
align-items: center;
}
.sensor {
margin: 10px;
flex: 1 1 0;
flex-direction: column;
justify-content: space-evenly;
display: flex;
align-self: flex-start;
}
@supports not (-ms-flex: 1) {
.flex-container {
height: auto; /* 2 */
// min-height: 24em; /* 2 */
}
}
img {
max-height: 50px;
}
.forecast {
max-height: 25px;
}
.sensor {
display: block;
min-width: 16.66%;
flex: 1;
}
</style>`
}
_tryParseInt(str,defaultValue) {
var retValue = defaultValue;
if(str !== null) {
if(str.length > 0) {
if (!isNaN(str)) {
retValue = parseInt(str);
}
}
}
return retValue;
}
set hass(hass) {
this._hass = hass;
var sensors = [];
if (this.config.title == null || this.config.title == true) {
var header_region = this.config.region_desc
this.header = `Pollenprognose ${header_region.charAt(0).toUpperCase() + header_region.slice(1)}`;
}
const region_id = this.config.region_id;
const allergens = this.config.allergens;
for (var i = 0; i < allergens.length; i++) {
var dict = {};
dict.allergen = (allergens[i].charAt(0).toUpperCase() + allergens[i].slice(1));
var allergen = allergens[i].replace(' / ', '_').toLowerCase();
if ((this.config.filter_unknown == null || this.config.filter_unknown == true) && hass.states[`sensor.pollenflug_${allergen}_${region_id}`].state == 0)
continue;
dict.forecast = hass.states[`sensor.pollenflug_${allergen}_${region_id}`]
sensors.push(dict)
}
this.sensors = sensors;
this.requestUpdate();
}
// @TODO: This requires more intelligent logic
getCardSize() {
return 1;
}
}
class HAPC extends PollenPrognoseCard {} ;
customElements.define('pollenprognose-card', PollenPrognoseCard);