UNPKG

homebridge-wemo

Version:

Homebridge plugin to integrate Wemo devices into HomeKit.

160 lines (134 loc) 5.31 kB
import { hasProperty, parseError } from '../../utils/functions.js' import platformLang from '../../utils/lang-en.js' export default class { constructor(platform, accessory) { // Set up variables from the platform this.hapChar = platform.api.hap.Characteristic this.hapErr = platform.api.hap.HapStatusError this.hapServ = platform.api.hap.Service this.platform = platform // Set up variables from the accessory this.accessory = accessory // If the accessory has an outlet service then remove it if (this.accessory.getService(this.hapServ.Outlet)) { this.accessory.removeService(this.accessory.getService(this.hapServ.Outlet)) } // If the accessory has a switch service then remove it if (this.accessory.getService(this.hapServ.Switch)) { this.accessory.removeService(this.accessory.getService(this.hapServ.Switch)) } // Add the air purifier service if it doesn't already exist this.service = this.accessory.getService(this.hapServ.AirPurifier) || this.accessory.addService(this.hapServ.AirPurifier) // Add the set handler to the purifier active characteristic this.service .getCharacteristic(this.hapChar.Active) .removeOnSet() .onSet(async value => this.internalStateUpdate(value)) // Add options to the purifier target state characteristic this.service.getCharacteristic(this.hapChar.TargetAirPurifierState).setProps({ minValue: 1, maxValue: 1, validValues: [1], }) this.service.updateCharacteristic(this.hapChar.TargetAirPurifierState, 1) // Output the customised options to the log const opts = JSON.stringify({ showAs: 'purifier', }) platform.log('[%s] %s %s.', accessory.displayName, platformLang.devInitOpts, opts) // Request a device update immediately this.requestDeviceUpdate() // Start a polling interval if the user has disabled upnp if (this.accessory.context.connection === 'http') { this.pollingInterval = setInterval( () => this.requestDeviceUpdate(), platform.config.pollingInterval * 1000, ) } } receiveDeviceUpdate(attribute) { // Log the receiving update if debug is enabled this.accessory.logDebug(`${platformLang.recUpd} [${attribute.name}: ${JSON.stringify(attribute.value)}]`) // Send a HomeKit needed 1/0 argument // attribute.value is 0 if and only if the switch is off this.externalStateUpdate(attribute.value) } async sendDeviceUpdate(value) { // Log the sending update if debug is enabled this.accessory.logDebug(`${platformLang.senUpd} ${JSON.stringify(value)}`) // Send the update await this.platform.httpClient.sendDeviceUpdate( this.accessory, 'urn:Belkin:service:basicevent:1', 'SetBinaryState', value, ) } async requestDeviceUpdate() { try { // Request the update const data = await this.platform.httpClient.sendDeviceUpdate( this.accessory, 'urn:Belkin:service:basicevent:1', 'GetBinaryState', ) // Check for existence since BinaryState can be int 0 if (hasProperty(data, 'BinaryState')) { // Send the data to the receiver function this.receiveDeviceUpdate({ name: 'BinaryState', value: Number.parseInt(data.BinaryState, 10), }) } } catch (err) { const eText = parseError(err, [ platformLang.timeout, platformLang.timeoutUnreach, platformLang.noService, ]) this.accessory.logDebugWarn(`${platformLang.rduErr} ${eText}`) } } async internalStateUpdate(value) { try { // Send the update await this.sendDeviceUpdate({ BinaryState: value, }) // Update the HomeKit characteristic this.service.updateCharacteristic(this.hapChar.CurrentAirPurifierState, value === 1 ? 2 : 0) // Update the cache value this.cacheState = value // Log the change if appropriate this.accessory.log(`${platformLang.curState} [${value === 1 ? platformLang.purifyYes : platformLang.purifyNo}]`) } catch (err) { // Catch any errors const eText = parseError(err, [platformLang.timeout, platformLang.timeoutUnreach]) this.accessory.logWarn(`${platformLang.cantCtl} ${eText}`) // Throw a 'no response' error and set a timeout to revert this after 2 seconds setTimeout(() => { this.service.updateCharacteristic(this.hapChar.Active, this.cacheState) }, 2000) throw new this.hapErr(-70402) } } externalStateUpdate(value) { try { // Check to see if the cache value is different if (value === this.cacheState) { return } // Update the HomeKit characteristic this.service.updateCharacteristic(this.hapChar.Active, value) this.service.updateCharacteristic(this.hapChar.CurrentAirPurifierState, value === 1 ? 2 : 0) // Update the cache value this.cacheState = value // Log the change if appropriate this.accessory.log(`${platformLang.curState} [${value === 1 ? platformLang.purifyYes : platformLang.purifyNo}]`) } catch (err) { // Catch any errors this.accessory.logWarn(`${platformLang.cantUpd} ${parseError(err)}`) } } }