homebridge-tsvesync 1.3.8 → 1.3.10

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.
@@ -4,6 +4,11 @@ import { RetryManager } from '../../utils/retry';
4
4
  import { VeSyncOutlet, VeSyncSwitch, VeSyncBulb, VeSyncFan, VeSyncDimmerSwitch } from '../../types/device.types';
5
5
  import { VeSync } from 'tsvesync';
6
6
 
7
+ const TEST_DEVICE_MIN_KELVIN = 2700;
8
+ const TEST_DEVICE_MAX_KELVIN = 6500;
9
+
10
+ const percentToKelvin = (percent: number): number => TEST_DEVICE_MIN_KELVIN + ((TEST_DEVICE_MAX_KELVIN - TEST_DEVICE_MIN_KELVIN) * Math.max(0, Math.min(100, percent)) / 100);
11
+
7
12
  /**
8
13
  * Creates a mock Logger instance for testing
9
14
  */
@@ -501,22 +506,82 @@ export interface MockBulbConfig {
501
506
  * Creates a mock bulb instance for testing
502
507
  */
503
508
  export const createMockBulb = (config: MockBulbConfig = {}): jest.Mocked<VeSyncBulb> => {
509
+ const deviceType = config.deviceType || 'ESL100MC';
510
+ const features = new Set<string>(['dimmable']);
511
+ if (deviceType.includes('CW') || deviceType === 'XYD0001') {
512
+ features.add('color_temp');
513
+ }
514
+ if (deviceType.includes('MC') || deviceType === 'XYD0001') {
515
+ features.add('rgb_shift');
516
+ }
517
+
518
+ const colorModel = features.has('rgb_shift') ? (deviceType === 'XYD0001' ? 'hsv' : 'rgb') : 'none';
519
+
520
+ const hsvToRgb = (h: number, s: number, v: number): { red: number; green: number; blue: number } => {
521
+ let hue = h % 360;
522
+ if (hue < 0) {
523
+ hue += 360;
524
+ }
525
+ const saturation = Math.max(0, Math.min(100, s)) / 100;
526
+ const value = Math.max(0, Math.min(100, v)) / 100;
527
+
528
+ const c = value * saturation;
529
+ const x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
530
+ const m = value - c;
531
+
532
+ let rPrime = 0;
533
+ let gPrime = 0;
534
+ let bPrime = 0;
535
+
536
+ if (hue < 60) {
537
+ rPrime = c;
538
+ gPrime = x;
539
+ } else if (hue < 120) {
540
+ rPrime = x;
541
+ gPrime = c;
542
+ } else if (hue < 180) {
543
+ gPrime = c;
544
+ bPrime = x;
545
+ } else if (hue < 240) {
546
+ gPrime = x;
547
+ bPrime = c;
548
+ } else if (hue < 300) {
549
+ rPrime = x;
550
+ bPrime = c;
551
+ } else {
552
+ rPrime = c;
553
+ bPrime = x;
554
+ }
555
+
556
+ return {
557
+ red: Math.round((rPrime + m) * 255),
558
+ green: Math.round((gPrime + m) * 255),
559
+ blue: Math.round((bPrime + m) * 255)
560
+ };
561
+ };
562
+
504
563
  const state = {
505
- brightness: config.brightness || 100,
506
- colorTemp: config.colorTemp || 200,
507
- hue: config.hue || 0,
508
- saturation: config.saturation || 0,
509
- deviceStatus: 'off',
564
+ brightness: config.brightness ?? 100,
565
+ colorTempPercent: config.colorTemp ?? 50,
566
+ hue: config.hue ?? 0,
567
+ saturation: config.saturation ?? 0,
568
+ deviceStatus: 'off' as 'on' | 'off',
569
+ value: 100,
570
+ rgb: { red: 0, green: 0, blue: 0 }
510
571
  };
511
572
 
512
- return {
573
+ if (colorModel === 'rgb') {
574
+ state.rgb = hsvToRgb(state.hue, state.saturation, state.value);
575
+ }
576
+
577
+ const mockBulb: any = {
513
578
  deviceName: config.deviceName || 'Test Bulb',
514
- deviceType: config.deviceType || 'ESL100MC',
579
+ deviceType,
515
580
  cid: config.cid || 'test-cid',
516
581
  uuid: config.uuid || 'test-uuid',
517
582
  deviceStatus: state.deviceStatus,
518
583
  brightness: state.brightness,
519
- colorTemp: state.colorTemp,
584
+ colorTemp: state.colorTempPercent,
520
585
  hue: state.hue,
521
586
  saturation: state.saturation,
522
587
  subDeviceNo: 0,
@@ -528,12 +593,53 @@ export const createMockBulb = (config: MockBulbConfig = {}): jest.Mocked<VeSyncB
528
593
  connectionStatus: 'online',
529
594
  getDetails: jest.fn().mockResolvedValue(true),
530
595
  setApiBaseUrl: jest.fn(),
531
- turnOn: jest.fn().mockResolvedValue(true),
532
- turnOff: jest.fn().mockResolvedValue(true),
533
- setBrightness: jest.fn().mockResolvedValue(true),
534
- setColorTemperature: jest.fn().mockResolvedValue(true),
535
- setColor: jest.fn().mockResolvedValue(true),
536
- } as unknown as jest.Mocked<VeSyncBulb>;
596
+ turnOn: jest.fn().mockImplementation(async () => {
597
+ state.deviceStatus = 'on';
598
+ mockBulb.deviceStatus = 'on';
599
+ return true;
600
+ }),
601
+ turnOff: jest.fn().mockImplementation(async () => {
602
+ state.deviceStatus = 'off';
603
+ mockBulb.deviceStatus = 'off';
604
+ return true;
605
+ }),
606
+ setBrightness: jest.fn().mockImplementation(async (value: number) => {
607
+ state.brightness = value;
608
+ mockBulb.brightness = value;
609
+ if (value > 0) {
610
+ state.deviceStatus = 'on';
611
+ mockBulb.deviceStatus = 'on';
612
+ }
613
+ return true;
614
+ }),
615
+ setColorTemperature: jest.fn().mockImplementation(async (percent: number) => {
616
+ state.colorTempPercent = percent;
617
+ mockBulb.colorTemp = percent;
618
+ return true;
619
+ }),
620
+ setColor: jest.fn().mockImplementation(async (hue: number, saturation: number, value: number = 100) => {
621
+ state.hue = hue;
622
+ state.saturation = saturation;
623
+ state.value = value;
624
+ mockBulb.hue = hue;
625
+ mockBulb.saturation = saturation;
626
+ if (colorModel === 'rgb') {
627
+ state.rgb = hsvToRgb(hue, saturation, value);
628
+ }
629
+ return true;
630
+ }),
631
+ hasFeature: jest.fn().mockImplementation((feature: string) => features.has(feature)),
632
+ getColorModel: jest.fn().mockImplementation(() => colorModel),
633
+ getBrightness: jest.fn().mockImplementation(() => state.brightness),
634
+ getColorTempPercent: jest.fn().mockImplementation(() => state.colorTempPercent),
635
+ getColorTempKelvin: jest.fn().mockImplementation(() => percentToKelvin(state.colorTempPercent)),
636
+ getColorHue: jest.fn().mockImplementation(() => state.hue),
637
+ getColorSaturation: jest.fn().mockImplementation(() => state.saturation),
638
+ getColorValue: jest.fn().mockImplementation(() => state.value),
639
+ getRGBValues: jest.fn().mockImplementation(() => state.rgb),
640
+ };
641
+
642
+ return mockBulb as jest.Mocked<VeSyncBulb>;
537
643
  };
538
644
 
539
645
  export interface MockDimmerConfig {
@@ -7,6 +7,17 @@ import { DeviceCapabilities, VeSyncBulb, VeSyncDimmerSwitch, VeSyncLightDevice }
7
7
  const MIN_COLOR_TEMP = 140; // 7143K (cool)
8
8
  const MAX_COLOR_TEMP = 500; // 2000K (warm)
9
9
  const DEFAULT_COLOR_TEMP = MIN_COLOR_TEMP;
10
+ const DEVICE_MIN_KELVIN = 2700;
11
+ const DEVICE_MAX_KELVIN = 6500;
12
+
13
+ const clamp = (value: number, min: number, max: number): number => Math.min(Math.max(value, min), max);
14
+ const clampMired = (value: number): number => clamp(Math.round(value), MIN_COLOR_TEMP, MAX_COLOR_TEMP);
15
+ const percentToKelvin = (percent: number): number => DEVICE_MIN_KELVIN + ((DEVICE_MAX_KELVIN - DEVICE_MIN_KELVIN) * clamp(percent, 0, 100) / 100);
16
+ const kelvinToPercent = (kelvin: number): number => ((clamp(kelvin, DEVICE_MIN_KELVIN, DEVICE_MAX_KELVIN) - DEVICE_MIN_KELVIN) / (DEVICE_MAX_KELVIN - DEVICE_MIN_KELVIN)) * 100;
17
+ const kelvinToMired = (kelvin: number): number => Math.round(1_000_000 / clamp(kelvin, DEVICE_MIN_KELVIN, DEVICE_MAX_KELVIN));
18
+ const miredToKelvin = (mired: number): number => clamp(Math.round(1_000_000 / clamp(mired, MIN_COLOR_TEMP, MAX_COLOR_TEMP)), DEVICE_MIN_KELVIN, DEVICE_MAX_KELVIN);
19
+ const percentToMired = (percent: number): number => clampMired(kelvinToMired(percentToKelvin(percent)));
20
+ const miredToPercent = (mired: number): number => kelvinToPercent(miredToKelvin(mired));
10
21
 
11
22
  export class LightAccessory extends BaseAccessory {
12
23
  protected readonly device: VeSyncLightDevice;
@@ -115,46 +126,82 @@ export class LightAccessory extends BaseAccessory {
115
126
  saturation?: number;
116
127
  };
117
128
 
118
- // Update active state
119
- const isActive = lightDetails.deviceStatus === 'on';
129
+ const deviceStatus = lightDetails.deviceStatus ?? this.device.deviceStatus;
130
+ const isActive = deviceStatus === 'on';
120
131
  this.updateCharacteristicValue(
121
132
  this.platform.Characteristic.On,
122
133
  isActive
123
134
  );
124
135
 
125
136
  // Update brightness if supported
126
- if (this.capabilities.hasBrightness && lightDetails.brightness !== undefined) {
127
- this.updateCharacteristicValue(
128
- this.platform.Characteristic.Brightness,
129
- lightDetails.brightness
130
- );
137
+ if (this.capabilities.hasBrightness) {
138
+ let brightness: number | undefined = lightDetails.brightness;
131
139
 
132
- if (this.isDimmerDevice && lightDetails.brightness > 0) {
133
- this.lastKnownDimmerBrightness = lightDetails.brightness;
140
+ if (!this.isDimmerDevice) {
141
+ const bulb = this.device as VeSyncBulb;
142
+ if (typeof bulb.getBrightness === 'function') {
143
+ brightness = bulb.getBrightness();
144
+ }
145
+ }
146
+
147
+ if (brightness !== undefined) {
148
+ this.updateCharacteristicValue(
149
+ this.platform.Characteristic.Brightness,
150
+ brightness
151
+ );
152
+
153
+ if (this.isDimmerDevice && brightness > 0) {
154
+ this.lastKnownDimmerBrightness = brightness;
155
+ }
134
156
  }
135
157
  }
136
158
 
137
159
  // Update color temperature if supported
138
- if (this.capabilities.hasColorTemp && lightDetails.colorTemp !== undefined) {
139
- this.updateCharacteristicValue(
140
- this.platform.Characteristic.ColorTemperature,
141
- lightDetails.colorTemp
142
- );
143
- }
160
+ if (this.capabilities.hasColorTemp && !this.isDimmerDevice) {
161
+ const bulb = this.device as VeSyncBulb;
162
+ let tempPercent: number | undefined;
163
+
164
+ if (typeof bulb.getColorTempPercent === 'function') {
165
+ tempPercent = bulb.getColorTempPercent();
166
+ } else if (typeof lightDetails.colorTemp === 'number') {
167
+ tempPercent = lightDetails.colorTemp > 100
168
+ ? miredToPercent(lightDetails.colorTemp)
169
+ : lightDetails.colorTemp;
170
+ }
144
171
 
145
- // Update color if supported
146
- if (this.capabilities.hasColor) {
147
- if (lightDetails.hue !== undefined) {
172
+ if (typeof tempPercent === 'number' && !Number.isNaN(tempPercent) && tempPercent > 0) {
173
+ const mired = percentToMired(tempPercent);
148
174
  this.updateCharacteristicValue(
149
- this.platform.Characteristic.Hue,
150
- lightDetails.hue
175
+ this.platform.Characteristic.ColorTemperature,
176
+ mired
151
177
  );
152
178
  }
153
- if (lightDetails.saturation !== undefined) {
154
- this.updateCharacteristicValue(
155
- this.platform.Characteristic.Saturation,
156
- lightDetails.saturation
157
- );
179
+ }
180
+
181
+ // Update color if supported
182
+ if (this.capabilities.hasColor && !this.isDimmerDevice) {
183
+ const bulb = this.device as VeSyncBulb;
184
+ const colorModel = typeof bulb.getColorModel === 'function' ? bulb.getColorModel() : 'none';
185
+
186
+ if (colorModel === 'rgb' && typeof bulb.getRGBValues === 'function') {
187
+ const rgb = bulb.getRGBValues();
188
+ const hsv = this.rgbToHsv(rgb.red, rgb.green, rgb.blue);
189
+ this.updateCharacteristicValue(this.platform.Characteristic.Hue, hsv.hue);
190
+ this.updateCharacteristicValue(this.platform.Characteristic.Saturation, hsv.saturation);
191
+ } else {
192
+ const hue = typeof bulb.getColorHue === 'function'
193
+ ? bulb.getColorHue()
194
+ : lightDetails.hue;
195
+ const saturation = typeof bulb.getColorSaturation === 'function'
196
+ ? bulb.getColorSaturation()
197
+ : lightDetails.saturation;
198
+
199
+ if (typeof hue === 'number') {
200
+ this.updateCharacteristicValue(this.platform.Characteristic.Hue, hue);
201
+ }
202
+ if (typeof saturation === 'number') {
203
+ this.updateCharacteristicValue(this.platform.Characteristic.Saturation, saturation);
204
+ }
158
205
  }
159
206
  }
160
207
 
@@ -193,10 +240,26 @@ export class LightAccessory extends BaseAccessory {
193
240
  }
194
241
 
195
242
  const model = this.device.deviceType.toUpperCase();
243
+ const bulb = this.device as VeSyncBulb;
244
+ const supports = typeof bulb.hasFeature === 'function'
245
+ ? bulb.hasFeature.bind(bulb)
246
+ : (feature: string): boolean => {
247
+ if (feature === 'color_temp') {
248
+ return model.includes('CW') || model.includes('MC') || model === 'XYD0001';
249
+ }
250
+ if (feature === 'rgb_shift') {
251
+ return model.includes('MC') || model === 'XYD0001';
252
+ }
253
+ if (feature === 'dimmable') {
254
+ return true;
255
+ }
256
+ return false;
257
+ };
258
+
196
259
  return {
197
- hasBrightness: true, // All VeSync bulbs support brightness
198
- hasColorTemp: model.includes('CW') || model.includes('MC'), // CW and MC models support color temperature
199
- hasColor: model.includes('MC') || model === 'XYD0001', // MC models and XYD0001 support color
260
+ hasBrightness: typeof (this.device as any).setBrightness === 'function',
261
+ hasColorTemp: supports('color_temp'),
262
+ hasColor: supports('rgb_shift'),
200
263
  hasSpeed: false,
201
264
  hasHumidity: false,
202
265
  hasAirQuality: false,
@@ -233,6 +296,10 @@ export class LightAccessory extends BaseAccessory {
233
296
  }
234
297
 
235
298
  private async getBrightness(): Promise<CharacteristicValue> {
299
+ const deviceWithGetter = this.device as VeSyncDimmerSwitch & { getBrightness?: () => number };
300
+ if (typeof deviceWithGetter.getBrightness === 'function') {
301
+ return deviceWithGetter.getBrightness();
302
+ }
236
303
  return this.device.brightness;
237
304
  }
238
305
 
@@ -277,7 +344,19 @@ export class LightAccessory extends BaseAccessory {
277
344
  }
278
345
 
279
346
  const bulb = this.device as VeSyncBulb;
280
- return bulb.colorTemp || DEFAULT_COLOR_TEMP;
347
+ if (typeof bulb.getColorTempPercent === 'function') {
348
+ const percent = bulb.getColorTempPercent();
349
+ if (percent > 0) {
350
+ return percentToMired(percent);
351
+ }
352
+ }
353
+
354
+ const stored = this.accessory.context?.device?.details?.colorTemp;
355
+ if (typeof stored === 'number') {
356
+ return clampMired(stored);
357
+ }
358
+
359
+ return DEFAULT_COLOR_TEMP;
281
360
  }
282
361
 
283
362
  private async setColorTemperature(value: CharacteristicValue): Promise<void> {
@@ -292,7 +371,8 @@ export class LightAccessory extends BaseAccessory {
292
371
  throw new Error('Device does not support color temperature');
293
372
  }
294
373
 
295
- const success = await bulb.setColorTemperature(value as number);
374
+ const percent = miredToPercent(Number(value));
375
+ const success = await bulb.setColorTemperature(percent);
296
376
 
297
377
  if (!success) {
298
378
  throw new Error(`Failed to set color temperature to ${value}`);
@@ -310,7 +390,18 @@ export class LightAccessory extends BaseAccessory {
310
390
  }
311
391
 
312
392
  const bulb = this.device as VeSyncBulb;
313
- return bulb.hue || 0;
393
+ const colorModel = typeof bulb.getColorModel === 'function' ? bulb.getColorModel() : 'none';
394
+
395
+ if (colorModel === 'rgb' && typeof bulb.getRGBValues === 'function') {
396
+ const rgb = bulb.getRGBValues();
397
+ return this.rgbToHsv(rgb.red, rgb.green, rgb.blue).hue;
398
+ }
399
+
400
+ if (typeof bulb.getColorHue === 'function') {
401
+ return bulb.getColorHue();
402
+ }
403
+
404
+ return 0;
314
405
  }
315
406
 
316
407
  private async getSaturation(): Promise<CharacteristicValue> {
@@ -319,7 +410,18 @@ export class LightAccessory extends BaseAccessory {
319
410
  }
320
411
 
321
412
  const bulb = this.device as VeSyncBulb;
322
- return bulb.saturation || 0;
413
+ const colorModel = typeof bulb.getColorModel === 'function' ? bulb.getColorModel() : 'none';
414
+
415
+ if (colorModel === 'rgb' && typeof bulb.getRGBValues === 'function') {
416
+ const rgb = bulb.getRGBValues();
417
+ return this.rgbToHsv(rgb.red, rgb.green, rgb.blue).saturation;
418
+ }
419
+
420
+ if (typeof bulb.getColorSaturation === 'function') {
421
+ return bulb.getColorSaturation();
422
+ }
423
+
424
+ return 0;
323
425
  }
324
426
 
325
427
  private async setHue(value: CharacteristicValue): Promise<void> {
@@ -334,9 +436,18 @@ export class LightAccessory extends BaseAccessory {
334
436
  throw new Error('Device does not support color');
335
437
  }
336
438
 
439
+ const fallbackSaturation = this.service.getCharacteristic(this.platform.Characteristic.Saturation).value as number | undefined;
440
+ const currentSaturation = typeof bulb.getColorSaturation === 'function'
441
+ ? bulb.getColorSaturation()
442
+ : (typeof fallbackSaturation === 'number' ? fallbackSaturation : 0);
443
+ const currentValue = typeof bulb.getColorValue === 'function'
444
+ ? bulb.getColorValue()
445
+ : 100;
446
+
337
447
  const success = await bulb.setColor(
338
- value as number,
339
- bulb.saturation || 0
448
+ Number(value),
449
+ currentSaturation || 0,
450
+ currentValue || 100
340
451
  );
341
452
 
342
453
  if (!success) {
@@ -361,9 +472,18 @@ export class LightAccessory extends BaseAccessory {
361
472
  throw new Error('Device does not support color');
362
473
  }
363
474
 
475
+ const fallbackHue = this.service.getCharacteristic(this.platform.Characteristic.Hue).value as number | undefined;
476
+ const currentHue = typeof bulb.getColorHue === 'function'
477
+ ? bulb.getColorHue()
478
+ : (typeof fallbackHue === 'number' ? fallbackHue : 0);
479
+ const currentValue = typeof bulb.getColorValue === 'function'
480
+ ? bulb.getColorValue()
481
+ : 100;
482
+
364
483
  const success = await bulb.setColor(
365
- bulb.hue || 0,
366
- value as number
484
+ currentHue || 0,
485
+ Number(value),
486
+ currentValue || 100
367
487
  );
368
488
 
369
489
  if (!success) {
@@ -28,14 +28,23 @@ export interface VeSyncDeviceWithPower extends VeSyncBaseDevice {
28
28
  export interface VeSyncDeviceWithBrightness extends VeSyncDeviceWithPower {
29
29
  brightness: number;
30
30
  setBrightness(value: number): Promise<boolean>;
31
+ getBrightness?(): number;
31
32
  }
32
33
 
33
34
  export interface VeSyncDeviceWithColor extends VeSyncDeviceWithBrightness {
34
35
  colorTemp?: number;
35
36
  hue?: number;
36
37
  saturation?: number;
38
+ getColorTempPercent?(): number;
39
+ getColorTempKelvin?(): number;
40
+ getColorHue?(): number;
41
+ getColorSaturation?(): number;
42
+ getColorValue?(): number;
43
+ getRGBValues?(): { red: number; green: number; blue: number };
44
+ hasFeature?(feature: string): boolean;
45
+ getColorModel?(): string;
37
46
  setColorTemperature?(value: number): Promise<boolean>;
38
- setColor?(hue: number, saturation: number): Promise<boolean>;
47
+ setColor?(hue: number, saturation: number, value?: number): Promise<boolean>;
39
48
  }
40
49
 
41
50
  export interface VeSyncDeviceWithSpeed extends VeSyncDeviceWithPower {