Skip to content

Commit

Permalink
Add energy value from tuya devices
Browse files Browse the repository at this point in the history
  • Loading branch information
callemand authored and Corentin Allemand committed Oct 16, 2023
1 parent 33b2264 commit b4e5084
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
6 changes: 5 additions & 1 deletion server/services/tuya/lib/device/tuya.convertDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
45 changes: 44 additions & 1 deletion server/services/tuya/lib/device/tuya.deviceMapping.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b4e5084

Please sign in to comment.