UNPKG

@homebridge-plugins/homebridge-ewelink

Version:

Homebridge plugin to integrate eWeLink devices into HomeKit.

124 lines (112 loc) 4.63 kB
import platformConsts from '../../utils/constants.js' import { hasProperty } from '../../utils/functions.js' export default class { constructor(platform, accessory) { // Set up variables from the platform this.eveChar = platform.eveChar this.hapChar = platform.api.hap.Characteristic this.hapServ = platform.api.hap.Service this.lang = platform.lang this.log = platform.log this.platform = platform // Set up variables from the accessory this.name = accessory.displayName this.accessory = accessory // Set up custom variables for this device type const deviceConf = platform.deviceConf[accessory.context.eweDeviceId] || {} this.lowBattThreshold = deviceConf.lowBattThreshold ? Math.min(deviceConf.lowBattThreshold, 100) : platformConsts.defaultValues.lowBattThreshold // Set the correct logging variables for this accessory switch (deviceConf.overrideLogging) { case 'standard': this.enableLogging = true this.enableDebugLogging = false break case 'debug': this.enableLogging = true this.enableDebugLogging = true break case 'disable': this.enableLogging = false this.enableDebugLogging = false break default: this.enableLogging = !platform.config.disableDeviceLogging this.enableDebugLogging = platform.config.debug break } // If the accessory has a contact sensor service then remove it if (this.accessory.getService(this.hapServ.ContactSensor)) { this.accessory.removeService(this.accessory.getService(this.hapServ.ContactSensor)) } // Add the leak sensor service if it doesn't already exist this.service = this.accessory.getService(this.hapServ.LeakSensor) if (!this.service) { this.service = this.accessory.addService(this.hapServ.LeakSensor) this.service.addCharacteristic(this.eveChar.LastActivation) } // Add the battery service if it doesn't already exist this.batteryService = this.accessory.getService(this.hapServ.Battery) || this.accessory.addService(this.hapServ.Battery) // Pass the accessory to Fakegato to set up with Eve this.accessory.eveService = new platform.eveService('motion', this.accessory, { log: () => {}, }) // Output the customised options to the log const normalLogging = this.enableLogging ? 'standard' : 'disable' const opts = JSON.stringify({ logging: this.enableDebugLogging ? 'debug' : normalLogging, lowBattThreshold: this.lowBattThreshold, showAs: 'sensor_leak', }) this.log('[%s] %s %s.', this.name, this.lang.devInitOpts, opts) } async externalUpdate(params) { try { if (hasProperty(params, 'battery') && this.cacheBatt !== params.battery) { this.cacheBatt = params.battery if (this.accessory.context.eweUIID === 154) { // No scaling needed for this model as provided as a % this.cacheBattScaled = this.cacheBatt } else { // Scaling needed for UIID 102 as provided as a voltage rather than a % this.cacheBattScaled = Math.min(Math.max(this.cacheBatt, 2), 3) this.cacheBattScaled = Math.round((this.cacheBattScaled - 2) * 100) } this.batteryService.updateCharacteristic(this.hapChar.BatteryLevel, this.cacheBattScaled) this.batteryService.updateCharacteristic( this.hapChar.StatusLowBattery, this.cacheBattScaled < this.lowBattThreshold ? 1 : 0, ) if (params.updateSource && this.enableLogging) { this.log('[%s] %s [%s%].', this.name, this.lang.curBatt, this.cacheBattScaled) } } if (!params.switch || params.switch === this.cacheState) { return } this.cacheState = params.switch const newState = params.switch === 'on' ? 0 : 1 this.service.updateCharacteristic(this.hapChar.LeakDetected, newState) this.accessory.eveService.addEntry({ status: newState }) if (newState === 1) { const initialTime = this.accessory.eveService.getInitialTime() this.service.updateCharacteristic( this.eveChar.LastActivation, Math.round(new Date().valueOf() / 1000) - initialTime, ) } if (params.updateSource && this.enableLogging) { this.log( '[%s] %s [%s].', this.name, this.lang.curState, newState === 1 ? this.lang.leakYes : this.lang.leakNo, ) } } catch (err) { this.platform.deviceUpdateError(this.accessory, err, false) } } }