@wanderxjtu/homebridge-yeelighter
Version:
Yeelight support for Homebridge with particular support of ceiling lights
153 lines • 7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemperatureLightService = void 0;
const lightservice_1 = require("./lightservice");
class TemperatureLightService extends lightservice_1.LightService {
constructor(parameters) {
super(parameters);
this.blocker = false;
this.onAttributesUpdated = (newAttributes) => {
this.log(`temperature light updated ${JSON.stringify(newAttributes)}`);
this.powerMode = (0, lightservice_1.powerModeFromColorModeAndActiveMode)(newAttributes.color_mode, newAttributes.active_mode);
this.updateCharacteristic(this.platform.Characteristic.On, newAttributes.power);
this.updateCharacteristic(this.platform.Characteristic.Brightness, this.getBrightness(newAttributes));
this.updateCharacteristic(this.platform.Characteristic.ColorTemperature, (0, lightservice_1.convertColorTemperature)(newAttributes.ct));
};
this.service.displayName = "Temperature Light";
this.installHandlers();
this.adaptiveLightingController = new this.platform.AdaptiveLightingController(this.service, {
controllerMode: 1 /* AUTOMATIC */
});
this.accessory.configureController(this.adaptiveLightingController);
}
getBrightness(attributes) {
if (this.specs.nightLight) {
const { bright, nl_br, active_mode } = attributes;
const br1 = Number(bright);
const br2 = Number(nl_br);
return active_mode === 0 ? br1 / 2 + 50 : br2 / 2;
}
else {
return attributes.bright;
}
}
async sendDebouncedPower(mode) {
if (this.timer) {
this.debug("aborting prior power command");
clearTimeout(this.timer);
}
if (this.blocker) {
this.debug("found blocker when setting manual power");
return;
}
this.timer = setTimeout(() => {
this.debug("sending power command", mode);
if (mode === undefined) {
this.sendCommand("set_power", ["off", "smooth", 500]);
}
else {
this.sendCommand("set_power", ["on", "sudden", 0, mode]);
this.powerMode = mode;
}
delete this.timer;
}, 500);
}
async sendDebouncedPowerOverride(mode) {
if (this.timer) {
this.debug("aborting prior power command");
clearTimeout(this.timer);
}
delete this.timer;
this.debug("sending override power command", mode);
// eslint-disable-next-line unicorn/prefer-ternary
if (mode === undefined) {
await this.sendCommand("set_power", ["off", "smooth", 500]);
}
else {
this.powerMode = mode;
await this.sendCommand("set_power", ["on", "sudden", 0, mode]);
this.blocker = true;
setTimeout(() => { this.blocker = false; }, 1000);
}
}
async installHandlers() {
this.handleCharacteristic(this.platform.Characteristic.On, async () => {
const attributes = await this.attributes();
return attributes.power;
}, async (value) => {
if (this.config.ignorePower && value) {
this.log(`Ignoring explicit power on`);
}
else {
this.debug(`Manual power setting with powerMode: ${this.powerMode}`, value);
// eslint-disable-next-line unicorn/prefer-ternary
if (value) {
this.sendDebouncedPower(this.powerMode || lightservice_1.POWERMODE_CT);
// await this.sendCommand("set_power", ["on", "sudden", 0, this.powerMode || POWERMODE_CT]);
}
else {
this.sendDebouncedPower();
}
this.setAttributes({ power: value });
}
// this.updateCharacteristic(this.platform.Characteristic.On, value);
});
this.handleCharacteristic(this.platform.Characteristic.Brightness, async () => {
return this.getBrightness(await this.attributes());
}, async (value) => {
if (value > 0) {
let valueToSet = value;
if (this.specs.nightLight) {
if (value < 50) {
if (this.powerMode !== lightservice_1.POWERMODE_MOON) {
await this.sendDebouncedPowerOverride(lightservice_1.POWERMODE_MOON);
this.debug("Moonlight", "on");
}
valueToSet = value * 2;
}
else {
if (this.powerMode !== lightservice_1.POWERMODE_CT) {
await this.sendDebouncedPowerOverride(lightservice_1.POWERMODE_CT);
this.debug("Moonlight", "off");
}
valueToSet = Math.max(1, (value - 50) * 2);
}
}
this.log(`set brightness ${value} (translated to ${valueToSet})`);
await this.sendSuddenCommand("set_bright", valueToSet);
if (value < 50) {
this.setAttributes({ nl_br: valueToSet });
}
else {
this.setAttributes({ bright: valueToSet });
}
// this.updateCharacteristic(this.platform.Characteristic.Brightness, this.getBrightness(valueToSet));
}
else {
this.log(`set brightness to 0, power off`);
await this.sendDebouncedPowerOverride();
}
this.saveDefaultIfNeeded();
});
const characteristic = this.handleCharacteristic(this.platform.Characteristic.ColorTemperature, async () => {
const attributes = await this.attributes();
return (0, lightservice_1.convertColorTemperature)(attributes.ct);
}, async (value) => {
await this.ensurePowerMode(lightservice_1.POWERMODE_CT);
await this.sendSuddenCommand("set_ct_abx", (0, lightservice_1.convertColorTemperature)(value));
this.setAttributes({ ct: (0, lightservice_1.convertColorTemperature)(value) });
/*this.updateCharacteristic(
this.platform.Characteristic.ColorTemperature,
value
);\*/
this.saveDefaultIfNeeded();
});
characteristic.setProps({
...characteristic.props,
maxValue: (0, lightservice_1.convertColorTemperature)(this.specs.colorTemperature.min),
minValue: (0, lightservice_1.convertColorTemperature)(this.specs.colorTemperature.max),
});
}
}
exports.TemperatureLightService = TemperatureLightService;
//# sourceMappingURL=temperaturelightservice.js.map