From b4e5084ec25b1008f6db42ffc218a56cb15144bd Mon Sep 17 00:00:00 2001 From: callemand Date: Mon, 25 Sep 2023 09:02:08 +0200 Subject: [PATCH] Add energy value from tuya devices --- .../tuya/lib/device/tuya.convertDevice.js | 6 ++- .../tuya/lib/device/tuya.deviceMapping.js | 45 ++++++++++++++++++- .../device/feature/tuya.deviceMapping.test.js | 24 ++++++++-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/server/services/tuya/lib/device/tuya.convertDevice.js b/server/services/tuya/lib/device/tuya.convertDevice.js index b3094ae487..e0ca57440a 100644 --- a/server/services/tuya/lib/device/tuya.convertDevice.js +++ b/server/services/tuya/lib/device/tuya.convertDevice.js @@ -12,11 +12,15 @@ const logger = require('../../../../utils/logger'); function convertDevice(tuyaDevice) { const { name, product_name: model, id, specifications = {} } = tuyaDevice; const externalId = `tuya:${id}`; - const { functions = [] } = specifications; + const { functions = [], status = [] } = specifications; logger.debug(`Tuya convert device"${name}, ${model}"`); // Groups functions and status on same code const groups = {}; + status.forEach((stat) => { + const { code } = stat; + groups[code] = { ...stat, readOnly: true }; + }); functions.forEach((func) => { const { code } = func; groups[code] = { ...func, readOnly: false }; diff --git a/server/services/tuya/lib/device/tuya.deviceMapping.js b/server/services/tuya/lib/device/tuya.deviceMapping.js index ee76cd650f..0e88cce7d0 100644 --- a/server/services/tuya/lib/device/tuya.deviceMapping.js +++ b/server/services/tuya/lib/device/tuya.deviceMapping.js @@ -1,4 +1,10 @@ -const { DEVICE_FEATURE_TYPES, DEVICE_FEATURE_CATEGORIES, COVER_STATE } = require('../../../../utils/constants'); +const { + DEVICE_FEATURE_TYPES, + DEVICE_FEATURE_CATEGORIES, + DEVICE_FEATURE_UNITS, + COVER_STATE, +} = require('../../../../utils/constants'); + const { intToRgb, rgbToHsb, rgbToInt, hsbToRgb } = require('../../../../utils/colors'); const SWITCH_LED = 'switch_led'; @@ -8,6 +14,11 @@ const COLOUR_DATA_V2 = 'colour_data_v2'; const COLOUR_DATA = 'colour_data'; +const ADD_ELE = 'add_ele'; +const CUR_CURRENT = 'cur_current'; +const CUR_POWER = 'cur_power'; +const CUR_VOLTAGE = 'cur_voltage'; + const SWITCH_1 = 'switch_1'; const SWITCH_2 = 'switch_2'; const SWITCH_3 = 'switch_3'; @@ -66,6 +77,26 @@ const mappings = { category: DEVICE_FEATURE_CATEGORIES.CURTAIN, type: DEVICE_FEATURE_TYPES.CURTAIN.POSITION, }, + [ADD_ELE]: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SWITCH.ENERGY, + unit: DEVICE_FEATURE_UNITS.KILOWATT_HOUR, + }, + [CUR_CURRENT]: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SWITCH.CURRENT, + unit: DEVICE_FEATURE_UNITS.MILLI_AMPERE, + }, + [CUR_POWER]: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SWITCH.POWER, + unit: DEVICE_FEATURE_UNITS.WATT, + }, + [CUR_VOLTAGE]: { + category: DEVICE_FEATURE_CATEGORIES.SWITCH, + type: DEVICE_FEATURE_TYPES.SWITCH.VOLTAGE, + unit: DEVICE_FEATURE_UNITS.VOLT, + }, }; const writeValues = { @@ -135,6 +166,18 @@ const readValues = { [DEVICE_FEATURE_TYPES.SWITCH.BINARY]: (valueFromDevice) => { return valueFromDevice === true ? 1 : 0; }, + [DEVICE_FEATURE_TYPES.SWITCH.ENERGY]: (valueFromDevice) => { + return parseInt(valueFromDevice, 10); + }, + [DEVICE_FEATURE_TYPES.SWITCH.CURRENT]: (valueFromDevice) => { + return parseInt(valueFromDevice, 10); + }, + [DEVICE_FEATURE_TYPES.SWITCH.POWER]: (valueFromDevice) => { + return parseInt(valueFromDevice, 10) / 10; + }, + [DEVICE_FEATURE_TYPES.SWITCH.VOLTAGE]: (valueFromDevice) => { + return parseInt(valueFromDevice, 10) / 10; + }, }, [DEVICE_FEATURE_CATEGORIES.CURTAIN]: { [DEVICE_FEATURE_TYPES.CURTAIN.STATE]: (valueFromDevice) => { diff --git a/server/test/services/tuya/lib/device/feature/tuya.deviceMapping.test.js b/server/test/services/tuya/lib/device/feature/tuya.deviceMapping.test.js index 410be92c79..1e3b558f93 100644 --- a/server/test/services/tuya/lib/device/feature/tuya.deviceMapping.test.js +++ b/server/test/services/tuya/lib/device/feature/tuya.deviceMapping.test.js @@ -69,9 +69,27 @@ describe('Tuya device mapping', () => { ); expect(result).to.eq(300); }); - it('switch binary', () => { - const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.BINARY](true); - expect(result).to.eq(1); + describe('binary', () => { + it('switch', () => { + const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.BINARY](true); + expect(result).to.eq(1); + }); + it('energy', () => { + const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.ENERGY]('30'); + expect(result).to.eq(30); + }); + it('current', () => { + const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.CURRENT]('20'); + expect(result).to.eq(20); + }); + it('power', () => { + const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.POWER]('2245'); + expect(result).to.eq(224.5); + }); + it('voltage', () => { + const result = readValues[DEVICE_FEATURE_CATEGORIES.SWITCH][DEVICE_FEATURE_TYPES.SWITCH.VOLTAGE]('120'); + expect(result).to.eq(12.0); + }); }); describe('curtain state', () => { it('open', () => {