Skip to content

Commit

Permalink
New: Add color support for backlight/ambilight
Browse files Browse the repository at this point in the history
Fixes: #102, #78, #65, #40
  • Loading branch information
vieira committed Nov 10, 2023
1 parent ec7ca4f commit a44be2c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
102 changes: 102 additions & 0 deletions bulbs/backlight/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { isInteger } = Number;

const BacklightColor = (Device) => {
let hue;
let sat;

return class extends Device {
constructor(props, platform) {
super(props, platform);
this.backlightHue = props['bg_hue'];
this.backlightSat = props['bg_sat'];

const { Hue, Saturation } = global.Characteristic;

(
this.backlightService.getCharacteristic(Hue) ||
this.backlightService.addCharacteristic(Hue)
)
.on('set', async (value, callback) => {
try {
await this.setBacklightColor(value, null);
callback(null);
} catch (err) {
callback(err);
}
})
.updateValue(this.backlightHue);

(
this.backlightService.getCharacteristic(Saturation) ||
this.backlightService.addCharacteristic(Saturation)
)
.on('set', async (value, callback) => {
try {
await this.setBacklightColor(null, value);
callback(null);
} catch (err) {
callback(err);
}
})
.updateValue(this.backlightSat);
}

get backlightHue() {
return this._backlightHue;
}

set backlightHue(value) {
this._backlightHue = Number(value);
}

get backlightSat() {
return this._backlightSat;
}

set backlightSat(value) {
this._backlightSat = Number(value);
}

updateStateFromProp(prop, value) {
if (prop === 'bg_hue') {
this.backlightHue = value;
this.backlightService
.getCharacteristic(global.Characteristic.Hue)
.updateValue(this.backlightHue);
return;
}
if (prop === 'bg_sat') {
this.backlightSat = value;
this.backlightService
.getCharacteristic(global.Characteristic.Saturation)
.updateValue(this.backlightSat);
return;
}
if (prop === 'bg_rgb') {
return;
}
super.updateStateFromProp(prop, value);
}

async setBacklightColor(hv, sv) {
hue = isInteger(hue) ? hue : hv;
sat = isInteger(sat) ? sat : sv;
if (!isInteger(hue) || !isInteger(sat)) return;

const { color: transition = 400 } = this.config.transitions || {};
await this.setBacklightPower(true);
const req = {
method: 'bg_set_hsv',
params: [hue, sat, 'smooth', transition],
};
return this.sendCmd(req).then(() => {
this._backlightHue = hue;
this._backlightSat = sat;
hue = null;
sat = null;
});
}
};
};

module.exports = BacklightColor;
6 changes: 6 additions & 0 deletions platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Color = require('./bulbs/color');
const Temperature = require('./bulbs/temperature');
const Backlight = require('./bulbs/backlight/bulb');
const BacklightBrightness = require('./bulbs/backlight/brightness');
const BacklightColor = require('./bulbs/backlight/color');
const { getDeviceId, getName, blacklist, sleep, pipe } = require('./utils');

class YeePlatform {
Expand Down Expand Up @@ -158,6 +159,11 @@ class YeePlatform {
mixins.push(BacklightBrightness);
}

if (features.includes('bg_set_hsv')) {
this.log(`Device ${name} supports backlight color`);
mixins.push(BacklightColor);
}

const Bulb = class extends pipe(...mixins)(YeeBulb) {};
return new Bulb({ id, model, endpoint, accessory, limits, ...props }, this);
}
Expand Down

0 comments on commit a44be2c

Please sign in to comment.