matterbridge-zigbee2mqtt 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.json +46 -0
  3. package/.gitattributes +2 -0
  4. package/.prettierignore +2 -0
  5. package/.prettierrc.json +12 -0
  6. package/README.md +63 -0
  7. package/dist/colorUtils.d.ts +50 -0
  8. package/dist/colorUtils.d.ts.map +1 -0
  9. package/dist/colorUtils.js +304 -0
  10. package/dist/colorUtils.js.map +1 -0
  11. package/dist/entity.d.ts +57 -0
  12. package/dist/entity.d.ts.map +1 -0
  13. package/dist/entity.js +488 -0
  14. package/dist/entity.js.map +1 -0
  15. package/dist/index.d.ts +12 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +12 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/payloadTypes.d.ts +16 -0
  20. package/dist/payloadTypes.d.ts.map +1 -0
  21. package/dist/payloadTypes.js +13 -0
  22. package/dist/payloadTypes.js.map +1 -0
  23. package/dist/platform.d.ts +29 -0
  24. package/dist/platform.d.ts.map +1 -0
  25. package/dist/platform.js +186 -0
  26. package/dist/platform.js.map +1 -0
  27. package/dist/zigbee2mqtt.d.ts +157 -0
  28. package/dist/zigbee2mqtt.d.ts.map +1 -0
  29. package/dist/zigbee2mqtt.js +964 -0
  30. package/dist/zigbee2mqtt.js.map +1 -0
  31. package/dist/zigbee2mqttTypes.d.ts +339 -0
  32. package/dist/zigbee2mqttTypes.d.ts.map +1 -0
  33. package/dist/zigbee2mqttTypes.js +13 -0
  34. package/dist/zigbee2mqttTypes.js.map +1 -0
  35. package/link-matterbridge-script.js +15 -0
  36. package/package.json +71 -0
  37. package/src/colorUtils.ts +364 -0
  38. package/src/entity.ts +560 -0
  39. package/src/index.ts +14 -0
  40. package/src/payloadTypes.ts +17 -0
  41. package/src/platform.ts +203 -0
  42. package/src/zigbee2mqtt.ts +1191 -0
  43. package/src/zigbee2mqttTypes.ts +311 -0
  44. package/tsconfig.json +112 -0
@@ -0,0 +1,364 @@
1
+ /**
2
+ * This file contains the color utilities.
3
+ *
4
+ * @file colorUtils.ts
5
+ * @author Luca Liguori
6
+ * @date 2023-10-05
7
+ * @version 1.2.4
8
+ *
9
+ * All rights reserved.
10
+ *
11
+ */
12
+
13
+ /* eslint-disable max-len */
14
+ /* eslint-disable @typescript-eslint/no-unused-vars */
15
+
16
+ import { assert } from 'console';
17
+
18
+ export type RGB = {
19
+ r: number;
20
+ g: number;
21
+ b: number;
22
+ };
23
+
24
+ export type XY = {
25
+ x: number;
26
+ y: number;
27
+ };
28
+
29
+ export type HSL = {
30
+ h: number;
31
+ s: number;
32
+ l: number;
33
+ };
34
+
35
+ export function hslColorToRgbColor(hue: number, saturation: number, luminance: number): RGB {
36
+ if (hue === 360) {
37
+ hue = 0;
38
+ }
39
+ assert(hue >= 0 && hue <= 359, 'hslColorToRgbColor Hue error');
40
+ assert(saturation >= 0 && saturation <= 100, 'hslColorToRgbColor Saturation error');
41
+ assert(luminance === 50, 'hslColorToRgbColor Luminance error');
42
+
43
+ saturation /= 100;
44
+ luminance /= 100;
45
+ let r: number, g: number, b: number;
46
+
47
+ if (saturation === 0) {
48
+ r = g = b = luminance; // achromatic
49
+ } else {
50
+ const hue2rgb = (p: number, q: number, t: number): number => {
51
+ if (t < 0) {
52
+ t += 1;
53
+ }
54
+ if (t > 1) {
55
+ t -= 1;
56
+ }
57
+ if (t < 1 / 6) {
58
+ return p + (q - p) * 6 * t;
59
+ }
60
+ if (t < 1 / 2) {
61
+ return q;
62
+ }
63
+ if (t < 2 / 3) {
64
+ return p + (q - p) * (2 / 3 - t) * 6;
65
+ }
66
+ return p;
67
+ };
68
+
69
+ const q = luminance < 0.5 ? luminance * (1 + saturation) : luminance + saturation - luminance * saturation;
70
+ const p = 2 * luminance - q;
71
+
72
+ r = hue2rgb(p, q, hue / 360 + 1 / 3);
73
+ g = hue2rgb(p, q, hue / 360);
74
+ b = hue2rgb(p, q, hue / 360 - 1 / 3);
75
+ }
76
+
77
+ return {
78
+ r: Math.ceil(r * 255),
79
+ g: Math.ceil(g * 255),
80
+ b: Math.ceil(b * 255),
81
+ };
82
+ }
83
+
84
+ // Converts from RGB to the XY color (CIE 1931 color space)
85
+ export function rgbColorToXYColor(rgb: RGB): XY {
86
+ let r = rgb.r / 255;
87
+ let g = rgb.g / 255;
88
+ let b = rgb.b / 255;
89
+
90
+ // Apply gamma correction
91
+ r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
92
+ g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
93
+ b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
94
+
95
+ // Scale the values to the D65 illuminant
96
+ r = r * 100;
97
+ g = g * 100;
98
+ b = b * 100;
99
+
100
+ // Convert RGB to XYZ
101
+ const X = r * 0.664511 + g * 0.154324 + b * 0.162028;
102
+ const Y = r * 0.283881 + g * 0.668433 + b * 0.047685;
103
+ const Z = r * 0.000088 + g * 0.07231 + b * 0.986039;
104
+
105
+ // Normalization
106
+ let x = X / (X + Y + Z);
107
+ let y = Y / (X + Y + Z);
108
+
109
+ // Round to 4 digits
110
+ x = Math.round(x * 10000) / 10000;
111
+ y = Math.round(y * 10000) / 10000;
112
+
113
+ return { x, y };
114
+ }
115
+
116
+ export function xyColorToRgbColor(x: number, y: number, brightness = 254): RGB {
117
+ const z = 1.0 - x - y;
118
+ const Y = (brightness / 254).toFixed(2);
119
+ const X = (Number(Y) / y) * x;
120
+ const Z = (Number(Y) / y) * z;
121
+
122
+ //Convert to RGB using Wide RGB D65 conversion
123
+ let red = X * 1.656492 - Number(Y) * 0.354851 - Z * 0.255038;
124
+ let green = -X * 0.707196 + Number(Y) * 1.655397 + Z * 0.036152;
125
+ let blue = X * 0.051713 - Number(Y) * 0.121364 + Z * 1.01153;
126
+
127
+ //If red, green or blue is larger than 1.0 set it back to the maximum of 1.0
128
+ if (red > blue && red > green && red > 1.0) {
129
+ green = green / red;
130
+ blue = blue / red;
131
+ red = 1.0;
132
+ } else if (green > blue && green > red && green > 1.0) {
133
+ red = red / green;
134
+ blue = blue / green;
135
+ green = 1.0;
136
+ } else if (blue > red && blue > green && blue > 1.0) {
137
+ red = red / blue;
138
+ green = green / blue;
139
+ blue = 1.0;
140
+ }
141
+
142
+ //Reverse gamma correction
143
+ red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, 1.0 / 2.4) - 0.055;
144
+ green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, 1.0 / 2.4) - 0.055;
145
+ blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, 1.0 / 2.4) - 0.055;
146
+
147
+ //Convert normalized decimal to decimal
148
+ red = Math.round(red * 255);
149
+ green = Math.round(green * 255);
150
+ blue = Math.round(blue * 255);
151
+
152
+ //Normalize
153
+ if (isNaN(red) || red < 0) {
154
+ red = 0;
155
+ }
156
+ if (isNaN(green) || green < 0) {
157
+ green = 0;
158
+ }
159
+ if (isNaN(blue) || blue < 0) {
160
+ blue = 0;
161
+ }
162
+
163
+ return { r: red, g: green, b: blue };
164
+ }
165
+
166
+ export function rgbColorToHslColor(rgb: RGB): HSL {
167
+ const r = rgb.r / 255;
168
+ const g = rgb.g / 255;
169
+ const b = rgb.b / 255;
170
+
171
+ const max = Math.max(r, g, b);
172
+ const min = Math.min(r, g, b);
173
+ let h = 0,
174
+ s = 0;
175
+ const l = (max + min) / 2;
176
+
177
+ if (max === min) {
178
+ h = s = 0; // achromatic
179
+ } else {
180
+ const d = max - min;
181
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
182
+ switch (max) {
183
+ case r:
184
+ h = (g - b) / d + (g < b ? 6 : 0);
185
+ break;
186
+ case g:
187
+ h = (b - r) / d + 2;
188
+ break;
189
+ case b:
190
+ h = (r - g) / d + 4;
191
+ break;
192
+ }
193
+ h /= 6;
194
+ }
195
+
196
+ return {
197
+ h: Math.round(h * 360),
198
+ s: Math.round(s * 100),
199
+ l: Math.round(l * 100),
200
+ };
201
+ }
202
+
203
+ export function xyToHsl(x: number, y: number): HSL {
204
+ const rgb = xyColorToRgbColor(x, y);
205
+ return rgbColorToHslColor(rgb);
206
+ }
207
+
208
+ export function testColors(): void {
209
+ // this table has been checked with different apps and sites and is correct 100%
210
+ const colors = [
211
+ { name: 'Pure Red 0', hsl: { h: 0, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 0 }, xy: { x: 0.7006, y: 0.2993 } },
212
+ { name: 'Bright Orange 30', hsl: { h: 30, s: 100, l: 50 }, rgb: { r: 255, g: 128, b: 0 }, xy: { x: 0.6112, y: 0.375 } },
213
+ { name: 'Pure Yellow 60', hsl: { h: 60, s: 100, l: 50 }, rgb: { r: 255, g: 255, b: 0 }, xy: { x: 0.4442, y: 0.5166 } },
214
+ { name: 'Lime Green 90', hsl: { h: 90, s: 100, l: 50 }, rgb: { r: 128, g: 255, b: 0 }, xy: { x: 0.2707, y: 0.6635 } },
215
+ { name: 'Pure Green 120', hsl: { h: 120, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 0 }, xy: { x: 0.1724, y: 0.7468 } },
216
+ { name: 'Light Sea Green 150', hsl: { h: 150, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 128 }, xy: { x: 0.1642, y: 0.5886 } },
217
+ { name: 'Pure Cyan 180', hsl: { h: 180, s: 100, l: 50 }, rgb: { r: 0, g: 255, b: 255 }, xy: { x: 0.1513, y: 0.3425 } },
218
+ { name: 'Deep Sky Blue 210', hsl: { h: 210, s: 100, l: 50 }, rgb: { r: 0, g: 128, b: 255 }, xy: { x: 0.1406, y: 0.1382 } },
219
+ { name: 'Pure Blue 240', hsl: { h: 240, s: 100, l: 50 }, rgb: { r: 0, g: 0, b: 255 }, xy: { x: 0.1355, y: 0.0399 } },
220
+ { name: 'Blue Violet 270', hsl: { h: 270, s: 100, l: 50 }, rgb: { r: 128, g: 0, b: 255 }, xy: { x: 0.2181, y: 0.0778 } },
221
+ { name: 'Pure Magenta 300', hsl: { h: 300, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 255 }, xy: { x: 0.3855, y: 0.1546 } },
222
+ { name: 'Deep Pink 330', hsl: { h: 330, s: 100, l: 50 }, rgb: { r: 255, g: 0, b: 128 }, xy: { x: 0.5797, y: 0.2438 } },
223
+
224
+ { name: 'Pure Red 50% 0', hsl: { h: 0, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 64 }, xy: { x: 0.6036, y: 0.3069 } },
225
+ { name: 'Bright Orange 50% 30', hsl: { h: 30, s: 50, l: 50 }, rgb: { r: 192, g: 128, b: 64 }, xy: { x: 0.5194, y: 0.3928 } },
226
+ { name: 'Pure Yellow 50% 60', hsl: { h: 60, s: 50, l: 50 }, rgb: { r: 192, g: 192, b: 64 }, xy: { x: 0.4258, y: 0.4883 } },
227
+ { name: 'Lime Green 50% 90', hsl: { h: 90, s: 50, l: 50 }, rgb: { r: 128, g: 192, b: 64 }, xy: { x: 0.3159, y: 0.5639 } },
228
+ { name: 'Pure Green 50% 120', hsl: { h: 120, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 64 }, xy: { x: 0.2127, y: 0.6349 } },
229
+ { name: 'Light Sea Green 50% 150', hsl: { h: 150, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 128 }, xy: { x: 0.1932, y: 0.4845 } },
230
+ { name: 'Pure Cyan 50% 180', hsl: { h: 180, s: 50, l: 50 }, rgb: { r: 64, g: 192, b: 192 }, xy: { x: 0.1745, y: 0.3407 } },
231
+ { name: 'Deep Sky Blue 50% 210', hsl: { h: 210, s: 50, l: 50 }, rgb: { r: 64, g: 128, b: 192 }, xy: { x: 0.1752, y: 0.211 } },
232
+ { name: 'Pure Blue 50% 240', hsl: { h: 240, s: 50, l: 50 }, rgb: { r: 64, g: 64, b: 192 }, xy: { x: 0.1758, y: 0.102 } },
233
+ { name: 'Blue Violet 50% 270', hsl: { h: 270, s: 50, l: 50 }, rgb: { r: 128, g: 64, b: 192 }, xy: { x: 0.2688, y: 0.137 } },
234
+ { name: 'Pure Magenta 50% 300', hsl: { h: 300, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 192 }, xy: { x: 0.3772, y: 0.1777 } },
235
+ { name: 'Deep Pink 50% 330', hsl: { h: 330, s: 50, l: 50 }, rgb: { r: 192, g: 64, b: 128 }, xy: { x: 0.489, y: 0.2416 } },
236
+ ];
237
+
238
+ colors.forEach((color) => {
239
+ // eslint-disable-next-line no-console
240
+ console.log(`\x1b[48;2;${color.rgb.r};${color.rgb.g};${color.rgb.b}mColor: ${color.name}\x1b[0m, hsl: { h: ${color.hsl.h}, s: ${color.hsl.s}, l: ${color.hsl.l} }, rgb: { r: ${color.rgb.r}, g: ${color.rgb.g}, b: ${color.rgb.b} }, xy: { x: ${color.xy.x}, y: ${color.xy.y} }`);
241
+
242
+ const rgb = hslColorToRgbColor(color.hsl.h, color.hsl.s, color.hsl.l);
243
+ assert(rgb.r === color.rgb.r && rgb.g === color.rgb.g && rgb.b === color.rgb.b, `\x1b[48;2;${rgb.r};${rgb.g};${rgb.b}mColor: ${color.name}\x1b[0m hslColorToRgbColor { r: ${rgb.r}, g: ${rgb.g}, b: ${rgb.b} } conversion error`);
244
+
245
+ const hsl = rgbColorToHslColor({ r: color.rgb.r, g: color.rgb.g, b: color.rgb.b });
246
+ assert(hsl.h === color.hsl.h && hsl.s === color.hsl.s && hsl.l === color.hsl.l, `Color: ${color.name} rgbColorToHslColor conversion error`);
247
+
248
+ const xy = rgbColorToXYColor({ r: color.rgb.r, g: color.rgb.g, b: color.rgb.b });
249
+ assert(xy.x === color.xy.x && xy.y === color.xy.y, `Color: ${color.name} rgbColorToXYColor conversion error got x ${xy.x} y ${xy.y}`);
250
+
251
+ const rgb2 = xyColorToRgbColor(color.xy.x, color.xy.y);
252
+ assert(rgb2.r === color.rgb.r && rgb2.g === color.rgb.g && rgb2.b === color.rgb.b, `\x1b[48;2;${rgb2.r};${rgb2.g};${rgb2.b}mColor: ${color.name}\x1b[0m xyColorToRgbColor(${color.xy.x}, ${color.xy.y}) conversion error -> r: ${rgb2.r} g: ${rgb2.g} b: ${rgb2.g}`);
253
+ });
254
+ }
255
+
256
+ /**
257
+ * Converts CIE color space to RGB color space
258
+ * @param {Number} x
259
+ * @param {Number} y
260
+ * @param {Number} brightness - Ranges from 1 to 254
261
+ * @return {Array} Array that contains the color values for red, green and blue
262
+ * From: https://github.com/usolved/cie-rgb-converter/blob/master/cie_rgb_converter.js
263
+ */
264
+ export function cie_to_rgb(x: number, y: number, brightness: number = 254): RGB {
265
+ //Set to maximum brightness if no custom value was given (Not the slick ECMAScript 6 way for compatibility reasons)
266
+
267
+ const z = 1.0 - x - y;
268
+ const Y = (brightness / 254).toFixed(2);
269
+ const X = (Number(Y) / y) * x;
270
+ const Z = (Number(Y) / y) * z;
271
+
272
+ //Convert to RGB using Wide RGB D65 conversion
273
+ let red = X * 1.656492 - Number(Y) * 0.354851 - Z * 0.255038;
274
+ let green = -X * 0.707196 + Number(Y) * 1.655397 + Z * 0.036152;
275
+ let blue = X * 0.051713 - Number(Y) * 0.121364 + Z * 1.01153;
276
+
277
+ //If red, green or blue is larger than 1.0 set it back to the maximum of 1.0
278
+ if (red > blue && red > green && red > 1.0) {
279
+ green = green / red;
280
+ blue = blue / red;
281
+ red = 1.0;
282
+ } else if (green > blue && green > red && green > 1.0) {
283
+ red = red / green;
284
+ blue = blue / green;
285
+ green = 1.0;
286
+ } else if (blue > red && blue > green && blue > 1.0) {
287
+ red = red / blue;
288
+ green = green / blue;
289
+ blue = 1.0;
290
+ }
291
+
292
+ //Reverse gamma correction
293
+ red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, 1.0 / 2.4) - 0.055;
294
+ green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, 1.0 / 2.4) - 0.055;
295
+ blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, 1.0 / 2.4) - 0.055;
296
+
297
+ //Convert normalized decimal to decimal
298
+ red = Math.round(red * 255);
299
+ green = Math.round(green * 255);
300
+ blue = Math.round(blue * 255);
301
+
302
+ //Normalize
303
+ if (isNaN(red) || red < 0) {
304
+ red = 0;
305
+ }
306
+ if (isNaN(green) || green < 0) {
307
+ green = 0;
308
+ }
309
+ if (isNaN(blue) || blue < 0) {
310
+ blue = 0;
311
+ }
312
+
313
+ return { r: red, g: green, b: blue };
314
+ }
315
+
316
+ /**
317
+ * Converts RGB color space to CIE color space
318
+ * @param {Number} red
319
+ * @param {Number} green
320
+ * @param {Number} blue
321
+ * @return {Array} Array that contains the CIE color values for x and y
322
+ * From: https://github.com/usolved/cie-rgb-converter/blob/master/cie_rgb_converter.js
323
+ */
324
+ export function rgb_to_cie(red: number, green: number, blue: number): XY {
325
+ //Apply a gamma correction to the RGB values, which makes the color more vivid and more the like the color displayed on the screen of your device
326
+ red = red > 0.04045 ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : red / 12.92;
327
+ green = green > 0.04045 ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : green / 12.92;
328
+ blue = blue > 0.04045 ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : blue / 12.92;
329
+
330
+ //RGB values to XYZ using the Wide RGB D65 conversion formula
331
+ const X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
332
+ const Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
333
+ const Z = red * 0.000088 + green * 0.07231 + blue * 0.986039;
334
+
335
+ //Calculate the xy values from the XYZ values
336
+ let x = (X / (X + Y + Z)).toFixed(4);
337
+ let y = (Y / (X + Y + Z)).toFixed(4);
338
+
339
+ if (isNaN(Number(x))) {
340
+ x = '0';
341
+ }
342
+
343
+ if (isNaN(Number(y))) {
344
+ y = '0';
345
+ }
346
+
347
+ return { x: Number(x), y: Number(y) };
348
+ }
349
+
350
+ /*
351
+ testColors();
352
+ console.log('rgb_to_cie(0, 128, 255)', rgb_to_cie(0, 128, 255));
353
+ console.log('cie_to_rgb(0.1401, 0.1284, 254)', cie_to_rgb(0.1401, 0.1284, 254));
354
+ console.log('cie_to_rgb(0.1406, 0.1382, 254)', cie_to_rgb(0.1406, 0.1382, 254));
355
+ for (let h = 0; h < 360; h++) {
356
+ const rgb = hslColorToRgbColor(h, 100, 50);
357
+ const xy = rgbColorToXYColor({ r: rgb.r, g: rgb.g, b: rgb.b });
358
+ const rgb2 = cie_to_rgb(xy.x, xy.y, 254);
359
+ const hsl = rgbColorToHslColor({ r: rgb2.r, g: rgb2.g, b: rgb2.b });
360
+ assert(rgb.r === rgb2.r && rgb.g === rgb2.g && rgb.b === rgb2.b, 'Color rgb conversion error');
361
+ assert(h === hsl.h, 'Color hsl conversion error');
362
+ console.log(`\x1b[48;2;${rgb.r};${rgb.g};${rgb.b}mColor: r:${rgb.r} g:${rgb.g} b:${rgb.b}\x1b[0m => x:${xy.x} y:${xy.y} => \x1b[48;2;${rgb2.r};${rgb2.g};${rgb2.b}mColor: r:${rgb2.r} g:${rgb2.g} b:${rgb2.b} h:${hsl.h} s:${hsl.s}\x1b[0m\x1b[K`);
363
+ }
364
+ */