This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ble-bulb-card.js
260 lines (249 loc) · 8.79 KB
/
ble-bulb-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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const array_types_bulb = [
{
type: "triones",
prefix: "Triones-",
service: 0xffd5,
characteristic: 0xffd9,
},
{
type: "magicblue",
prefix: "LEDBLE-",
service: 0xffe5,
characteristic: 0xffe9,
},
];
class BleBulbCard extends HTMLElement {
static get tag() {
return "ble-bulb-card";
}
setConfig(config) {
this.config = config;
if (!config["bulb-types"]) {
throw new Error("You must specify a bulb type");
}
this.array_prefixes = array_types_bulb
.filter((object_type_bulb) =>
config["bulb-types"].includes(object_type_bulb.type)
)
.map((object_type_bulb) => object_type_bulb.prefix);
if (this.array_prefixes.length < 1) {
throw new Error(
`This types chosen are not supported. Please choose one of the following: "${array_types_bulb
.map((a) => a.type)
.join('","')}"`
);
}
}
connectedCallback() {
const condition_firstRun = !this.content;
if (condition_firstRun) {
const card = document.createElement("ha-card");
card.header = this.config.header || "BLE light";
this.appendChild(card);
const style = document.createElement("style");
style.innerHTML = `
.p-4{padding: 1rem;}
.flex{display:flex}
.m-auto{margin: auto;}
.border-2{border-width: 2px;}
.px-2{padding-left: 0.5rem;padding-right: 0.5rem;}
.py-1{padding-top: 0.25rem;padding-bottom: 0.25rem;}
.bg-blue-600{background-color: #3182ce;}
.cursor-pointer{cursor: pointer;}
.cursor-not-allowed{cursor: not-allowed;}
`;
card.appendChild(style);
this.content = document.createElement("div");
this.content.classList.add("p-4");
card.appendChild(this.content);
const interface_bulb = document.createElement("interface-bulb");
interface_bulb.array_prefixes = this.array_prefixes;
interface_bulb.innerHTML = `
<div class="flex">
<button id="button_connect" class="m-auto border-2 px-2 py-1 bg-blue-600"><ha-icon icon="mdi:link-variant"></ha-icon></button>
<button id="button_disconnect" class="m-auto border-2 px-2 py-1 bg-blue-600" disabled><ha-icon icon="mdi:link-variant-off"></ha-icon></button>
<span id="status_connection" class="m-auto px-2 py-1">disconnected</span>
<button is="${ButtonCommand.tag}" value="204, 35, 51" class="m-auto border-2 px-2 py-1"><ha-icon icon="mdi:lightbulb"></ha-icon></button>
<button is="${ButtonCommand.tag}" value="204, 36, 51" class="m-auto border-2 px-2 py-1"><ha-icon icon="mdi:lightbulb-off-outline"></ha-icon></button>
<input is="${PickerColorCommand.tag}" class="m-auto border-2 px-2 py-1">
</div>
`;
this.content.appendChild(interface_bulb);
}
}
}
customElements.define(BleBulbCard.tag, BleBulbCard);
const is_Disableable = {
enable(condition_enable = true, element_destination) {
const element = element_destination || this;
element[condition_enable ? "removeAttribute" : "setAttribute"](
"disabled",
true
);
element.classList[condition_enable ? "add" : "remove"](
"cursor-pointer",
"bg-blue-600"
);
element.classList[condition_enable ? "remove" : "add"](
"cursor-not-allowed"
);
element.classList[!condition_enable ? "add" : "remove"](
"cursor-not-allowed"
);
element.classList[!condition_enable ? "remove" : "add"](
"cursor-pointer",
"bg-blue-600"
);
const element_status = (
this instanceof InterfaceBulb ? this : this.closest(InterfaceBulb.tag)
).querySelector("#status_connection");
if (element_status) {
element_status.innerHTML = condition_enable ? "🟢" : "⚫";
}
},
disable(condition_disable = true, element_destination) {
this.enable(!condition_disable, element_destination);
},
};
class InterfaceBulb extends HTMLElement {
#device;
#bulbType;
#server;
#service;
#characteristic;
static get tag() {
return "interface-bulb";
}
connectedCallback() {
const element_button_connect = this.querySelector("#button_connect");
element_button_connect.addEventListener("click", async () => {
this.connect();
});
const element_button_disconnect = this.querySelector("#button_disconnect");
element_button_disconnect.addEventListener("click", async () => {
this.#device.gatt.disconnect();
});
this.disable(true, element_button_disconnect);
this.addEventListener("ble_connection", (object_event) => {
this.disable(object_event.detail.connected, element_button_connect);
this.enable(object_event.detail.connected, element_button_disconnect);
this.querySelectorAll(
`[is="${ButtonCommand.tag}"],[is="${PickerColorCommand.tag}"]`
).forEach((element) => {
element.enable(object_event.detail.connected);
});
});
}
async connect() {
this.disable(true, this.querySelector("#button_connect"));
this.querySelector("#status_connection").innerHTML = "🟠";
try {
const object_filters = {
filters: this.array_prefixes.map((prefix) => {
return {
namePrefix: prefix,
};
}),
optionalServices: array_types_bulb.map(
(object_type_bulb) => object_type_bulb.service
),
};
this.#device = await navigator.bluetooth.requestDevice(object_filters);
const deviceName = this.#device.name;
this.#bulbType = array_types_bulb.find((type) => {
return deviceName.startsWith(type.prefix);
});
this.#device.ongattserverdisconnected = (error) => {
this.dispatchEvent(
new CustomEvent("ble_connection", {
detail: {
connected: false,
},
})
);
};
this.#server = await this.#device.gatt.connect();
this.#service = await this.#server.getPrimaryService(
this.#bulbType.service
);
this.#characteristic = await this.#service.getCharacteristic(
this.#bulbType.characteristic
);
this.querySelector("#status_connection").innerHTML = "🟢";
this.dispatchEvent(
new CustomEvent("ble_connection", {
detail: {
connected: true,
},
})
);
} catch (error) {
console.warn("error", error);
this.dispatchEvent(
new CustomEvent("ble_connection", {
detail: {
connected: false,
},
})
);
}
}
writeValue(value) {
this.#characteristic.writeValue(value);
}
}
Object.assign(InterfaceBulb.prototype, is_Disableable);
customElements.define(InterfaceBulb.tag, InterfaceBulb);
const is_InterfaceCommand = {
async send_command(value) {
const interface_bulb = this.closest(InterfaceBulb.tag);
interface_bulb.writeValue(new Uint8Array(value));
},
};
class ButtonCommand extends HTMLButtonElement {
static get tag() {
return "button-command";
}
connectedCallback() {
this.disable();
const value = this.getAttribute("value").split(",");
this.addEventListener("click", async () => {
this.send_command(value);
});
}
}
Object.assign(ButtonCommand.prototype, is_InterfaceCommand);
Object.assign(ButtonCommand.prototype, is_Disableable);
customElements.define(ButtonCommand.tag, ButtonCommand, {
extends: "button",
});
class PickerColorCommand extends HTMLInputElement {
static get tag() {
return "picker-color-command";
}
constructor() {
super();
this.type = "color";
}
connectedCallback() {
this.disable();
const convert_hex_to_RGB = (hex) =>
hex
.replace(
/^#?([a-f\d])([a-f\d])([a-f\d])$/i,
(m, r, g, b) => "#" + r + r + g + g + b + b
)
.substring(1)
.match(/.{2}/g)
.map((x) => parseInt(x, 16));
this.addEventListener("input", async () => {
const array_RGB = convert_hex_to_RGB(this.value);
this.send_command([0x56, ...array_RGB, 0x00, 0xf0, 0xaa]);
});
}
}
Object.assign(PickerColorCommand.prototype, is_InterfaceCommand);
Object.assign(PickerColorCommand.prototype, is_Disableable);
customElements.define(PickerColorCommand.tag, PickerColorCommand, {
extends: "input",
});