UNPKG

@homebridge-plugins/homebridge-ewelink

Version:

Homebridge plugin to integrate eWeLink devices into HomeKit.

183 lines (165 loc) 6.46 kB
import platformConsts from '../utils/constants.js' import { hasProperty, parseError } from '../utils/functions.js' export default class { constructor(platform, accessory) { // Set up variables from the platform 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 // Initially set the online flag as true (to be then updated as false if necessary) this.isOnline = true // Set up custom variables for this device type const deviceConf = platform.deviceConf[accessory.context.eweDeviceId] || {} this.tempOffset = deviceConf.offset || platformConsts.defaultValues.offset this.tempOffsetFactor = deviceConf.offsetFactor this.humiOffset = deviceConf && deviceConf.humidityOffset ? Number.parseInt(deviceConf.humidityOffset, 10) : platformConsts.defaultValues.humidityOffset this.humiOffsetFactor = deviceConf.humidityOffsetFactor // 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 } // Add the temperature sensor service if it doesn't already exist this.tempService = this.accessory.getService(this.hapServ.TemperatureSensor) || this.accessory.addService(this.hapServ.TemperatureSensor) // Set custom properties of the current temperature characteristic this.tempService.getCharacteristic(this.hapChar.CurrentTemperature).setProps({ minStep: 0.1, }) this.cacheTemp = this.tempService.getCharacteristic(this.hapChar.CurrentTemperature).value this.updateCache() // Add the humidity sensor service if it doesn't already exist this.humiService = this.accessory.getService(this.hapServ.HumiditySensor) || this.accessory.addService(this.hapServ.HumiditySensor) // Add the get handlers only if the user hasn't disabled the disableNoResponse setting if (!platform.config.disableNoResponse) { this.tempService .getCharacteristic(this.hapChar.CurrentTemperature) .setProps({ minStep: 0.1, }) .onGet(() => { if (!this.isOnline) { throw new this.hapErr(-70402) } return this.tempService.getCharacteristic(this.hapChar.CurrentTemperature).value }) this.humiService.getCharacteristic(this.hapChar.CurrentRelativeHumidity).onGet(() => { if (!this.isOnline) { throw new this.hapErr(-70402) } return this.humiService.getCharacteristic(this.hapChar.CurrentRelativeHumidity).value }) } // Pass the accessory to Fakegato to set up with Eve this.accessory.eveService = new platform.eveService('custom', this.accessory, { log: () => {}, }) // Output the customised options to the log const normalLogging = this.enableLogging ? 'standard' : 'disable' const opts = JSON.stringify({ humidityOffset: this.humiOffset, humidityOffsetFactor: this.humiOffsetFactor, logging: this.enableDebugLogging ? 'debug' : normalLogging, offset: this.tempOffset, offsetFactor: this.tempOffsetFactor, }) this.log('[%s] %s %s.', this.name, this.lang.devInitOpts, opts) } async externalUpdate(params) { try { if (hasProperty(params, 'temperature')) { let newTemp = Number(params.temperature) if (this.tempOffsetFactor) { newTemp *= this.tempOffset } else { newTemp += this.tempOffset } if (newTemp !== this.cacheTemp) { this.cacheTemp = newTemp this.tempService.updateCharacteristic(this.hapChar.CurrentTemperature, this.cacheTemp) this.accessory.eveService.addEntry({ temp: this.cacheTemp }) if (params.updateSource && this.enableLogging) { this.log('[%s] %s [%s°C].', this.name, this.lang.curTemp, this.cacheTemp) } // Update the cache file with the new temperature this.updateCache() } } if (hasProperty(params, 'humidity')) { let newHumi = Number.parseInt(params.humidity, 10) if (this.humiOffsetFactor) { newHumi *= this.humiOffset } else { newHumi += this.humiOffset } newHumi = Math.max(Math.min(Number.parseInt(newHumi, 10), 100), 0) if (newHumi !== this.cacheHumi) { this.cacheHumi = newHumi this.humiService.updateCharacteristic( this.hapChar.CurrentRelativeHumidity, this.cacheHumi, ) this.accessory.eveService.addEntry({ humidity: this.cacheHumi }) if (params.updateSource && this.enableLogging) { this.log('[%s] %s [%s%].', this.name, this.lang.curHumi, this.cacheHumi) } } } } catch (err) { this.platform.deviceUpdateError(this.accessory, err, false) } } async updateCache() { // Don't continue if the storage client hasn't initialised properly if (!this.platform.storageClientData) { return } // Attempt to save the new temperature to the cache try { await this.platform.storageData.setItem( `${this.accessory.context.eweDeviceId}_temp`, this.cacheTemp, ) } catch (err) { if (this.enableLogging) { this.log.warn('[%s] %s %s.', this.name, this.lang.storageWriteErr, parseError(err)) } } } currentState() { const toReturn = {} toReturn.services = ['temperature', 'humidity'] toReturn.temperature = { current: this.tempService.getCharacteristic(this.hapChar.CurrentTemperature).value, } toReturn.humidity = { current: this.humiService.getCharacteristic(this.hapChar.CurrentRelativeHumidity).value, } return toReturn } markStatus(isOnline) { this.isOnline = isOnline } }