4
4
import Yeelight from 'yeelight2' ;
5
5
import convert from 'color-convert' ;
6
6
7
- import { sanitizeState , hexToRgbInt , normalize , colorTemperatureToRgbInt } from './utils' ;
7
+ import { sanitizeState , hexToRgbInt , normalize , colorTemperatureToRgbInt , clamp } from './utils' ;
8
8
9
9
export default function YeeLightNodeOut ( RED ) {
10
10
return function ( config ) {
@@ -15,71 +15,68 @@ export default function YeeLightNodeOut(RED) {
15
15
try {
16
16
msg . payload = JSON . parse ( msg . payload ) ;
17
17
} catch ( e ) {
18
- node . error ( `Yeelight: Error during payload parsing\n${ e } \n${ msg . payload } ` ) ;
19
- return ;
18
+ return node . error (
19
+ `Yeelight: Error during payload parsing\n${ e } \n${ msg . payload } `
20
+ ) ;
20
21
}
21
22
}
22
23
23
24
if ( typeof msg . payload !== 'object' ) {
24
- node . error ( `Yeelight: Invalid payload\n${ msg . payload } ` ) ;
25
- return ;
25
+ return node . error ( `Yeelight: Invalid payload\n${ msg . payload } ` ) ;
26
26
}
27
27
28
- const { on, hex, bri, hue, sat, duration, ct } = msg . payload ;
28
+ const { on, hex, bri, hue, sat, duration = 500 , ct } = msg . payload ;
29
29
30
30
if ( on === false ) {
31
- node . serverConfig . yeelight . set_power ( on , null , duration ) ;
32
- return ;
31
+ return node . serverConfig . yeelight . set_power ( on , null , duration ) ;
33
32
}
34
33
35
34
node . serverConfig . yeelight . sync ( ) . then ( state => {
35
+ let colorValue ;
36
+ let colorMode ;
36
37
const currentState = sanitizeState ( state ) . state ;
37
- let rgbIntToTurnTo ;
38
- let briToTurnTo ;
38
+ let briToTurnTo = clamp ( normalize ( bri || currentState . bri , 255 , 100 ) , 1 , 100 ) ;
39
39
40
40
if ( typeof ct !== 'undefined' ) {
41
- rgbIntToTurnTo = colorTemperatureToRgbInt ( ct ) ;
42
- briToTurnTo = normalize ( bri || currentState . bri , 255 , 100 ) ;
41
+ colorMode = 2 ;
42
+ colorValue = ct ;
43
43
} else if ( typeof hex !== 'undefined' ) {
44
- rgbIntToTurnTo = hexToRgbInt ( hex ) ;
45
- briToTurnTo = ( bri && normalize ( bri , 255 , 100 ) ) || convert . hex . hsv ( hex ) [ 2 ] ;
44
+ colorMode = 1 ;
45
+ colorValue = hexToRgbInt ( hex ) ;
46
+ // if no bri was specified, calculate from hex value
47
+ briToTurnTo = bri ? briToTurnTo : clamp ( convert . hex . hsv ( hex ) [ 2 ] , 1 , 100 ) ;
46
48
} else if (
47
49
typeof hue !== 'undefined' ||
48
50
typeof sat !== 'undefined' ||
49
51
typeof bri !== 'undefined'
50
52
) {
51
- rgbIntToTurnTo = hexToRgbInt (
53
+ colorMode = 1 ;
54
+ colorValue = hexToRgbInt (
52
55
convert . hsv . hex (
53
56
normalize ( hue || currentState . hue , 65535 , 359 ) ,
54
57
normalize ( sat || currentState . sat , 255 , 100 ) ,
55
- normalize ( bri || currentState . bri , 255 , 100 )
58
+ briToTurnTo
56
59
)
57
60
) ;
58
- briToTurnTo = normalize ( bri || currentState . bri , 255 , 100 ) ;
59
61
} else if ( on ) {
60
- node . serverConfig . yeelight . set_power ( on , null , duration ) ;
61
- return ;
62
- }
63
-
64
- const flowExpression = `${ duration || 500 } , 1, ${ rgbIntToTurnTo } , ${ briToTurnTo } ` ;
65
-
66
- let preparePromise ;
67
-
68
- if ( currentState . on ) {
69
- preparePromise = Promise . resolve ( ) ;
70
- } else {
71
- preparePromise = node . serverConfig . yeelight . set_scene (
72
- 'color' ,
73
- rgbIntToTurnTo ,
74
- 1
75
- ) ;
62
+ return node . serverConfig . yeelight . set_power ( on , null , duration ) ;
76
63
}
77
64
78
- preparePromise
79
- . then ( ( ) => {
80
- node . serverConfig . yeelight . start_cf ( 1 , 1 , flowExpression ) ;
81
- } )
82
- . catch ( e => console . log ( 'yeelight error' , e ) ) ;
65
+ return node . serverConfig . yeelight
66
+ . set_scene (
67
+ 'cf' ,
68
+ 1 , // don't repeat
69
+ 1 , // keep in end-state
70
+ `${ duration } , ${ colorMode } , ${ colorValue } , ${ briToTurnTo } `
71
+ )
72
+ . catch ( e => {
73
+ if ( e . code === - 5000 ) {
74
+ return node . error (
75
+ 'Yeelight "general error (code -5000)". Payload might be invalid.'
76
+ ) ;
77
+ }
78
+ throw e ;
79
+ } ) ;
83
80
} ) ;
84
81
} ;
85
82
0 commit comments