11import {
2+ Characteristic ,
23 CharacteristicGetCallback ,
34 CharacteristicSetCallback ,
45 CharacteristicValue ,
6+ Formats ,
57} from "homebridge" ;
68import { TuyaWebCharacteristic } from "./base" ;
79import { MapRange } from "../../helpers/MapRange" ;
@@ -22,7 +24,36 @@ export class ColorTemperatureCharacteristic extends TuyaWebCharacteristic {
2224 return accessory . deviceConfig . data . color_temp !== undefined ;
2325 }
2426
25- private rangeMapper = MapRange . tuya ( 140 , 500 ) . homeKit ( 10000 , 1000 ) ;
27+ public setProps ( char ?: Characteristic ) : Characteristic | undefined {
28+ return char ?. setProps ( {
29+ format : Formats . INT ,
30+ minValue : this . minHomekit ,
31+ maxValue : this . maxHomekit ,
32+ } ) ;
33+ }
34+
35+ public get minKelvin ( ) : number {
36+ const data = this . accessory . deviceConfig . config ;
37+ return Number ( data ?. min_kelvin ) || 1000000 / 500 ;
38+ }
39+
40+ public get maxKelvin ( ) : number {
41+ const data = this . accessory . deviceConfig . config ;
42+ return Number ( data ?. max_kelvin ) || 1000000 / 140 ;
43+ }
44+
45+ public get minHomekit ( ) : number {
46+ return 1000000 / this . maxKelvin ;
47+ }
48+
49+ public get maxHomekit ( ) : number {
50+ return 1000000 / this . minKelvin ;
51+ }
52+
53+ public rangeMapper = MapRange . tuya ( this . maxKelvin , this . minKelvin ) . homeKit (
54+ this . minHomekit ,
55+ this . maxHomekit
56+ ) ;
2657
2758 public getRemoteValue ( callback : CharacteristicGetCallback ) : void {
2859 this . accessory
@@ -46,7 +77,7 @@ export class ColorTemperatureCharacteristic extends TuyaWebCharacteristic {
4677 }
4778
4879 // Set device state in Tuya Web API
49- const value = Math . round ( this . rangeMapper . tuyaToHomekit ( homekitValue ) ) ;
80+ const value = Math . round ( this . rangeMapper . homekitToTuya ( homekitValue ) ) ;
5081
5182 this . accessory
5283 . setDeviceState ( "colorTemperatureSet" , { value } , { color_temp : value } )
@@ -59,15 +90,38 @@ export class ColorTemperatureCharacteristic extends TuyaWebCharacteristic {
5990
6091 updateValue ( data : DeviceState , callback ?: CharacteristicGetCallback ) : void {
6192 if ( data ?. color_temp !== undefined ) {
93+ const tuyaValue = data . color_temp ;
6294 const homekitColorTemp = Math . round (
63- this . rangeMapper . homekitToTuya ( Number ( data . color_temp ) )
95+ this . rangeMapper . tuyaToHomekit ( Number ( data . color_temp ) )
6496 ) ;
97+
98+ if ( homekitColorTemp > this . maxHomekit ) {
99+ this . warn (
100+ "Characteristic 'ColorTemperature' will receive value higher than allowed mired (%s) since provided Tuya kelvin value (%s) " +
101+ "is lower then configured minimum Tuya kelvin value (%s). Please update your configuration!" ,
102+ homekitColorTemp ,
103+ tuyaValue ,
104+ this . rangeMapper . tuyaStart
105+ ) ;
106+ } else if ( homekitColorTemp < this . minHomekit ) {
107+ this . warn (
108+ "Characteristic 'ColorTemperature' will receive value lower than allowed mired (%s) since provided Tuya kelvin value (%s) " +
109+ "exceeds configured maximum Tuya kelvin value (%s). Please update your configuration!" ,
110+ homekitColorTemp ,
111+ tuyaValue ,
112+ this . rangeMapper . tuyaEnd
113+ ) ;
114+ }
115+
65116 this . accessory . setCharacteristic (
66117 this . homekitCharacteristic ,
67118 homekitColorTemp ,
68119 ! callback
69120 ) ;
70121 callback && callback ( null , homekitColorTemp ) ;
122+ } else {
123+ callback &&
124+ callback ( new Error ( "Could not find required property 'color_temp'" ) ) ;
71125 }
72126 }
73127}
0 commit comments