@pst-on-npm/homebridge-enocean
Version:
Integrate EnOcean® devices into Homebridge.
83 lines • 5.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MotionSensorAccessory = void 0;
const BatteryServiceHelper_1 = require("../serviceHelper/BatteryServiceHelper");
const HistoryServiceFactory_1 = require("../serviceHelper/HistoryServiceFactory");
const InformationServiceHelper_1 = require("../serviceHelper/InformationServiceHelper");
const EnoAccessory_1 = require("./EnoAccessory");
const StatusActiveWrapper_1 = require("../serviceHelper/StatusActiveWrapper");
/**
* EnoTemperatureSensorAccessory
* An instance of this class is created for each temperature sensor
* Creates a relative humidity service as well depending on EEP
*/
class MotionSensorAccessory extends EnoAccessory_1.EnoAccessory {
_gateway;
_service;
_batteryService;
_lightSensorService;
_statusActiveWrapper;
_currentAmbientLightLevel = 0.0001;
_isMotionDetected;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
historyService = undefined;
constructor(platform, accessory, config) {
super(platform, accessory, config);
this._statusActiveWrapper = new StatusActiveWrapper_1.StatusActiveWrapper(platform.log, accessory.displayName);
this._statusActiveWrapper.statusActiveChanged = (statusActive) => {
this._service.updateCharacteristic(this.hap.Characteristic.StatusActive, statusActive);
if (statusActive === false) {
this._isMotionDetected = 0;
this._service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this._isMotionDetected);
}
};
if (accessory.context.motionDetected === undefined) {
accessory.context.motionDetected = 0;
}
this._isMotionDetected = accessory.context.motionDetected;
// 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 service if it exists, otherwise create a new service
this._service = this.accessory.getService(this.hap.Service.MotionSensor)
|| this.accessory.addService(this.hap.Service.MotionSensor);
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/MotionSensor
// Register handlers for the Characteristics
this._service.getCharacteristic(this.hap.Characteristic.StatusActive)
.onGet(async () => this.getProperty(this._statusActiveWrapper.statusActive));
const ct = this._service.getCharacteristic(this.hap.Characteristic.MotionDetected);
ct.onGet(async () => this.getProperty(this._isMotionDetected));
this._batteryService = new BatteryServiceHelper_1.BatteryServiceHelper(platform, accessory);
// See https://developers.homebridge.io/#/service/LightSensor
this._lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
|| this.accessory.addService(this.hap.Service.LightSensor);
this._lightSensorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
this._lightSensorService.getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
.onGet(async () => this.getProperty(this._currentAmbientLightLevel));
}
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()}`);
this._statusActiveWrapper.update();
if (message.values.isMotionDetected !== undefined) {
this._isMotionDetected = message.values.isMotionDetected ? 1 : 0;
this._service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this._isMotionDetected);
this.historyService?.addEntry({ time: Math.round(new Date().valueOf() / 1000), motion: this._isMotionDetected });
}
if (this._lightSensorService !== undefined && message.values.ambientLightLevel !== undefined) {
this._currentAmbientLightLevel = Math.max(0.0001, message.values.ambientLightLevel);
this._lightSensorService.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this._currentAmbientLightLevel);
this.historyService?.addEntry({ time: Math.round(new Date().valueOf() / 1000), lux: this._currentAmbientLightLevel });
}
this._batteryService?.updateCharacteristics(message);
}
}
exports.MotionSensorAccessory = MotionSensorAccessory;
//# sourceMappingURL=MotionSensorAccessory.js.map