homebridge-deconz-converter
Version:
Homebridge plugin for converting Deconz roller shutters interpreted as light into Homekit Window Covering type.
92 lines • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RollerShutterAccessory = void 0;
const deconzEvent_1 = require("../typing/deconzEvent");
const briPercentsConverter_1 = require("../utils/briPercentsConverter");
class RollerShutterAccessory {
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
this.service = this.accessory.getService(this.platform.Service.WindowCovering)
|| this.accessory.addService(this.platform.Service.WindowCovering);
this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.displayName);
this.service.getCharacteristic(this.platform.Characteristic.CurrentPosition)
.on("get" /* GET */, this.getCurrentPosition.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.PositionState)
.on("get" /* GET */, this.getPositionState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetPosition)
.on("get" /* GET */, this.getTargetPosition.bind(this))
.on("set" /* SET */, this.setTargetPosition.bind(this));
this.platform.wsClient.on('message', this.handleMessage.bind(this));
}
async setAccessoryInformations() {
const response = await this.platform.client.getLightByUniqueId(this.accessory.context.device.uniqueId);
const device = response.data;
const serialNumber = device.uniqueid.slice(0, device.uniqueid.length - 3).replaceAll(':', '').toUpperCase();
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, device.manufacturername)
.setCharacteristic(this.platform.Characteristic.Model, device.modelid)
.setCharacteristic(this.platform.Characteristic.SerialNumber, serialNumber);
}
async getCurrentPosition(callback) {
try {
const response = await this.platform.client.getLightByUniqueId(this.accessory.context.device.uniqueId);
const device = response.data;
const brightness = device.state.bri;
const percentage = (0, briPercentsConverter_1.briToPercents)(brightness);
callback(null, percentage);
}
catch (err) {
callback(err);
}
}
async getPositionState(callback) {
this.platform.log.debug(`Get PositionState for ${this.accessory.context.device.displayName}`);
try {
const nextValue = this.platform.Characteristic.PositionState.STOPPED;
callback(null, nextValue);
}
catch (err) {
callback(err);
}
}
async getTargetPosition(callback) {
this.platform.log.debug(`Get TargetPosition for ${this.accessory.context.device.displayName}`);
try {
const response = await this.platform.client.getLightByUniqueId(this.accessory.context.device.uniqueId);
const device = response.data;
const brightness = device.state.bri;
const percentage = (0, briPercentsConverter_1.briToPercents)(brightness);
callback(null, percentage);
}
catch (err) {
callback(err);
}
}
async setTargetPosition(value, callback) {
this.platform.log.info(`Set ${this.accessory.context.device.displayName} target position to ${value}%`);
try {
const brightness = (0, briPercentsConverter_1.percentsToBri)(value);
await this.platform.client.updateLightBrightness(brightness, this.accessory.context.device.uniqueId);
callback();
}
catch (err) {
callback(err);
}
}
handleMessage(e) {
var _a;
const event = JSON.parse(e);
if (event.e === deconzEvent_1.DeconzEventType.Changed &&
event.r === deconzEvent_1.DeconzEventResourceType.Lights &&
event.uniqueid === this.accessory.context.device.uniqueId &&
((_a = event.state) === null || _a === void 0 ? void 0 : _a.bri) !== undefined) {
const percentage = (0, briPercentsConverter_1.briToPercents)(event.state.bri);
this.service.setCharacteristic(this.platform.Characteristic.CurrentPosition, percentage);
this.service.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
this.platform.log.debug(`============= Characteristic set ${this.accessory.context.device.displayName} CurrentPosition to ${percentage} %`);
}
}
}
exports.RollerShutterAccessory = RollerShutterAccessory;
//# sourceMappingURL=rollerShutter.js.map