Skip to content

Commit 227cb43

Browse files
author
Matt
authored
Merge pull request #3 from mattmattmatt/set-color-temperature
Set color temperature
2 parents bf66a70 + 71e1800 commit 227cb43

6 files changed

+33
-12
lines changed

CHANGELOG.md

Whitespace-only changes.

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
[Node-RED](http://nodered.org) nodes to control [MiHome/Xiaomi Yeelights](https://www.yeelight.com/) from your smart home, somewhat [API compatible with node-red-contrib-node-hue nodes](https://github.com/jdomeij/node-red-contrib-node-hue#input-node).
1212

13+
1314
## Install
1415

1516
Run the following command in your Node-RED user directory – typically `~/.node-red`:
@@ -24,6 +25,7 @@ Provides two palette nodes – one to send control commands to a Yeelight, and o
2425

2526
![](https://github.com/mattmattmatt/node-red-contrib-yeelight-compat-hue/blob/master/tooling/nodes.png?raw=true)
2627

28+
2729
### Output node
2830

2931
Sets the state of the selected Yeelight device.
@@ -34,6 +36,7 @@ Sets the state of the selected Yeelight device.
3436
| :---| :---|
3537
| `on` | Sets the `on` state where the value is `true` or `false`|
3638
| `bri` | Sets the brightness value from `0` to `255` |
39+
| `ct` | Sets the color temperature value in Kelvin from `1700` to `6500` |
3740
| `hue` | Sets the hue value from `0` to `65535` |
3841
| `sat` | Sets the saturation value from `0` to `255` |
3942
| `hex` | Sets the rgb value from `#00000` to `#FFFFFF` |
@@ -57,13 +60,18 @@ Sets the state of the selected Yeelight device.
5760
"hex": "#AA00CC",
5861
}
5962
```
60-
The node supports sending [hex values](http://htmlcolorcodes.com/) and [HSV values](https://alloyui.com/examples/color-picker/hsv).
63+
```JSON
64+
{
65+
"ct": 2200,
66+
}
67+
```
68+
The node supports sending [color temperature values](http://www.erco.com/service/rgbw/), [hex values](http://htmlcolorcodes.com/) and [HSV values](https://alloyui.com/examples/color-picker/hsv).
6169
The brightness value will always have to be provided separately and will not be deducted from e.g. a hex value's brightness component.
6270

63-
6471
##### References
6572
This node's input payload structure is based on [node-red-contrib-node-hue](https://github.com/jdomeij/node-red-contrib-node-hue#input-node), which is based on [Node Hue API](https://github.com/peter-murray/node-hue-api#lightstate-options).
6673

74+
6775
### Input node
6876

6977
Returns the current state of the selected Yeelight device.
@@ -106,6 +114,7 @@ Additionally, a fresh state can be requested from the connected Yeelight by send
106114
The `raw` property of `msg.payload` contains the raw state information retrieved from the Yeelight for advanced usage.
107115
Note that value scales are not compatible with _node-red-contrib-node-hue_, and that `hue` value and `rgb` value will not match since only the correct color per `color_mode` is returned by the lamp.
108116

117+
109118
### Configuration node
110119

111120
Configures a Yeelight connection to one light in the local network.

src/YeeLightNodeOut.js

+6-3
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 } from './utils';
7+
import { sanitizeState, hexToRgbInt, normalize, colorTemperatureToRgbInt } from './utils';
88

99
export default function YeeLightNodeOut(RED) {
1010
return function(config) {
@@ -25,7 +25,7 @@ export default function YeeLightNodeOut(RED) {
2525
return;
2626
}
2727

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

3030
if (on === false) {
3131
node.serverConfig.yeelight.set_power(on, null, duration);
@@ -37,7 +37,10 @@ export default function YeeLightNodeOut(RED) {
3737
let rgbIntToTurnTo;
3838
let briToTurnTo;
3939

40-
if (typeof hex !== 'undefined') {
40+
if (typeof ct !== 'undefined') {
41+
rgbIntToTurnTo = colorTemperatureToRgbInt(ct);
42+
briToTurnTo = normalize(bri || currentState.bri, 255, 100);
43+
} else if (typeof hex !== 'undefined') {
4144
rgbIntToTurnTo = hexToRgbInt(hex);
4245
briToTurnTo = (bri && normalize(bri, 255, 100)) || convert.hex.hsv(hex)[2];
4346
} else if (

src/YeeLightNodeState.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Yeelight from 'yeelight2';
44

5-
import { rgbIntToHex, colorTemperatureToRGB, sanitizeState } from './utils';
5+
import { rgbIntToHex, sanitizeState } from './utils';
66

77
export default function YeeLightNodeState(RED) {
88
return function(config) {

src/utils.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function rgbIntToHex(number) {
2626
);
2727
}
2828

29-
export function colorTemperatureToRGB(kelvin) {
29+
export function colorTemperatureToRgb(kelvin) {
3030
var temp = kelvin / 100;
3131
var red, green, blue;
3232

@@ -52,9 +52,9 @@ export function colorTemperatureToRGB(kelvin) {
5252
}
5353

5454
return {
55-
r: clamp(red, 0, 255),
56-
g: clamp(green, 0, 255),
57-
b: clamp(blue, 0, 255),
55+
red: clamp(red, 0, 255),
56+
green: clamp(green, 0, 255),
57+
blue: clamp(blue, 0, 255),
5858
};
5959
}
6060

@@ -73,6 +73,11 @@ export function hexToRgbInt(hex) {
7373
return parseInt('0x' + hex.replace('#', ''), 16);
7474
}
7575

76+
export function colorTemperatureToRgbInt(ct) {
77+
const { red, green, blue } = colorTemperatureToRgb(ct);
78+
return hexToRgbInt('#' + convert.rgb.hex(red, green, blue));
79+
}
80+
7681
export function normalize(value, currentMax = 100, newMax = 255) {
7782
return Math.round(parseInt(value, 10) / currentMax * newMax);
7883
}
@@ -102,7 +107,7 @@ export function sanitizeState(state) {
102107
sat: normalize(convert.hex.hsv(hex)[1]),
103108
};
104109
} else if (state.color_mode === '2') {
105-
const { r: red, g: green, b: blue } = colorTemperatureToRGB(state.ct);
110+
const { red, green, blue } = colorTemperatureToRgb(state.ct);
106111
result.state = {
107112
...result.state,
108113
ct: parseInt(state.ct, 10),

yeelight-compat-hue-out.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ <h4>Example inputs</h4>
6363
<pre><code>{
6464
"on": true,
6565
"bri": 120,
66-
"hex": "#AA00CC",
66+
"hex": "#AA00CC"
67+
}</code></pre>
68+
<pre><code>{
69+
"on": true,
70+
"ct": 2200
6771
}</code></pre>
6872
</p>
6973

0 commit comments

Comments
 (0)