diff --git a/README.md b/README.md index bc9ebf8..6372d1e 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Example: "name": "Kitchen", "host": "192.168.1.21", "type": "Plug", - "updateInterval": 30, + "updateInterval": 10, "timeout": 2 } ] @@ -105,7 +105,7 @@ Example response from this endpoint: "rssi":-61, "region":"Europe/Athens", "time_diff":120, -"lang":"en_US"}} +"lang":"en_US"} ``` ## Information diff --git a/config.schema.json b/config.schema.json index dae084c..608a5e3 100644 --- a/config.schema.json +++ b/config.schema.json @@ -80,7 +80,7 @@ "updateInterval": { "title": "Interval in which the state of the Accessory should be updated (in seconds)", "type": "number", - "default": "30", + "default": "10", "required": false }, "timeout": { diff --git a/package.json b/package.json index 1a5231d..93ecdeb 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "displayName": "Tapo P100 Plugin", "name": "homebridge-tapo", - "version": "1.5.1-beta1", + "version": "1.5.1", "description": "Homebridge Plugin for TP-Link Tapo P100 Plugs", "license": "Apache-2.0", "repository": { "type": "git", - "url": "https://github.com/apatsufas/homebridge-tapo-p100" + "url": "git+https://github.com/apatsufas/homebridge-tapo-p100.git" }, "bugs": { "url": "https://github.com/apatsufas/homebridge-tapo-p100/issues" diff --git a/src/platformL510EAccessory.ts b/src/platformL510EAccessory.ts index cf960f5..b88ae89 100644 --- a/src/platformL510EAccessory.ts +++ b/src/platformL510EAccessory.ts @@ -34,7 +34,7 @@ export class L510EAccessory extends TPLinkPlatformAccessory { } protected init(platform: TapoPlatform, updateInterval?: number){ - this.tpLinkAccessory.getDeviceInfo().then((sysInfo) => { + this.tpLinkAccessory.getDeviceInfo(true).then((sysInfo) => { // set accessory information this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'TP-Link') @@ -54,7 +54,7 @@ export class L510EAccessory extends TPLinkPlatformAccessory { .on('set', this.setBrightness.bind(this)) // SET - bind to the `setBrightness` method below .on('get', this.getBrightness.bind(this)); // GET - bind to the `getBrightness` method below - const interval = updateInterval ? updateInterval*1000 : 30000; + const interval = updateInterval ? updateInterval*1000 : 10000; setTimeout(()=>{ this.updateState(interval); }, interval); @@ -116,7 +116,7 @@ export class L510EAccessory extends TPLinkPlatformAccessory { } protected updateState(interval:number){ - this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.tpLinkAccessory.getDeviceInfo(true).then((response) => { if(response){ const isOn = response.device_on; const brightness = response.brightness; diff --git a/src/platformL520EAccessory.ts b/src/platformL520EAccessory.ts index 41f3c4d..22a914b 100644 --- a/src/platformL520EAccessory.ts +++ b/src/platformL520EAccessory.ts @@ -34,7 +34,7 @@ export class L520EAccessory extends TPLinkPlatformAccessory { } protected init(platform: TapoPlatform, updateInterval?: number){ - this.tpLinkAccessory.getDeviceInfo().then((sysInfo) => { + this.tpLinkAccessory.getDeviceInfo(true).then((sysInfo) => { // set accessory information this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'TP-Link') @@ -65,7 +65,7 @@ export class L520EAccessory extends TPLinkPlatformAccessory { }); // GET - bind to the `getColorTemp` method below - const interval = updateInterval ? updateInterval*1000 : 30000; + const interval = updateInterval ? updateInterval*1000 : 10000; setTimeout(()=>{ this.updateState(interval); }, interval); @@ -127,7 +127,7 @@ export class L520EAccessory extends TPLinkPlatformAccessory { } protected updateState(interval:number){ - this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.tpLinkAccessory.getDeviceInfo(true).then((response) => { if(response){ const isOn = response.device_on; const brightness = response.brightness; diff --git a/src/platformL530Accessory.ts b/src/platformL530Accessory.ts index 6e87955..6c693ab 100644 --- a/src/platformL530Accessory.ts +++ b/src/platformL530Accessory.ts @@ -43,7 +43,9 @@ export class L530Accessory extends TPLinkPlatformAccessory { } protected init(platform: TapoPlatform, updateInterval?: number){ - this.tpLinkAccessory.getDeviceInfo().then((sysInfo) => { + this.log.debug('init called'); + + this.tpLinkAccessory.getDeviceInfo(true).then((sysInfo) => { this.log.debug('SysInfo: ', sysInfo); // set accessory information @@ -84,18 +86,10 @@ export class L530Accessory extends TPLinkPlatformAccessory { this.service.getCharacteristic(this.platform.Characteristic.Saturation) .on('set', this.setSaturation.bind(this)) // SET - bind to the `setSaturation` method below .on('get', this.getSaturation.bind(this)); // GET - bind to the `getSaturation` method below - - this.service.getCharacteristic(this.platform.customCharacteristics.CurrentConsumptionCharacteristic) - .on('get', this.getCurrentConsumption.bind(this)); - - this.service.getCharacteristic(this.platform.customCharacteristics.TotalConsumptionCharacteristic) - .on('get', this.getTotalConsumption.bind(this)); - - this.service.getCharacteristic(this.platform.customCharacteristics.ResetConsumptionCharacteristic) - .on('set', this.resetConsumption.bind(this)); // Setup the adaptive lighting controller if available if (this.platform.api.versionGreaterOrEqual && this.platform.api.versionGreaterOrEqual('1.3.0-beta.23')) { + this.log.debug('Enabling Adaptvie Lightning'); this.adaptiveLightingController = new platform.api.hap.AdaptiveLightingController( this.service, ); @@ -104,7 +98,7 @@ export class L530Accessory extends TPLinkPlatformAccessory { this.updateConsumption(); - const interval = updateInterval ? updateInterval*1000 : 30000; + const interval = updateInterval ? updateInterval*1000 : 10000; setTimeout(()=>{ this.updateState(interval); }, interval); @@ -145,6 +139,7 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ getBrightness(callback: CharacteristicGetCallback) { this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.log.debug('getBritness: ' + JSON.stringify(response)); if(response){ const brightness = response.brightness; if(brightness !== undefined){ @@ -156,12 +151,15 @@ export class L530Accessory extends TPLinkPlatformAccessory { // you must call the callback function callback(null, brightness); }else{ + this.log.debug('getBritness: ' + 167); callback(new Error('unreachable'), 0); } } else{ + this.log.debug('getBritness: ' + 171); callback(new Error('unreachable'), 0); } }).catch(() => { + this.log.debug('getBritness: ' + 175); callback(new Error('unreachable'), 0); }); } @@ -197,6 +195,8 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ getColorTemp(callback: CharacteristicGetCallback) { this.tpLinkAccessory.getColorTemp().then((response) => { + this.log.debug('getColorTemp: ' + JSON.stringify(response)); + if(response !== undefined){ const color_temp = response; @@ -208,9 +208,14 @@ export class L530Accessory extends TPLinkPlatformAccessory { // you must call the callback function callback(null, color_temp); }else{ + this.log.debug('getColorTemp: ' + 224); + callback(new Error('unreachable'), 0); } - }).catch(() => { + }).catch((error) => { + this.log.debug('getColorTemp: ' + 229); + this.log.debug('error: ' + error); + callback(new Error('unreachable'), 0); }); } @@ -221,6 +226,7 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ setHue(value: CharacteristicValue, callback: CharacteristicSetCallback) { if(this.tpLinkAccessory.getSysInfo().device_on){ + this.log.debug('Homekit Hue: ' + value); this.tpLinkAccessory.setColor(Math.round(value as number), this.tpLinkAccessory.getSysInfo().saturation).then((result) => { if(result){ this.tpLinkAccessory.getSysInfo().hue = Math.round(value as number); @@ -245,6 +251,8 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ getHue(callback: CharacteristicGetCallback) { this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.log.debug('getHue: ' + JSON.stringify(response)); + if(response){ let hue = response.hue; this.platform.log.debug('Get Characteristic Hue ->', hue); @@ -259,9 +267,15 @@ export class L530Accessory extends TPLinkPlatformAccessory { // you must call the callback function callback(null, hue); } else{ + this.log.debug('getHue: ' + 282); + callback(new Error('unreachable'), 0); } - }).catch(() => { + }).catch((error) => { + this.log.debug('Unreachable'); + this.log.debug('getHue: ' + 288); + this.log.debug('error: ' + error); + callback(new Error('unreachable'), 0); }); } @@ -272,6 +286,7 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ setSaturation(value: CharacteristicValue, callback: CharacteristicSetCallback) { if(this.tpLinkAccessory.getSysInfo().device_on){ + this.log.debug('Homekit Saturation: ' + value); this.tpLinkAccessory.setColor(this.tpLinkAccessory.getSysInfo().hue, Math.round(value as number)).then((result) => { if(result){ this.tpLinkAccessory.getSysInfo().saturation = Math.round(value as number); @@ -296,6 +311,8 @@ export class L530Accessory extends TPLinkPlatformAccessory { */ getSaturation(callback: CharacteristicGetCallback) { this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.log.debug('getSaturation: ' + JSON.stringify(response)); + if(response){ let saturation = response.saturation; @@ -310,18 +327,25 @@ export class L530Accessory extends TPLinkPlatformAccessory { // you must call the callback function callback(null, saturation); } else{ + this.log.debug('getSaturation: ' + 341); + callback(new Error('unreachable'), 0); } - }).catch(() => { + }).catch((error) => { + this.log.debug('getSaturation: ' + 346); + this.log.debug('error: ' + error); + callback(new Error('unreachable'), 0); }); } private updateConsumption(){ + this.log.debug('updateConsumption called'); this.tpLinkAccessory.getEnergyUsage().then((response) => { + this.log.debug('Get Characteristic Power consumption ->', JSON.stringify(response)); if (response && response.power_usage) { if(this.lastMeasurement){ - this.platform.log.debug('Get Characteristic Power consumption ->', JSON.stringify(response)); + this.log.debug('Get Characteristic Power consumption ->', JSON.stringify(response)); if (this.fakeGatoHistoryService ) { this.fakeGatoHistoryService.addEntry({ time: new Date().getTime() / 1000, @@ -388,8 +412,10 @@ export class L530Accessory extends TPLinkPlatformAccessory { } protected updateState(interval:number){ - this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.log.debug('updateState called'); + this.tpLinkAccessory.getDeviceInfo(true).then((response) => { if(response){ + this.log.info('Device Info: ' + JSON.stringify(response)); const isOn = response.device_on; const saturation = response.saturation; const hue = response.hue; diff --git a/src/platformP100Accessory.ts b/src/platformP100Accessory.ts index b9cf419..df18bf7 100644 --- a/src/platformP100Accessory.ts +++ b/src/platformP100Accessory.ts @@ -34,7 +34,7 @@ export class P100Accessory extends TPLinkPlatformAccessory{ } protected init(platform: TapoPlatform, updateInterval?: number){ - this.tpLinkAccessory.getDeviceInfo().then((sysInfo) => { + this.tpLinkAccessory.getDeviceInfo(true).then((sysInfo) => { // set accessory information this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'TP-Link') @@ -53,7 +53,7 @@ export class P100Accessory extends TPLinkPlatformAccessory{ this.service.getCharacteristic(this.platform.Characteristic.OutletInUse) .on('get', this.handleOutletInUseGet.bind(this)); - const interval = updateInterval ? updateInterval*1000 : 30000; + const interval = updateInterval ? updateInterval*1000 : 10000; this.log.debug('interval: ' + interval); setTimeout(()=>{ diff --git a/src/platformP110Accessory.ts b/src/platformP110Accessory.ts index 025e3e7..71f2f0d 100644 --- a/src/platformP110Accessory.ts +++ b/src/platformP110Accessory.ts @@ -41,7 +41,7 @@ export class P110Accessory extends TPLinkPlatformAccessory{ } protected init(platform: TapoPlatform, updateInterval?: number){ - this.tpLinkAccessory.getDeviceInfo().then((sysInfo) => { + this.tpLinkAccessory.getDeviceInfo(true).then((sysInfo) => { // set accessory information this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'TP-Link') @@ -56,18 +56,9 @@ export class P110Accessory extends TPLinkPlatformAccessory{ .on('set', this.setOn.bind(this)) // SET - bind to the `setOn` method below .on('get', this.getOn.bind(this)); // GET - bind to the `getOn` method below - this.service.getCharacteristic(this.platform.customCharacteristics.CurrentConsumptionCharacteristic) - .on('get', this.getCurrentConsumption.bind(this)); - - this.service.getCharacteristic(this.platform.customCharacteristics.TotalConsumptionCharacteristic) - .on('get', this.getTotalConsumption.bind(this)); - - this.service.getCharacteristic(this.platform.customCharacteristics.ResetConsumptionCharacteristic) - .on('set', this.resetConsumption.bind(this)); - this.updateConsumption(); - const interval = updateInterval ? updateInterval*1000 : 30000; + const interval = updateInterval ? updateInterval*1000 : 10000; setTimeout(()=>{ this.updateState(interval); }, interval); diff --git a/src/platformTPLinkAccessory.ts b/src/platformTPLinkAccessory.ts index 9e17185..01f45d4 100644 --- a/src/platformTPLinkAccessory.ts +++ b/src/platformTPLinkAccessory.ts @@ -17,15 +17,6 @@ export abstract class TPLinkPlatformAccessory { protected readonly updateInterval?: number, ) { this.log.debug('Start adding accessory: ' + accessory.context.device.host); - - - this.cron.schedule('*/10 * * * *', () => { - this.initialise(platform, updateInterval); - }); - - /*this.cron.schedule('0 0 * * *', () => { - this.initialise(platform, updateInterval); - });*/ } protected initialise(platform: TapoPlatform, updateInterval?: number):void{ @@ -98,14 +89,16 @@ export abstract class TPLinkPlatformAccessory { } else{ callback(new Error('unreachable'), false); } - }).catch(() => { - callback(new Error('unreachable'), false); + }).catch((error) => { + this.log.debug('error: ' + error); + + callback(new Error('unreachable'), 0); }); } protected updateState(interval:number){ this.platform.log.debug('Updating state'); - this.tpLinkAccessory.getDeviceInfo().then((response) => { + this.tpLinkAccessory.getDeviceInfo(true).then((response) => { if(response){ const isOn = response.device_on; diff --git a/src/utils/l510e.ts b/src/utils/l510e.ts index 3dd699f..25117f0 100644 --- a/src/utils/l510e.ts +++ b/src/utils/l510e.ts @@ -17,8 +17,8 @@ export default class L510E extends P100 { this.log.debug('Constructing L510E on host: ' + ipAddress); } - async getDeviceInfo(): Promise{ - return super.getDeviceInfo().then(() => { + async getDeviceInfo(force?:boolean): Promise{ + return super.getDeviceInfo(force).then(() => { return this.getSysInfo(); }); } diff --git a/src/utils/l520e.ts b/src/utils/l520e.ts index 17a4e5b..644ffdf 100644 --- a/src/utils/l520e.ts +++ b/src/utils/l520e.ts @@ -17,13 +17,13 @@ export default class L520E extends L510E { this.log.debug('Constructing L510E on host: ' + ipAddress); } - async getDeviceInfo(): Promise{ - return super.getDeviceInfo().then(() => { + async getDeviceInfo(force?:boolean): Promise{ + return super.getDeviceInfo(force).then(() => { return this.getSysInfo(); }); } - async setColorTemp(color_temp:number):Promise{ + async setColorTemp(color_temp:number):Promise{ const transformedColorTemp = this.transformColorTemp(color_temp); this.log.debug('Color Temp Tapo :' + transformedColorTemp); @@ -40,9 +40,7 @@ export default class L520E extends L510E { '"requestTimeMils": ' + Math.round(Date.now() * 1000) + ''+ '};'; - return this.handleRequest(payload).then(()=>{ - return true; - }); + return this.sendRequest(payload); } private transformColorTemp(value: number){ diff --git a/src/utils/l530.ts b/src/utils/l530.ts index 880cbe5..c806e2a 100644 --- a/src/utils/l530.ts +++ b/src/utils/l530.ts @@ -23,8 +23,8 @@ export default class L530 extends L520E { }; } - async getDeviceInfo(): Promise{ - return super.getDeviceInfo().then(() => { + async getDeviceInfo(force?:boolean): Promise{ + return super.getDeviceInfo(force).then(() => { return this.getSysInfo(); }); } @@ -36,6 +36,7 @@ export default class L530 extends L520E { if(!saturation){ saturation = 0; } + this.log.debug('Setting color: ' + hue + ', ' + saturation); const payload = '{'+ '"method": "set_device_info",'+ '"params": {'+ @@ -63,30 +64,64 @@ export default class L530 extends L520E { '"method": "get_device_usage",'+ '"requestTimeMils": ' + Math.round(Date.now() * 1000) + ''+ '};'; - return this.handleRequest(payload).then((response)=>{ - if(response && response.result){ - this._consumption = { - total: response.result.power_usage.today / 1000, - current: this._consumption ? response.result.power_usage.today - this._consumption.current : 0, - }; - } else{ - this._consumption = { - total: 0, - current: 0, - }; - } - - return response.result; - }).catch((error)=>{ - if(error.message.indexOf('9999') > 0){ - return this.reconnect().then(()=>{ - return this.handleRequest(payload).then(()=>{ - return true; + this.log.debug('getEnergyUsage called'); + + if(this.is_klap){ + this.log.debug('getEnergyUsage is klap'); + + return this.handleKlapRequest(payload).then((response)=>{ + this.log.debug('Consumption: ' + JSON.stringify(response)); + if(response && response.result){ + this._consumption = { + total: response.result.power_usage.today / 1000, + current: this._consumption ? response.result.power_usage.today - this._consumption.current : 0, + }; + } else{ + this._consumption = { + total: 0, + current: 0, + }; + } + + return response.result; + }).catch((error)=>{ + if(error.message.indexOf('9999') > 0){ + return this.reconnect().then(()=>{ + return this.handleKlapRequest(payload).then(()=>{ + return true; + }); }); - }); - } - return false; - }); + } + return false; + }); + }else{ + return this.handleRequest(payload).then((response)=>{ + this.log.debug('Consumption: ' + response); + if(response && response.result){ + this._consumption = { + total: response.result.power_usage.today / 1000, + current: this._consumption ? response.result.power_usage.today - this._consumption.current : 0, + }; + } else{ + this._consumption = { + total: 0, + current: 0, + }; + } + + return response.result; + }).catch((error)=>{ + if(error.message.indexOf('9999') > 0){ + return this.reconnect().then(()=>{ + return this.handleRequest(payload).then(()=>{ + return true; + }); + }); + } + return false; + }); + } + } public getPowerConsumption():ConsumptionInfo{ diff --git a/src/utils/newTpLinkCipher.ts b/src/utils/newTpLinkCipher.ts index 638094f..4f78d50 100644 --- a/src/utils/newTpLinkCipher.ts +++ b/src/utils/newTpLinkCipher.ts @@ -1,3 +1,6 @@ +import { Logger } from "homebridge"; +import { TextDecoder } from "util"; + export default class NewTpLinkCipher{ public iv: any; public key: any; @@ -5,7 +8,7 @@ export default class NewTpLinkCipher{ public sig: any; public seq: any; - constructor(localSeed: Buffer, remoteSeed: Buffer, authHash: Buffer | undefined) { + constructor(localSeed: Buffer, remoteSeed: Buffer, authHash: Buffer | undefined, public readonly log: Logger) { if(authHash){ this.calculateKey(localSeed, remoteSeed, authHash); this.calculateIvSeq(localSeed, remoteSeed, authHash); @@ -47,8 +50,24 @@ export default class NewTpLinkCipher{ decipher.update(data.subarray(32)), decipher.final(), ]); - - return decrypted.toString('utf8'); + + const dec = decrypted.toString('utf8'); + this.log.debug('decrypted: ' + dec); + + //Some times the json returned is malformed, or the number returned in error_code + //is not valid e.g. -0301, so we need to use regex to replace the malformed/invalid json parts + let dec_fixed = ''; + if(dec.match(/{"error_code":([-0-9]+)[^,}]$/)){ + dec_fixed = dec.replace(/{"error_code":([-0-9]+)[^,}]/gm, '{"error_code":"$1"}'); + } else if(dec.match(/{"error_code":([-0-9]+)}$/)){ + dec_fixed = dec.replace(/{"error_code":([-0-9]+)}$/gm, '{"error_code":"$1"}'); + } else{ + dec_fixed = dec.replace(/{"error_code":([-0-9]+)[^,}](.*)/gm, '{"error_code":"$1",$2'); + } + + this.log.debug('decrypted fixed: ' + dec_fixed); + + return dec_fixed; } private calculateKey(local_seed: Buffer, remote_seed: Buffer, auth_hash: Buffer) { diff --git a/src/utils/p100.ts b/src/utils/p100.ts index 4d18464..ec04a1f 100644 --- a/src/utils/p100.ts +++ b/src/utils/p100.ts @@ -317,7 +317,7 @@ export default class P100 implements TpLinkAccessory{ return this.raw_request('handshake2', req, 'text').then((res) => { this.log.debug('Handshake 2 successful'); - this.newTpLinkCipher = new NewTpLinkCipher(local_seed, remote_seed, auth_hash); + this.newTpLinkCipher = new NewTpLinkCipher(local_seed, remote_seed, auth_hash, this.log); this.log.debug('Init cipher successful'); return; @@ -358,8 +358,8 @@ export default class P100 implements TpLinkAccessory{ } } - async getDeviceInfo(): Promise { - if (this.getSysInfo() && (Date.now() - this.getSysInfo().last_update) < 2000) { + async getDeviceInfo(force?:boolean): Promise { + if (!force) { return new Promise((resolve) => { resolve(this.getSysInfo()); }); @@ -562,12 +562,12 @@ export default class P100 implements TpLinkAccessory{ return false; }); } else { - return this.newHandleRequest(payload).then((result) => { + return this.handleKlapRequest(payload).then((result) => { return result ? true : false; }).catch((error) => { if (error.message.indexOf('9999') > 0 && this._reconnect_counter <= 3) { return this.newReconnect().then(() => { - return this.newHandleRequest(payload).then((result) => { + return this.handleKlapRequest(payload).then((result) => { return result ? true : false; }); }); @@ -578,22 +578,6 @@ export default class P100 implements TpLinkAccessory{ } } - protected async newSendRequest(payload: string): Promise { - return this.handleRequest(payload).then((result) => { - return result ? true : false; - }).catch((error) => { - if (error.message.indexOf('9999') > 0 && this._reconnect_counter <= 3) { - return this.reconnect().then(() => { - return this.handleRequest(payload).then((result) => { - return result ? true : false; - }); - }); - } - this._reconnect_counter = 0; - return false; - }); - } - protected handleRequest(payload: string): Promise { const URL = 'http://' + this.ip + '/app?token=' + this.token; @@ -652,7 +636,7 @@ export default class P100 implements TpLinkAccessory{ }); } - protected newHandleRequest(payload: string): Promise { + protected handleKlapRequest(payload: string): Promise { if (this.newTpLinkCipher) { const data = this.newTpLinkCipher.encrypt(payload); diff --git a/src/utils/p110.ts b/src/utils/p110.ts index d113909..de579ca 100644 --- a/src/utils/p110.ts +++ b/src/utils/p110.ts @@ -24,21 +24,40 @@ export default class P110 extends P100 { '"requestTimeMils": ' + Math.round(Date.now() * 1000) + ''+ '};'; - return this.handleRequest(payload).then((response)=>{ - if(response && response.result){ - this._consumption = { - current: response.result.current_power / 1000, - total: response.result.today_energy / 1000, - }; - } else{ - this._consumption = { - current: 0, - total: 0, - }; - } - - return response.result; - }); + if(this.is_klap){ + return this.handleKlapRequest(payload).then((response)=>{ + if(response && response.result){ + this._consumption = { + current: response.result.current_power / 1000, + total: response.result.today_energy / 1000, + }; + } else{ + this._consumption = { + current: 0, + total: 0, + }; + } + + return response.result; + }); + }else{ + return this.handleRequest(payload).then((response)=>{ + if(response && response.result){ + this._consumption = { + current: response.result.current_power / 1000, + total: response.result.today_energy / 1000, + }; + } else{ + this._consumption = { + current: 0, + total: 0, + }; + } + + return response.result; + }); + } + } public getPowerConsumption():ConsumptionInfo{ diff --git a/src/utils/tplinkAccessory.ts b/src/utils/tplinkAccessory.ts index 7dc6d62..f6fd0b2 100644 --- a/src/utils/tplinkAccessory.ts +++ b/src/utils/tplinkAccessory.ts @@ -17,7 +17,7 @@ export interface TpLinkAccessory{ setPowerState(state: boolean): Promise; - getDeviceInfo(): Promise ; + getDeviceInfo(force?:boolean): Promise ; getSysInfo(): PlugSysinfo; @@ -27,7 +27,7 @@ export interface TpLinkAccessory{ setBrightness?(brightness:number):Promise; - setColorTemp?(color_temp:number):Promise; + setColorTemp?(color_temp:number):Promise; getColorTemp?(): Promise;