Skip to content

Commit 452049b

Browse files
committed
Add hue lights example
1 parent 060681d commit 452049b

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

examples-manifest.json

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"fname": "hue-lights-control.js",
4+
"title": "Controlling hue lights with Shelly BLU Button or virtual buttons",
5+
"description": "This script allows you to control your hue lights with a Shelly BLU Button or virtual buttons. You can turn on/off lights, change brightness and color temperature. (Requires firmware version: 1.3 or newer)"
6+
},
27
{
38
"fname": "load-shedding.js",
49
"title": "Load shedding with Shelly Pro4PM and Pro3EM",

hue-lights-control.js

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const CONFIG = {
2+
ip: '', // Hue bridge IP
3+
user: '', // Hue bridge user
4+
lights: [1, 2, 3], // Light bulb IDs
5+
6+
handlers: {
7+
// bthomesensor:201 - first button
8+
'bthomesensor:201': {
9+
// on single push
10+
single_push: [
11+
{
12+
// by omitting the id, the request is sent to all bulb from the config
13+
on: true,
14+
bri: 20,
15+
hue: 3500
16+
}
17+
]
18+
},
19+
// bthomesensor:202 - second button
20+
'bthomesensor:202': {
21+
single_push: [
22+
{
23+
on: true,
24+
hue: 200,
25+
bri: 60
26+
},
27+
{
28+
id: 2, // specifying the bulb ID will send request only to this device
29+
on: true,
30+
hue: 6000,
31+
bri: 20
32+
}
33+
],
34+
double_push: [
35+
{
36+
on: false,
37+
}
38+
]
39+
},
40+
// button:201 - virtual button
41+
'button:201': {
42+
single_push: [
43+
{
44+
id: 1,
45+
on: true,
46+
hue: 5124,
47+
bri: 250
48+
}
49+
]
50+
}
51+
}
52+
};
53+
54+
/**
55+
* Bulk set the state of all light bulbs
56+
* @param {*} on bulb id
57+
* @param {*} bri brightness
58+
* @param {*} hue hue value
59+
* @param {*} sat saturation
60+
*/
61+
function SetAll(on, bri, hue, sat) {
62+
for (const id of CONFIG.lights) {
63+
Set(id, on, bri, hue, sat);
64+
}
65+
}
66+
67+
/**
68+
* Set the light bulb state
69+
* @param {*} id bulb id
70+
* @param {*} on on/off state
71+
* @param {*} bri brightness
72+
* @param {*} hue hue value
73+
* @param {*} sat saturation
74+
*/
75+
function Set(id, on, bri, hue, sat) {
76+
let body = {};
77+
78+
if (typeof on === 'boolean') {
79+
body.on = on;
80+
}
81+
82+
if (typeof bri === 'number') {
83+
body.bri = bri;
84+
}
85+
86+
if (typeof hue === 'number') {
87+
body.hue = hue;
88+
}
89+
90+
if (typeof sat === 'number') {
91+
body.sat = sat;
92+
}
93+
94+
const uri =
95+
'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/lights/' + id + '/state';
96+
97+
Shelly.call('http.request', {
98+
method: 'PUT',
99+
url: uri,
100+
body: body,
101+
});
102+
}
103+
104+
function onEvent(event_data) {
105+
const component = event_data.component;
106+
const info = event_data.info;
107+
108+
if (!info) {
109+
return;
110+
}
111+
112+
const handlers = CONFIG.handlers[component];
113+
114+
if (!handlers) {
115+
console.log('no handler for ', sensorId);
116+
return;
117+
}
118+
119+
const event = info.event;
120+
121+
const handler = handlers[event];
122+
123+
if (!Array.isArray(handler)) {
124+
console.log('no handler for', event);
125+
return;
126+
}
127+
128+
for (const obj of handler) {
129+
let bulbId = obj.id;
130+
let hue = obj.hue;
131+
let bri = obj.bri;
132+
let on = obj.on;
133+
let sat = obj.sat;
134+
135+
if (typeof bulbId === 'number') {
136+
Set(bulbId, on, bri, hue, sat);
137+
}
138+
else {
139+
SetAll(on, bri, hue, sat);
140+
}
141+
}
142+
}
143+
144+
Shelly.addEventHandler(onEvent);

0 commit comments

Comments
 (0)