UNPKG

@homenet/plugin-zway

Version:

Homenet plugin for Z-Way connected Z-Wave devices

107 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = require("events"); class ZwayMotionSensor extends events_1.EventEmitter { constructor(instanceId, controller, opts) { super(); this.controller = controller; this.opts = opts; this.isTrigger = false; this.isToggle = true; this.isValue = false; this.isTrigger = false; this.isValue = false; this.isToggle = true; controller.onSensorBinaryEvent(opts.deviceId, this.onSensorMotionEvent.bind(this)); } onSensorMotionEvent(e) { if (e.event !== '1') { console.log('Expected motion event to have event name "1"'); return; } this.emit('active', e.data.level); } } exports.ZwayMotionSensor = ZwayMotionSensor; class ZwayValueSensor extends events_1.EventEmitter { constructor(instanceId, controller, opts) { super(); this.controller = controller; this.opts = opts; this.isTrigger = false; this.isToggle = false; this.isValue = true; this.isTrigger = false; this.isValue = true; this.isToggle = false; this.deviceId = opts.deviceId; this.device = this.controller.getSensorDevice(this.deviceId); controller.onSensorMultiEvent(opts.deviceId, this.onSensorValueEvent.bind(this)); setInterval(() => { this.device.SensorMultilevel.refresh(); }, 60000); } get(key) { if (key === 'battery') return this.device.Battery.get('last'); if (key !== this.inputKey) return 0; if (!this.key) { this.key = getKey(this.zwayType, this.device.SensorMultilevel.getItems()); } if (!this.key) return 0; return this.device.SensorMultilevel.get(this.key).val; } set(key, value) { } onSensorValueEvent(e) { if (!this.key) { const device = this.controller.getSensorDevice(this.deviceId); this.key = getKey(this.zwayType, device.SensorMultilevel.getItems()); } if (e.event !== this.key) return; this.emit('value', this.inputKey, e.data.val); this.checkBattery(); } checkBattery() { const lastReading = this.battery; this.battery = this.get('battery'); if (this.battery === lastReading) return; this.emit('value', 'battery', this.battery); } } exports.ZwayValueSensor = ZwayValueSensor; class ZwayTemperatureSensor extends ZwayValueSensor { constructor(instanceId, controller, opts) { super(instanceId, controller, opts); this.zwayType = 'Temperature'; this.inputKey = 'temperature'; } } exports.ZwayTemperatureSensor = ZwayTemperatureSensor; class ZwayHumiditySensor extends ZwayValueSensor { constructor(instanceId, controller, opts) { super(instanceId, controller, opts); this.zwayType = 'Humidity'; this.inputKey = 'humidity'; } } exports.ZwayHumiditySensor = ZwayHumiditySensor; class ZwayLuminescenceSensor extends ZwayValueSensor { constructor(instanceId, controller, opts) { super(instanceId, controller, opts); this.zwayType = 'Luminiscence'; this.inputKey = 'luminescence'; } } exports.ZwayLuminescenceSensor = ZwayLuminescenceSensor; function getKey(type, items) { for (let i = 0; i < items.length; i++) { if (items[i].type === type) return items[i].id; } } //# sourceMappingURL=sensor.js.map