Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Feb 19, 2023
1 parent 292cf07 commit 8a5ec48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
23 changes: 23 additions & 0 deletions devices/tuya.js
Original file line number Diff line number Diff line change
Expand Up @@ -4068,4 +4068,27 @@ module.exports = [
],
},
},
{
zigbeeModel: ['TS0049'],
model: 'TS0049',
vendor: 'TuYa',
description: 'Water valve',
fromZigbee: [tuya.fz.datapoints],
toZigbee: [tuya.tz.datapoints],
onEvent: tuya.onEventSetLocalTime,
configure: tuya.configureMagicPacket,
exposes: [tuya.exposes.errorStatus(), tuya.exposes.switch(), tuya.exposes.batteryState(),
tuya.exposes.countdown().withValueMin(0).withValueMax(255).withUnit('minutes')
.withDescription('Max on time in minutes'),
],
meta: {
tuyaSendCommand: 'sendData',
tuyaDatapoints: [
[26, 'error_status', tuya.valueConverter.raw],
[101, 'state', tuya.valueConverter.onOff],
[111, 'countdown', tuya.valueConverter.raw],
[115, 'battery_state', tuya.valueConverter.batteryState],
],
},
},
];
17 changes: 9 additions & 8 deletions lib/tuya.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,8 @@ const tuyaTz = {
convertSet: async (entity, key, value, meta) => {
// A set converter is only called once; therefore we need to loop
const state = {};
if (!meta.mapped.meta || !meta.mapped.meta.tuyaDatapoints) throw new Error('No datapoints map defined');
const datapoints = meta.mapped.meta.tuyaDatapoints;
const datapoints = utils.getMetaValue(entity, meta.mapped, 'tuyaDatapoints', undefined, undefined);
if (!datapoints) throw new Error('No datapoints map defined');
for (const [key, value] of Object.entries(meta.message)) {
const convertedKey = meta.mapped.meta.multiEndpoint ? `${key}_${meta.endpoint_name}` : key;
const dpEntry = datapoints.find((d) => d[1] === convertedKey);
Expand All @@ -1631,20 +1631,21 @@ const tuyaTz = {
if (dpEntry[3] && dpEntry[3].skip && dpEntry[3].skip(meta)) continue;
const dpId = dpEntry[0];
const convertedValue = await dpEntry[2].to(value, meta);
const sendCommand = utils.getMetaValue(entity, meta.mapped, 'tuyaSendCommand', undefined, 'dataRequest');
if (convertedValue === undefined) {
// conversion done inside converter, ignore.
} else if (typeof convertedValue === 'boolean') {
await sendDataPointBool(entity, dpId, convertedValue, 'dataRequest', 1);
await sendDataPointBool(entity, dpId, convertedValue, sendCommand, 1);
} else if (typeof convertedValue === 'number') {
await sendDataPointValue(entity, dpId, convertedValue, 'dataRequest', 1);
await sendDataPointValue(entity, dpId, convertedValue, sendCommand, 1);
} else if (typeof convertedValue === 'string') {
await sendDataPointStringBuffer(entity, dpId, convertedValue, 'dataRequest', 1);
await sendDataPointStringBuffer(entity, dpId, convertedValue, sendCommand, 1);
} else if (Array.isArray(convertedValue)) {
await sendDataPointRaw(entity, dpId, convertedValue, 'dataRequest', 1);
await sendDataPointRaw(entity, dpId, convertedValue, sendCommand, 1);
} else if (convertedValue instanceof Enum) {
await sendDataPointEnum(entity, dpId, convertedValue.valueOf(), 'dataRequest', 1);
await sendDataPointEnum(entity, dpId, convertedValue.valueOf(), sendCommand, 1);
} else if (convertedValue instanceof Bitmap) {
await sendDataPointBitmap(entity, dpId, convertedValue.valueOf(), 'dataRequest', 1);
await sendDataPointBitmap(entity, dpId, convertedValue.valueOf(), sendCommand, 1);
} else {
throw new Error(`Don't know how to send type '${typeof convertedValue}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('index.js', () => {
}

if (device.meta) {
containsOnly(['disableActionGroup', 'multiEndpoint', 'multiEndpointSkip', 'multiEndpointEnforce', 'applyRedFix', 'disableDefaultResponse', 'enhancedHue', 'timeout', 'supportsHueAndSaturation', 'battery', 'coverInverted', 'turnsOffAtBrightness1', 'coverStateFromTilt', 'pinCodeCount', 'tuyaThermostatSystemMode', 'tuyaThermostatPreset', 'tuyaDatapoints', 'tuyaThermostatPresetToSystemMode', 'thermostat', 'fanStateOn', 'separateWhite', 'publishDuplicateTransaction'], Object.keys(device.meta));
containsOnly(['disableActionGroup', 'multiEndpoint', 'multiEndpointSkip', 'multiEndpointEnforce', 'applyRedFix', 'disableDefaultResponse', 'enhancedHue', 'timeout', 'supportsHueAndSaturation', 'battery', 'coverInverted', 'turnsOffAtBrightness1', 'coverStateFromTilt', 'pinCodeCount', 'tuyaThermostatSystemMode', 'tuyaThermostatPreset', 'tuyaDatapoints', 'tuyaThermostatPresetToSystemMode', 'thermostat', 'fanStateOn', 'separateWhite', 'publishDuplicateTransaction', 'tuyaSendCommand'], Object.keys(device.meta));
}

if (device.zigbeeModel) {
Expand Down

0 comments on commit 8a5ec48

Please sign in to comment.