UNPKG

@ronniepettersson/homebridge-dummy

Version:

Create Homebridge accessories to help with automation and control — scheduling, delays, sensors, commands, webhooks, and more

83 lines 3.79 kB
import { strings } from '../i18n/i18n.js'; import { SensorType, SensorCharacteristic, isValidSensorType, printableValues } from '../model/enums.js'; import { Timeout } from '../timeout/timeout.js'; const INFO_MAP = { [SensorType.CarbonDioxideSensor]: { characteristic: SensorCharacteristic.CarbonDioxideDetected, strings: strings.sensor.carbonDioxide }, [SensorType.CarbonMonoxideSensor]: { characteristic: SensorCharacteristic.CarbonMonoxideDetected, strings: strings.sensor.carbonMonoxide }, [SensorType.ContactSensor]: { characteristic: SensorCharacteristic.ContactSensorState, strings: strings.sensor.contact }, [SensorType.LeakSensor]: { characteristic: SensorCharacteristic.LeakDetected, strings: strings.sensor.leak }, [SensorType.MotionSensor]: { characteristic: SensorCharacteristic.MotionDetected, strings: strings.sensor.motion }, [SensorType.OccupancySensor]: { characteristic: SensorCharacteristic.OccupancyDetected, strings: strings.sensor.occupancy }, [SensorType.SmokeSensor]: { characteristic: SensorCharacteristic.SmokeDetected, strings: strings.sensor.smoke }, }; export class SensorAccessory extends Timeout { config; Characteristic; service; _active = 0; static new(Service, Characteristic, accessory, caller, log, disableLogging, sensor) { if (sensor) { if (typeof sensor === 'string') { sensor = { type: sensor, }; } if (!isValidSensorType(sensor.type)) { log.error(strings.sensor.badType, caller, `'${sensor.type}'`, printableValues(SensorType)); return; } return new SensorAccessory(sensor, Service, Characteristic, accessory, caller, log, disableLogging); } SensorAccessory.removeUnwantedServices(Service, accessory); return; } static removeUnwantedServices(Service, accessory, keep) { for (const type of Object.values(SensorType)) { if (type === keep) { continue; } const existingService = accessory.getService(Service[type]); if (existingService) { accessory.removeService(existingService); } } } constructor(config, Service, Characteristic, accessory, caller, log, disableLogging) { super(caller, log, disableLogging); this.config = config; this.Characteristic = Characteristic; this.service = accessory.getService(Service[config.type]) || accessory.addService(Service[config.type]); const characteristicInstance = Characteristic[this.sensorInfo.characteristic]; this.service.getCharacteristic(characteristicInstance) .onGet(this.onGet.bind(this)); SensorAccessory.removeUnwantedServices(Service, accessory, config.type); } async onGet() { return this._active; } get sensorInfo() { return INFO_MAP[this.config.type]; } get timerControlled() { return this.config.timerControlled === true; } get active() { return this._active === 1; } set active(isActive) { this.reset(); if (this.active === isActive) { return; } this._active = isActive ? 1 : 0; const characteristicInstance = this.Characteristic[this.sensorInfo.characteristic]; this.service.updateCharacteristic(characteristicInstance, this._active); this.logIfDesired(isActive ? this.sensorInfo.strings.active : this.sensorInfo.strings.inactive); if (this.timerControlled && this.active) { this.timeout = setTimeout(() => { this.active = false; }, 1000); } } } //# sourceMappingURL=sensor.js.map