UNPKG

@homebridge-plugins/homebridge-tado

Version:
74 lines (58 loc) 2.47 kB
import Logger from '../helper/logger.js'; import moment from 'moment'; const timeout = (ms) => new Promise((res) => setTimeout(res, ms)); export default class MotionAccessory { constructor(api, accessory, accessories, tado, deviceHandler, FakeGatoHistoryService) { this.api = api; this.accessory = accessory; this.accessories = accessories; this.FakeGatoHistoryService = FakeGatoHistoryService; this.deviceHandler = deviceHandler; this.tado = tado; this.getService(); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// // Services //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// async getService() { let service = this.accessory.getService(this.api.hap.Service.MotionSensor); let serviceOld = this.accessory.getService(this.api.hap.Service.OccupancySensor); if (serviceOld) { Logger.info('Removing Occupancy service', this.accessory.displayName); this.accessory.removeService(serviceOld); } if (!service) { Logger.info('Adding Motion service', this.accessory.displayName); service = this.accessory.addService( this.api.hap.Service.MotionSensor, this.accessory.displayName, this.accessory.context.config.subtype ); } if (!service.testCharacteristic(this.api.hap.Characteristic.LastActivation)) service.addCharacteristic(this.api.hap.Characteristic.LastActivation); this.historyService = this.FakeGatoHistoryService ? new this.FakeGatoHistoryService('motion', this.accessory, { storage: 'fs', path: this.api.user.storagePath(), disableTimer: true, }) : undefined; await timeout(250); //wait for historyService to load service .getCharacteristic(this.api.hap.Characteristic.MotionDetected) .on( 'change', this.deviceHandler.changedStates.bind(this, this.accessory, this.historyService, this.accessory.displayName) ); if (this.FakeGatoHistoryService && !this.refreshHistoryHandlerRegistered) { this.deviceHandler.refreshHistoryHandlers.push(() => this.refreshHistory(service)); this.refreshHistoryHandlerRegistered = true; } } refreshHistory(service) { let state = service.getCharacteristic(this.api.hap.Characteristic.MotionDetected).value; if (this.historyService) this.historyService.addEntry({ time: moment().unix(), status: state ? 1 : 0, }); } }