homebridge-hilo
Version:
Plugin Homebridge (non officiel) pour la passerelle et les appareils Hilo de Hydro-Québec | Unofficial Homebridge plugin for Hydro-Québec Hilo bridge and devices
74 lines • 3.67 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Light = void 0;
const axios_1 = __importDefault(require("axios"));
const HiloDevice_1 = require("./HiloDevice");
const hiloApi_1 = require("../hiloApi");
class Light extends HiloDevice_1.HiloDevice {
constructor(accessory, api, { canDim = true } = { canDim: true }) {
super(accessory, api);
this.service =
accessory.getService(this.api.hap.Service.Lightbulb) ||
accessory.addService(this.api.hap.Service.Lightbulb);
this.service.setCharacteristic(this.api.hap.Characteristic.Name, this.accessory.context.device.name);
this.service
.getCharacteristic(this.api.hap.Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
if (canDim) {
this.service
.getCharacteristic(this.api.hap.Characteristic.Brightness)
.onSet(this.setBrightness.bind(this))
.onGet(this.getBrightness.bind(this));
}
}
updateDevice(device) {
var _a, _b, _c, _d;
super.updateDevice(device);
if ("state" in device) {
(_b = (_a = this.service) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.On)) === null || _b === void 0 ? void 0 : _b.updateValue(device.state === "ON");
}
if ("level" in device &&
device.level !== undefined &&
device.level !== null) {
(_d = (_c = this.service) === null || _c === void 0 ? void 0 : _c.getCharacteristic(this.api.hap.Characteristic.Brightness)) === null || _d === void 0 ? void 0 : _d.updateValue(device.level);
}
}
async setOn(value) {
var _a;
const on = value;
this.logger.debug(`Setting ${this.accessory.context.device.name} ${on ? "on" : "off"}`);
try {
await hiloApi_1.hiloApi.put(`/Automation/v1/api/Locations/${this.accessory.context.device.locationId}/Devices/${this.accessory.context.device.id}/Attributes`, { OnOff: on });
}
catch (error) {
this.logger.error(`Failed to set ${this.accessory.context.device.name} ${on ? "on" : "off"}`, axios_1.default.isAxiosError(error) ? (_a = error.response) === null || _a === void 0 ? void 0 : _a.data : error);
}
}
async getOn() {
this.logger.debug(`Getting ${this.accessory.context.device.name} onOff status`);
return this.device.state === "ON";
}
async setBrightness(value) {
var _a;
const brightness = value;
this.logger.debug(`Setting ${this.accessory.context.device.name} brightness to ${brightness}`);
try {
await hiloApi_1.hiloApi.put(`/Automation/v1/api/Locations/${this.accessory.context.device.locationId}/Devices/${this.accessory.context.device.id}/Attributes`, { Intensity: brightness / 100 });
}
catch (error) {
this.logger.error(`Failed to set ${this.accessory.context.device.name} brightness to ${brightness}`, axios_1.default.isAxiosError(error) ? (_a = error.response) === null || _a === void 0 ? void 0 : _a.data : error);
}
}
async getBrightness() {
var _a;
const brightness = (_a = this.device.level) !== null && _a !== void 0 ? _a : 100;
this.logger.debug(`Getting ${this.accessory.context.device.name} brightness`);
return brightness;
}
}
exports.Light = Light;
//# sourceMappingURL=Light.js.map