-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelux-card.js
191 lines (168 loc) · 5.59 KB
/
velux-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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class VeluxCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
this.innerHTML = `
<style>
.container {
display: flex;
align-items: center;
justify-content: space-between;
}
.button-container {
display: flex;
flex-direction: column;
align-items: center;
}
.button {
background-color: var(--primary-color);
border: none;
color: white;
padding: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 0;
cursor: pointer;
border-radius: 8px;
}
.svg-container {
flex-grow: 1;
text-align: center;
}
.svg-image {
width: 100px;
height: 100px;
}
.svg-image circle {
fill: var(--svg-fill-color, black);
}
</style>
<div class="container">
<div class="button-container">
<button class="button" id="openButton1"><ha-icon icon="mdi:menu-up-outline"></ha-icon></button>
<button class="button" id="closeButton1"><ha-icon icon="mdi:menu-down-outline"></ha-icon></button>
</div>
<div id="statusImage">
</div>
<div class="button-container">
<button class="button" id="openButton2"><ha-icon icon="mdi:menu-up"></ha-icon></button>
<button class="button" id="closeButton2"><ha-icon icon="mdi:menu-down"></ha-icon></button>
</div>
</div>
`;
this.content = this.querySelector(".container");
this.querySelector("#openButton1").addEventListener("click", () => {
hass.callService("cover", "open_cover", {
entity_id: this.config.window,
});
});
this.querySelector("#closeButton1").addEventListener("click", () => {
hass.callService("cover", "close_cover", {
entity_id: this.config.window,
});
});
this.querySelector("#openButton2").addEventListener("click", () => {
hass.callService("cover", "open_cover", {
entity_id: this.config.cover,
});
});
this.querySelector("#closeButton2").addEventListener("click", () => {
hass.callService("cover", "close_cover", {
entity_id: this.config.cover,
});
});
}
const window = hass.states[this.config.window];
const cover = hass.states[this.config.cover];
const state1 = window.state;
const state2 = cover.state;
const svgPath = `/local/velux-card/window-${state1}-shutter-${state2}.svg`;
this.loadAndModifySVG(svgPath);
}
async loadAndModifySVG(svgPath) {
const response = await fetch(svgPath);
const svgText = await response.text();
const parser = new DOMParser();
const svgDoc = parser.parseFromString(svgText, "image/svg+xml");
const svgElement = svgDoc.querySelector("svg");
const svgColor =
getComputedStyle(this).getPropertyValue("--primary-text-color") ||
"black";
svgElement.querySelectorAll("*").forEach((el) => {
el.style.stroke = svgColor;
});
// Inject modified SVG into the DOM
const statusImage = this.querySelector("#statusImage");
statusImage.innerHTML = "";
statusImage.appendChild(svgElement);
}
setConfig(config) {
if (!config.window || !config.cover) {
throw new Error("You need to define both entities");
}
this.config = config;
}
getCardSize() {
return 1;
}
//static getConfigElement() {
// return document.createElement("velux-card-editor");
//}
static getStubConfig() {
return { window: "cover.window", cover: "cover.cover" };
}
}
customElements.define("velux-card", VeluxCard);
// Define the editor for the custom card
class VeluxCardEditor extends HTMLElement {
setConfig(config) {
this._config = config;
this.innerHTML = `
<div class="form">
<div class="form-group">
<label for="window">Window:</label>
<input type="text" id="window" name="window" value="${
config.window || ""
}">
</div>
<div class="form-group">
<label for="cover">Shutter:</label>
<input type="text" id="cover" name="cover" value="${
config.cover || ""
}">
</div>
</div>
`;
this.querySelector("#window").addEventListener("change", (e) => {
this._config.window = e.target.value;
this._updateConfig();
});
this.querySelector("#cover").addEventListener("change", (e) => {
this._config.cover = e.target.value;
this._updateConfig();
});
}
_updateConfig() {
const event = new Event("config-changed", {
bubbles: true,
composed: true,
});
event.detail = { config: this._config };
this.dispatchEvent(event);
}
}
customElements.define("velux-card-editor", VeluxCardEditor);
window.customCards = window.customCards || [];
window.customCards.push({
type: "velux-card",
name: "VELUX Card",
description: "Control and view the state of your VELUX roof windows.",
preview: false,
documentationURL: "https://github.com/bhuebschen/velux-card",
});
console.info(
"%c VELUX-CARD \n%c Version: 1.0.3 ",
"color: white; background: #db3434; font-weight: bold; padding: 5px 0;",
"color: white; background: #333; font-weight: bold; padding: 5px 0;"
);