UNPKG

homebridge-deconz-converter

Version:

Homebridge plugin for converting Deconz roller shutters interpreted as light into Homekit Window Covering type.

60 lines 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContactSensorAccessory = void 0; const deconzEvent_1 = require("../typing/deconzEvent"); class ContactSensorAccessory { constructor(platform, accessory) { this.platform = platform; this.accessory = accessory; this.service = this.accessory.getService(this.platform.Service.ContactSensor) || this.accessory.addService(this.platform.Service.ContactSensor); this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.displayName); this.service .getCharacteristic(this.platform.Characteristic.ContactSensorState) .on("get" /* GET */, this.getContactSensorState.bind(this)); this.platform.wsClient.on('message', this.handleContactSensorStateChange.bind(this)); } async setAccessoryInformations() { const response = await this.platform.client.getSensorState(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 getContactSensorState(callback) { this.platform.log.debug(`Get state for ${this.accessory.context.device.displayName}`); try { const response = await this.platform.client.getSensorState(this.accessory.context.device.uniqueId); const device = response.data; const state = device.state.open ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; callback(null, state); } catch (err) { callback(err); } } handleContactSensorStateChange(e) { var _a; const event = JSON.parse(e); if (event.e === deconzEvent_1.DeconzEventType.Changed && event.r === deconzEvent_1.DeconzEventResourceType.Sensors && event.uniqueid === this.accessory.context.device.uniqueId) { const state = ((_a = event.state) === null || _a === void 0 ? void 0 : _a.open) ? this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED; this.service.setCharacteristic(this.platform.Characteristic.ContactSensorState, state); this.platform.log.debug(`============= Characteristic set ${this.accessory.context.device.displayName} ContactSensorState to ${state}`); } } } exports.ContactSensorAccessory = ContactSensorAccessory; //# sourceMappingURL=contactSensor.js.map