Skip to content

Commit ebc1a58

Browse files
author
Matt
committed
feat(): support bulbs that only support color temparature
Previously, only light bulbs that supported RGB mode were properly supported by this node (even when sending `ct` payloads). Now light bulbs that only support color temperature and brightness should be poperly supported.
1 parent 227cb43 commit ebc1a58

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Sets the state of the selected Yeelight device.
4040
| `hue` | Sets the hue value from `0` to `65535` |
4141
| `sat` | Sets the saturation value from `0` to `255` |
4242
| `hex` | Sets the rgb value from `#00000` to `#FFFFFF` |
43-
| `duration` | Sets a transition time in milliseconds, e.g. `4500` means 4.5 seconds |
43+
| `duration` | Sets a transition time in milliseconds, e.g. `4500` means 4.5 seconds. Defaults to `500` |
4444

4545
#### Example payloads
4646

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dist-clean": "rm -rf dist",
2222
"build": "webpack --config tooling/webpack.config.babel.js",
2323
"red": "node-red -v ~/.node-red/flows.json",
24-
"build-red": "npm run build | npm run red",
24+
"build-red": "npm run build | sleep 1.5 && npm run red",
2525
"dev": "nodemon --ext js,html,json --exec \"npm run build-red\" --ignore \"dist/\" ",
2626
"semantic-release": "semantic-release",
2727
"commitmsg": "commitlint -x \"@commitlint/config-conventional\" -e $GIT_PARAMS"

src/YeeLightNodeOut.js

+35-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import Yeelight from 'yeelight2';
55
import convert from 'color-convert';
66

7-
import { sanitizeState, hexToRgbInt, normalize, colorTemperatureToRgbInt } from './utils';
7+
import { sanitizeState, hexToRgbInt, normalize, colorTemperatureToRgbInt, clamp } from './utils';
88

99
export default function YeeLightNodeOut(RED) {
1010
return function(config) {
@@ -15,71 +15,68 @@ export default function YeeLightNodeOut(RED) {
1515
try {
1616
msg.payload = JSON.parse(msg.payload);
1717
} 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+
);
2021
}
2122
}
2223

2324
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}`);
2626
}
2727

28-
const { on, hex, bri, hue, sat, duration, ct } = msg.payload;
28+
const { on, hex, bri, hue, sat, duration = 500, ct } = msg.payload;
2929

3030
if (on === false) {
31-
node.serverConfig.yeelight.set_power(on, null, duration);
32-
return;
31+
return node.serverConfig.yeelight.set_power(on, null, duration);
3332
}
3433

3534
node.serverConfig.yeelight.sync().then(state => {
35+
let colorValue;
36+
let colorMode;
3637
const currentState = sanitizeState(state).state;
37-
let rgbIntToTurnTo;
38-
let briToTurnTo;
38+
let briToTurnTo = clamp(normalize(bri || currentState.bri, 255, 100), 1, 100);
3939

4040
if (typeof ct !== 'undefined') {
41-
rgbIntToTurnTo = colorTemperatureToRgbInt(ct);
42-
briToTurnTo = normalize(bri || currentState.bri, 255, 100);
41+
colorMode = 2;
42+
colorValue = ct;
4343
} 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);
4648
} else if (
4749
typeof hue !== 'undefined' ||
4850
typeof sat !== 'undefined' ||
4951
typeof bri !== 'undefined'
5052
) {
51-
rgbIntToTurnTo = hexToRgbInt(
53+
colorMode = 1;
54+
colorValue = hexToRgbInt(
5255
convert.hsv.hex(
5356
normalize(hue || currentState.hue, 65535, 359),
5457
normalize(sat || currentState.sat, 255, 100),
55-
normalize(bri || currentState.bri, 255, 100)
58+
briToTurnTo
5659
)
5760
);
58-
briToTurnTo = normalize(bri || currentState.bri, 255, 100);
5961
} 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);
7663
}
7764

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+
});
8380
});
8481
};
8582

src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function colorTemperatureToRgb(kelvin) {
5858
};
5959
}
6060

61-
function clamp(x, min, max) {
61+
export function clamp(x, min, max) {
6262
if (x < min) {
6363
return min;
6464
}

0 commit comments

Comments
 (0)