iobroker.zigbee2mqtt 0.1.0

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.
package/lib/groups.js ADDED
@@ -0,0 +1,45 @@
1
+ const states = require('./states').states;
2
+ const utils = require('./utils');
3
+
4
+ function defineGroupDevice(devices, groupID, ieee_address, scenes) {
5
+ const newDevice = {
6
+ id: groupID,
7
+ ieee_address: ieee_address,
8
+ icon: undefined,
9
+ states: [
10
+ states.state,
11
+ states.brightness,
12
+ states.colortemp,
13
+ states.color,
14
+ states.brightness_move,
15
+ states.colortemp_move,
16
+ states.transition_time,
17
+ //states.brightness_step
18
+ ],
19
+ };
20
+
21
+ // Create buttons for scenes
22
+ for (const scene of scenes) {
23
+ const sceneSate = {
24
+ id: `scene_${scene.id}`,
25
+ prop: `scene_recall`,
26
+ name: scene.name,
27
+ icon: undefined,
28
+ role: 'button',
29
+ write: true,
30
+ read: true,
31
+ type: 'boolean',
32
+ setter: (value) => (value) ? scene.id : undefined
33
+ };
34
+ // @ts-ignore
35
+ newDevice.states.push(sceneSate);
36
+ }
37
+
38
+ // if the device is already present in the cache, remove it
39
+ utils.removeDeviceByIeee(devices, ieee_address);
40
+ devices.push(newDevice);
41
+ }
42
+
43
+ module.exports = {
44
+ defineGroupDevice: defineGroupDevice,
45
+ };
package/lib/rgb.js ADDED
@@ -0,0 +1,263 @@
1
+ /* eslint-disable no-prototype-builtins */
2
+ /*
3
+ With these functions you can convert the CIE color space to the RGB color space and vice versa.
4
+
5
+ The developer documentation for Philips Hue provides the formulas used in the code below:
6
+ https://developers.meethue.com/documentation/color-conversions-rgb-xy
7
+
8
+ I've used the formulas and Objective-C example code and transfered it to JavaScript.
9
+
10
+
11
+ Examples:
12
+
13
+ const rgb = cie_to_rgb(0.6611, 0.2936)
14
+ const cie = rgb_to_cie(255, 39, 60)
15
+
16
+ ------------------------------------------------------------------------------------
17
+
18
+ The MIT License (MIT)
19
+
20
+ Copyright (c) 2017 www.usolved.net
21
+ Published under https://github.com/usolved/cie-rgb-converter
22
+
23
+ Permission is hereby granted, free of charge, to any person obtaining a copy
24
+ of this software and associated documentation files (the "Software"), to deal
25
+ in the Software without restriction, including without limitation the rights
26
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27
+ copies of the Software, and to permit persons to whom the Software is
28
+ furnished to do so, subject to the following conditions:
29
+
30
+ The above copyright notice and this permission notice shall be included in
31
+ all copies or substantial portions of the Software.
32
+
33
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39
+ THE SOFTWARE.
40
+ */
41
+ 'use strict';
42
+
43
+ const colors = require('./colors.js');
44
+
45
+ /**
46
+ * Converts CIE color space to RGB color space
47
+ * @return {Array} Array that contains the color values for red, green and blue
48
+ */
49
+ function cie_to_rgb(x, y, brightness) {
50
+ //Set to maximum brightness if no custom value was given (Not the slick ECMAScript 6 way for compatibility reasons)
51
+ if (brightness === undefined) {
52
+ brightness = 254;
53
+ }
54
+
55
+ const z = 1.0 - x - y;
56
+ const Y = (brightness / 254).toFixed(2);
57
+ // @ts-ignore
58
+ const X = (Y / y) * x;
59
+ // @ts-ignore
60
+ const Z = (Y / y) * z;
61
+
62
+ //Convert to RGB using Wide RGB D65 conversion
63
+ // @ts-ignore
64
+ let red = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
65
+ // @ts-ignore
66
+ let green = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
67
+ // @ts-ignore
68
+ let blue = X * 0.051713 - Y * 0.121364 + Z * 1.011530;
69
+
70
+ //If red, green or blue is larger than 1.0 set it back to the maximum of 1.0
71
+ if (red > blue && red > green && red > 1.0) {
72
+
73
+ green = green / red;
74
+ blue = blue / red;
75
+ red = 1.0;
76
+ }
77
+ else if (green > blue && green > red && green > 1.0) {
78
+
79
+ red = red / green;
80
+ blue = blue / green;
81
+ green = 1.0;
82
+ }
83
+ else if (blue > red && blue > green && blue > 1.0) {
84
+
85
+ red = red / blue;
86
+ green = green / blue;
87
+ blue = 1.0;
88
+ }
89
+
90
+ //Reverse gamma correction
91
+ red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, (1.0 / 2.4)) - 0.055;
92
+ green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, (1.0 / 2.4)) - 0.055;
93
+ blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, (1.0 / 2.4)) - 0.055;
94
+
95
+
96
+ //Convert normalized decimal to decimal
97
+ red = Math.round(red * 255);
98
+ green = Math.round(green * 255);
99
+ blue = Math.round(blue * 255);
100
+
101
+ if (isNaN(red) || red < 0) {
102
+ red = 0;
103
+ }
104
+
105
+ if (isNaN(green) || green < 0) {
106
+ green = 0;
107
+ }
108
+
109
+ if (isNaN(blue) || blue < 0) {
110
+ blue = 0;
111
+ }
112
+
113
+ return [red, green, blue];
114
+ }
115
+
116
+
117
+ /**
118
+ * Converts RGB color space to CIE color space
119
+ * @param {Number} red
120
+ * @param {Number} green
121
+ * @param {Number} blue
122
+ * @return {Array} Array that contains the CIE color values for x and y
123
+ */
124
+ function rgb_to_cie(red, green, blue) {
125
+ // 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
126
+ red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
127
+ green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
128
+ blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
129
+
130
+ // RGB values to XYZ using the Wide RGB D65 conversion formula
131
+ const X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
132
+ const Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
133
+ const Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
134
+
135
+ // Calculate the xy values from the XYZ values
136
+ let x = (X / (X + Y + Z)).toFixed(4);
137
+ let y = (Y / (X + Y + Z)).toFixed(4);
138
+
139
+ // @ts-ignore
140
+ if (isNaN(x)) {
141
+ // @ts-ignore
142
+ x = 0;
143
+ }
144
+
145
+ // @ts-ignore
146
+ if (isNaN(y)) {
147
+ // @ts-ignore
148
+ y = 0;
149
+ }
150
+
151
+ return [x, y];
152
+ }
153
+
154
+
155
+ function hsvToRGB(h, s, v) {
156
+ h = h % 360 / 360;
157
+ s = s / 100;
158
+ v = v / 100;
159
+
160
+ let r; let g; let b;
161
+ if (arguments.length === 1) {
162
+ s = h.s, v = h.v, h = h.h;
163
+ }
164
+ const i = Math.floor(h * 6);
165
+ const f = h * 6 - i;
166
+ const p = v * (1 - s);
167
+ const q = v * (1 - f * s);
168
+ const t = v * (1 - (1 - f) * s);
169
+ switch (i % 6) {
170
+ case 0: r = v, g = t, b = p; break;
171
+ case 1: r = q, g = v, b = p; break;
172
+ case 2: r = p, g = v, b = t; break;
173
+ case 3: r = p, g = q, b = v; break;
174
+ case 4: r = t, g = p, b = v; break;
175
+ case 5: r = v, g = p, b = q; break;
176
+ }
177
+ return {
178
+ r: Math.round(r * 255),
179
+ g: Math.round(g * 255),
180
+ b: Math.round(b * 255),
181
+ };
182
+ }
183
+
184
+ function rgbToHSV(r, g, b, numeric) {
185
+ if (arguments.length === 1) {
186
+ g = r.g, b = r.b, r = r.r;
187
+ }
188
+ const max = Math.max(r, g, b); const min = Math.min(r, g, b);
189
+ const d = max - min;
190
+ let h;
191
+ const s = (max === 0 ? 0 : d / max);
192
+ const v = max / 255;
193
+
194
+ switch (max) {
195
+ case min: h = 0; break;
196
+ case r: h = (g - b) + d * (g < b ? 6 : 0); h /= 6 * d; break;
197
+ case g: h = (b - r) + d * 2; h /= 6 * d; break;
198
+ case b: h = (r - g) + d * 4; h /= 6 * d; break;
199
+ }
200
+ if (numeric) return {
201
+ // @ts-ignore
202
+ h: Math.round(h * 360),
203
+ s: Math.round(s * 100),
204
+ v: Math.round(v * 100),
205
+ };
206
+ return {
207
+ // @ts-ignore
208
+ h: (h * 360).toFixed(3),
209
+ s: (s * 100).toFixed(3),
210
+ v: (v * 100).toFixed(3),
211
+ };
212
+ }
213
+ function colorArrayFromString(value) {
214
+ if (typeof (value) === 'string') {
215
+ const rv = [];
216
+ value.split(',').forEach(element => {
217
+ rv.push(colors.ParseColor(element));
218
+ });
219
+ return rv;
220
+ }
221
+ return [{ r: 0, g: 128, b: 255 }];
222
+ }
223
+
224
+ function colorStringFromRGBArray(payload) {
225
+ const rv = [];
226
+ payload.forEach(element => {
227
+ rv.push(rgb_to_rgbstring(element));
228
+ });
229
+ return rv.toString();
230
+ }
231
+
232
+ function hsv_to_cie(h, s, v) {
233
+ const rgb = hsvToRGB(h, s, v);
234
+ return rgb_to_cie(rgb.r, rgb.g, rgb.b);
235
+ }
236
+
237
+ function rgb_to_rgbstring(element) {
238
+ let col = '#';
239
+ if (element && element.hasOwnProperty('r'))
240
+ col = col + element.r.toString(16).padStart(2, '0');
241
+ else col = col + '00';
242
+ if (element && element.hasOwnProperty('g'))
243
+ col = col + element.g.toString(16).padStart(2, '0');
244
+ else col = col + '00';
245
+ if (element && element.hasOwnProperty('b'))
246
+ col = col + element.b.toString(16).padStart(2, '0');
247
+ else col = col + '00';
248
+ return col;
249
+ }
250
+
251
+
252
+ function hsvToRGBString(h, s, v) {
253
+ return rgb_to_rgbstring(hsvToRGB(h, s, v));
254
+ }
255
+
256
+ exports.hsv_to_cie = hsv_to_cie;
257
+ exports.rgb_to_cie = rgb_to_cie;
258
+ exports.cie_to_rgb = cie_to_rgb;
259
+ exports.hsvToRGB = hsvToRGB;
260
+ exports.rgbToHSV = rgbToHSV;
261
+ exports.colorArrayFromString = colorArrayFromString;
262
+ exports.colorStringFromRGBArray = colorStringFromRGBArray;
263
+ exports.hsvToRGBString = hsvToRGBString;