-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarnet-card.js
129 lines (118 loc) · 3.35 KB
/
carnet-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
class CarnetCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
const scale = this.config.scale;
const height = this.config.height;
const card = document.createElement("ha-card");
this.content = document.createElement("div");
this.content.style.width = "100%";
if (!!height) {
this.content.style.height = `${height}`;
}
if (!!scale) {
this.content.style.transform = `scale(${scale})`;
}
this.content.style.transformOrigin = "top center";
card.appendChild(this.content);
this.appendChild(card);
}
const sensors = [
{
name: "sunroof_closed",
carnetId: "sunroof",
on: "open",
off: ""
},
{
name: "door_closed_left_front",
carnetId: "door_ul",
on: "open",
off: "closed",
},
{
name: "door_closed_left_back",
carnetId: "door_ll",
on: "open",
off: "closed",
},
{
name: "door_closed_right_front",
carnetId: "door_ur",
on: "open",
off: "closed",
},
{
name: "door_closed_right_back",
carnetId: "door_lr",
on: "open",
off: "closed",
},
{ name: "trunk_closed", carnetId: "trunk", on: "open" },
{
name: "window_closed_left_front",
carnetId: "window_ul",
on: "open",
},
{
name: "window_closed_left_back",
carnetId: "window_ll",
on: "open",
},
{
name: "window_closed_right_front",
carnetId: "window_ur",
on: "open",
},
{
name: "window_closed_right_back",
carnetId: "window_lr",
on: "open",
},
];
const carId = this.config.car_id;
const sliceUrl = this.config.slice_url;
const entities = sensors.map((s) => `binary_sensor.${carId}_${s.name}`);
const states = entities.map((e) => hass.states[e]);
const images = states.map((state, idx) => {
const sensor = sensors[idx];
// trunk doesn't have an "closed" image
// windows don't have "closed" state either.
if (
(state === undefined) ||
(sensor.carnetId === "trunk" && state.state == "off") ||
(sensor.carnetId.startsWith("window_") && state.state == "off")
) {
return "";
}
let key = state.state === "on" ? sensor.on : sensor.off;
if (key !== "") {
key = `_${key}`; // add underscore in front of key
}
const url = `${sliceUrl}_${sensor.carnetId}${key}@2x.png`;
return `<img src="${url}" style="transform-origin: 50%; position: absolute; top: 0; left: 0; width: 100%" />`;
});
// console.log(entities);
// console.log(urls);
// console.log(states);
// console.log(images);
this.content.innerHTML = `
<img src="${sliceUrl}[email protected]" style="width: 100%;" />
${images.join("")}
`;
}
setConfig(config) {
if (!config.slice_url) {
throw new Error("You need to define the slice_url");
}
if (!config.car_id) {
throw new Error("You need to define the car_id");
}
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("carnet-card", CarnetCard);