UNPKG

homebridge-nature-remo-controller

Version:
174 lines (133 loc) 4.03 kB
let Service, Characteristic module.exports = homebridge => { const hap = homebridge.hap Service = hap.Service Characteristic = hap.Characteristic homebridge.registerAccessory( 'homebridge-nature-remo-controller', 'HomebridgeNatureRemoController', NatureRemoController, ) } class NatureRemoController { constructor(log, config, api) { this.log = log this.state = false this.request = require('request-promise-native') this.configureBasedOn(config) } configureBasedOn(config) { this.id = config.id this.type = config.type this.header = this.makeHeaderFrom(config.accessToken) this.information = this.getInformation(config) } makeHeaderFrom(accessToken) { const header = { Authorization: `Bearer ${accessToken}`, } return header } getInformation(config) { const informationService = this.getInformationService() const device = this.setDeviceOf(config) const information = [informationService, device] return information } getInformationService() { const informationService = new Service.AccessoryInformation() .setCharacteristic(Characteristic.Manufacturer, 'Nature, Inc.') .setCharacteristic(Characteristic.Model, 'NatureRemo') .setCharacteristic(Characteristic.SerialNumber, 'nature-remo') return informationService } setDeviceOf(config) { const getHandler = this.getOnCharacteristicHandler.bind(this) const setHandler = this.setOnCharacteristicHandler.bind(this) const device = this.setConfigBasedOn(config) device.getCharacteristic(Characteristic.On) .on('get', getHandler) .on('set', setHandler) return device } setConfigBasedOn(config) { const name = config.name const baseURL = 'https://api.nature.global/1' const appliancesURL = `${baseURL}/appliances` const applianceidURL = `${appliancesURL}/${this.id}` // https://swagger.nature.global switch (this.type) { case 'light': this.options = this.makeOptions(appliancesURL, 'GET', {}) this.lightURL = `${applianceidURL}/light` this.getOptions = this.getLightOptions return new Service.Lightbulb(name) case 'tv': const url = `${applianceidURL}/tv` var form = { button: 'power' } if (config.tvButton) { form['button'] = config.tvButton } this.tvOptions = this.makePostOptions(url, form) this.getOptions = this.getTvOptions return new Service.Switch(name) case 'signal': default: const signalURL = `${baseURL}/signals` this.onURL = `${signalURL}/${config.onID}/send` this.offURL = `${signalURL}/${config.offID}/send` this.getOptions = this.getSignalOptions return new Service.Switch(name) } } makePostOptions(url, form) { return this.makeOptions(url, 'POST', form) } makeOptions(url, method, form) { const options = { url: url, headers: this.header, method: method, form: form, } return options } getLightOptions(value) { const form = { button: value ? 'on' : 'off', } const options = this.makePostOptions(this.lightURL, form) return options } getTvOptions(value) { return this.tvOptions } getSignalOptions(value) { const url = value ? this.onURL : this.offURL const options = this.makePostOptions(url, null) return options } getServices() { return this.information } async getOnCharacteristicHandler(callback) { if (this.type === 'light') { this.state = await this.getLightState(this.id, this.options) } callback(null, this.state) } async getLightState(id, options) { const responses = await this.request(options) const json = JSON.parse(responses) const devices = json.filter( response => response.id === id ) const device = devices[0] const light = device.light const lightState = light.state const state = lightState.power === 'on' return state } async setOnCharacteristicHandler(value, callback) { const options = this.getOptions(value) const responses = await this.request(options) callback(null) this.state = !this.state } }