@@ -8,6 +8,7 @@ import { inspect } from "util";
88import { TuyaWebCharacteristic } from "./base" ;
99import { BaseAccessory } from "../BaseAccessory" ;
1010import { DeviceState } from "../../api/response" ;
11+ import { MapRange } from "../../helpers/MapRange" ;
1112
1213export class BrightnessCharacteristic extends TuyaWebCharacteristic {
1314 public static Title = "Characteristic.Brightness" ;
@@ -16,8 +17,6 @@ export class BrightnessCharacteristic extends TuyaWebCharacteristic {
1617 return accessory . platform . Characteristic . Brightness ;
1718 }
1819
19- public static DEFAULT_VALUE = 100 ;
20-
2120 public static isSupportedByAccessory ( accessory ) : boolean {
2221 const configData = accessory . deviceConfig . data ;
2322 return (
@@ -26,6 +25,34 @@ export class BrightnessCharacteristic extends TuyaWebCharacteristic {
2625 ) ;
2726 }
2827
28+ public static DEFAULT_VALUE = 100 ;
29+
30+ public get usesColorBrightness ( ) : boolean {
31+ const deviceData = this . accessory . deviceConfig . data ;
32+ return (
33+ deviceData ?. color_mode !== undefined &&
34+ deviceData ?. color_mode in COLOR_MODES &&
35+ deviceData ?. color ?. brightness !== undefined
36+ ) ;
37+ }
38+
39+ public get rangeMapper ( ) : MapRange {
40+ let minTuya = 10 ;
41+ let maxTuya = 100 ;
42+ if (
43+ this . accessory . deviceConfig . config ?. min_brightness !== undefined &&
44+ this . accessory . deviceConfig . config ?. max_brightness !== undefined
45+ ) {
46+ minTuya = Number ( this . accessory . deviceConfig . config ?. min_brightness ) ;
47+ maxTuya = Number ( this . accessory . deviceConfig . config ?. max_brightness ) ;
48+ } else if ( this . usesColorBrightness ) {
49+ minTuya = 1 ;
50+ maxTuya = 255 ;
51+ }
52+
53+ return MapRange . tuya ( minTuya , maxTuya ) . homeKit ( 0 , 100 ) ;
54+ }
55+
2956 public getRemoteValue ( callback : CharacteristicGetCallback ) : void {
3057 this . accessory
3158 . getDeviceState ( )
@@ -40,11 +67,16 @@ export class BrightnessCharacteristic extends TuyaWebCharacteristic {
4067 homekitValue : CharacteristicValue ,
4168 callback : CharacteristicSetCallback
4269 ) : void {
43- // Set device state in Tuya Web API
44- const value = ( ( homekitValue as number ) / 10 ) * 9 + 10 ;
70+ const value = this . rangeMapper . homekitToTuya ( Number ( homekitValue ) ) ;
4571
4672 this . accessory
47- . setDeviceState ( "brightnessSet" , { value } , { brightness : homekitValue } )
73+ . setDeviceState (
74+ "brightnessSet" ,
75+ { value } ,
76+ this . usesColorBrightness
77+ ? { color : { brightness : value } }
78+ : { brightness : value }
79+ )
4880 . then ( ( ) => {
4981 this . debug ( "[SET] %s" , value ) ;
5082 callback ( ) ;
@@ -53,26 +85,36 @@ export class BrightnessCharacteristic extends TuyaWebCharacteristic {
5385 }
5486
5587 updateValue ( data : DeviceState , callback ?: CharacteristicGetCallback ) : void {
56- // data.brightness only valid for color_mode != color > https://github.com/PaulAnnekov/tuyaha/blob/master/tuyaha/devices/light.py
57- // however, according to local tuya app, calculation for color_mode=color is still incorrect (even more so in lower range)
58- let stateValue : number | undefined ;
59- if (
60- data ?. color_mode !== undefined &&
61- data ?. color_mode in COLOR_MODES &&
62- data ?. color ?. brightness !== undefined
63- ) {
64- stateValue = Number ( data . color . brightness ) ;
65- } else if ( data ?. brightness ) {
66- stateValue = Math . round ( ( Number ( data . brightness ) / 255 ) * 100 ) ;
88+ const tuyaValue = Number (
89+ this . usesColorBrightness ? data . color ?. brightness : data . brightness
90+ ) ;
91+ const homekitValue = this . rangeMapper . tuyaToHomekit ( tuyaValue ) ;
92+
93+ if ( homekitValue > 100 ) {
94+ this . warn (
95+ "Characteristic 'Brightness' will receive value higher than allowed (%s) since provided Tuya value (%s) " +
96+ "exceeds configured maximum Tuya value (%s). Please update your configuration!" ,
97+ homekitValue ,
98+ tuyaValue ,
99+ this . rangeMapper . tuyaEnd
100+ ) ;
101+ } else if ( homekitValue < 0 ) {
102+ this . warn (
103+ "Characteristic 'Brightness' will receive value lower than allowed (%s) since provided Tuya value (%s) " +
104+ "is lower than configured minimum Tuya value (%s). Please update your configuration!" ,
105+ homekitValue ,
106+ tuyaValue ,
107+ this . rangeMapper . tuyaStart
108+ ) ;
67109 }
68110
69- if ( stateValue ) {
111+ if ( homekitValue ) {
70112 this . accessory . setCharacteristic (
71113 this . homekitCharacteristic ,
72- stateValue ,
114+ homekitValue ,
73115 ! callback
74116 ) ;
75- callback && callback ( null , stateValue ) ;
117+ callback && callback ( null , homekitValue ) ;
76118 return ;
77119 }
78120
0 commit comments