Skip to content

Commit

Permalink
Merge pull request #100 from ALLTERCO/hue-lights-control
Browse files Browse the repository at this point in the history
Hue lights example
  • Loading branch information
taulfsime authored May 29, 2024
2 parents bce56c5 + e02a672 commit ecadc31
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"fname": "hue-lights-control.js",
"title": "Controlling hue lights with Shelly BLU Button or virtual buttons",
"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)"
},
{
"fname": "control-ha-light-entity-with-boolean.js",
"title": "Control Light entity from HA via virtual boolean component",
Expand Down
144 changes: 144 additions & 0 deletions hue-lights-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const CONFIG = {
ip: '', // Hue bridge IP
user: '', // Hue bridge user
lights: [1, 2, 3], // Light bulb IDs

handlers: {
// bthomesensor:201 - first button
'bthomesensor:201': {
// on single push
single_push: [
{
// by omitting the id, the request is sent to all bulb from the config
on: true,
bri: 20,
hue: 3500
}
]
},
// bthomesensor:202 - second button
'bthomesensor:202': {
single_push: [
{
on: true,
hue: 200,
bri: 60
},
{
id: 2, // specifying the bulb ID will send request only to this device
on: true,
hue: 6000,
bri: 20
}
],
double_push: [
{
on: false,
}
]
},
// button:201 - virtual button
'button:201': {
single_push: [
{
id: 1,
on: true,
hue: 5124,
bri: 250
}
]
}
}
};

/**
* Bulk set the state of all light bulbs
* @param {*} on bulb id
* @param {*} bri brightness
* @param {*} hue hue value
* @param {*} sat saturation
*/
function SetAll(on, bri, hue, sat) {
for (const id of CONFIG.lights) {
Set(id, on, bri, hue, sat);
}
}

/**
* Set the light bulb state
* @param {*} id bulb id
* @param {*} on on/off state
* @param {*} bri brightness
* @param {*} hue hue value
* @param {*} sat saturation
*/
function Set(id, on, bri, hue, sat) {
let body = {};

if (typeof on === 'boolean') {
body.on = on;
}

if (typeof bri === 'number') {
body.bri = bri;
}

if (typeof hue === 'number') {
body.hue = hue;
}

if (typeof sat === 'number') {
body.sat = sat;
}

const uri =
'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/lights/' + id + '/state';

Shelly.call('http.request', {
method: 'PUT',
url: uri,
body: body,
});
}

function onEvent(event_data) {
const component = event_data.component;
const info = event_data.info;

if (!info) {
return;
}

const handlers = CONFIG.handlers[component];

if (!handlers) {
console.log('no handler for ', sensorId);
return;
}

const event = info.event;

const handler = handlers[event];

if (!Array.isArray(handler)) {
console.log('no handler for', event);
return;
}

for (const obj of handler) {
let bulbId = obj.id;
let hue = obj.hue;
let bri = obj.bri;
let on = obj.on;
let sat = obj.sat;

if (typeof bulbId === 'number') {
Set(bulbId, on, bri, hue, sat);
}
else {
SetAll(on, bri, hue, sat);
}
}
}

Shelly.addEventHandler(onEvent);

0 comments on commit ecadc31

Please sign in to comment.