@pst-on-npm/homebridge-enocean
Version:
Integrate EnOcean® devices into Homebridge.
103 lines • 5.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContactSensorAccessory = void 0;
const BatteryServiceHelper_1 = require("../serviceHelper/BatteryServiceHelper");
const InformationServiceHelper_1 = require("../serviceHelper/InformationServiceHelper");
const HistoryServiceFactory_1 = require("../serviceHelper/HistoryServiceFactory");
const EnoAccessory_1 = require("./EnoAccessory");
const StatusActiveWrapper_1 = require("../serviceHelper/StatusActiveWrapper");
/**
*/
class ContactSensorAccessory extends EnoAccessory_1.EnoAccessory {
_gateway;
_statusActiveWrapper;
_service;
_batteryService;
_state;
_statusTampered;
_statusTamperedTimeout;
_statusTamperedTimeoutId;
_historyService = undefined;
constructor(platform, accessory, config) {
super(platform, accessory, config);
if (accessory.context.contactSensorState === undefined) {
accessory.context.contactSensorState
= this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
}
this._state = accessory.context.contactSensorState;
if (/^A5-14/.test(config.eep)) {
this._statusActiveWrapper = new StatusActiveWrapper_1.StatusActiveWrapper(platform.log, accessory.displayName);
this._statusActiveWrapper.statusActiveChanged = (statusActive) => {
this._service.updateCharacteristic(this.hap.Characteristic.StatusActive, statusActive);
};
}
// Get the historyService if available in the platform
this._historyService = HistoryServiceFactory_1.HistoryServiceFactory
.getOptionalHistoryService(platform, accessory, config);
// Set accessory information service
InformationServiceHelper_1.InformationServiceHelper.setupService(platform, accessory, config);
// Get the TemperatureSensor service if it exists, otherwise create a new TemperatureSensor service
// you can create multiple services for each accessory
this._service = this.accessory.getService(this.hap.Service.ContactSensor)
|| this.accessory.addService(this.hap.Service.ContactSensor);
this._service.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
// Each service must implement at-minimum the "required characteristics" for the given service type
// see https://developers.homebridge.io/#/service/ContactSensor
this._service.getCharacteristic(this.hap.Characteristic.ContactSensorState)
.onGet(async () => this.getProperty(this._state));
if (this._statusActiveWrapper !== undefined) {
this._service.getCharacteristic(this.hap.Characteristic.StatusActive)
.onGet(async () => this.getProperty(this._statusActiveWrapper.statusActive));
}
if (config.time && config.time > 10) {
this._statusTamperedTimeout = config.time;
this._statusTampered = this.hap.Characteristic.StatusTampered.NOT_TAMPERED;
this._service.getCharacteristic(this.hap.Characteristic.StatusTampered)
.onGet(async () => this.getProperty(this._statusTampered));
}
if (/^A5-14/.test(config.eep)) {
const isChargeable = false;
this._batteryService = new BatteryServiceHelper_1.BatteryServiceHelper(platform, accessory, isChargeable);
}
}
async setGateway(gateway) {
this._gateway = gateway;
// Register message receiver in the gateway
await this._gateway.registerEnoAccessory(this.config, this.EnoGateway_eepMessageReceived.bind(this));
}
EnoGateway_eepMessageReceived(message) {
this.platform.log.debug(`${this.accessory.displayName}: ${message.toString()}`);
const previousState = this._state;
const CONTACT_DETECTED = this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
const CONTACT_NOT_DETECTED = this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
let state = undefined;
state = (message.values.isContactClosed) ? CONTACT_DETECTED : CONTACT_NOT_DETECTED;
this._batteryService?.updateCharacteristics(message);
if (state !== undefined) {
this._state = state;
this.accessory.context.contactSensorState = this._state;
this._service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this._state);
this._historyService?.addEntry({ time: Math.round(new Date().valueOf() / 1000), contact: this._state });
}
this._statusActiveWrapper?.update();
if (this._statusTamperedTimeout) {
if (previousState !== this._state || !this._statusTamperedTimeoutId) {
if (this._state === CONTACT_NOT_DETECTED) {
this._statusTamperedTimeoutId = setTimeout(() => {
this._statusTampered = this.hap.Characteristic.StatusTampered.TAMPERED;
this._service.updateCharacteristic(this.hap.Characteristic.StatusTampered, this._statusTampered);
}, this._statusTamperedTimeout * 1000);
}
else {
if (this._statusTamperedTimeoutId) {
clearTimeout(this._statusTamperedTimeoutId);
}
this._statusTampered = this.hap.Characteristic.StatusTampered.NOT_TAMPERED;
this._service.updateCharacteristic(this.hap.Characteristic.StatusTampered, this._statusTampered);
}
}
}
}
}
exports.ContactSensorAccessory = ContactSensorAccessory;
//# sourceMappingURL=ContactSensorAccessory.js.map