Skip to content

Instantly share code, notes, and snippets.

@KipK
Created January 26, 2024 12:24
Show Gist options
  • Save KipK/249f6275105140ca998e1452c71cabf4 to your computer and use it in GitHub Desktop.
Save KipK/249f6275105140ca998e1452c71cabf4 to your computer and use it in GitHub Desktop.
TS0052-1ch.js
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
const reporting = require('zigbee-herdsman-converters/lib/reporting');
const extend = require('zigbee-herdsman-converters/lib/extend');
const legacy = require('zigbee-herdsman-converters/lib/legacy');
const ota = require('zigbee-herdsman-converters/lib/ota');
const e = exposes.presets;
const ea = exposes.access;
const tuya = require("zigbee-herdsman-converters/lib/tuya");
const utils = require('zigbee-herdsman-converters/lib/utils');
const globalStore = require('zigbee-herdsman-converters/lib/store');
const {
precisionRound, mapNumberRange, isLegacyEnabled, toLocalISOString, numberWithinRange, hasAlreadyProcessedMessage,
calibrateAndPrecisionRoundOptions, addActionGroup, postfixWithEndpointName, getKey,
batteryVoltageToPercentage, getMetaValue,
} = require('zigbee-herdsman-converters/lib/utils');
const fzLocal = {
brightness: {
cluster: 'genLevelCtrl',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
if (msg.data.hasOwnProperty('61440')) {
const property = postfixWithEndpointName('brightness', msg, model);
return {[property]: msg.data['61440']};
}
},
},
}
const tuyaBase = tuya.extend.light_onoff_brightness({disablePowerOnBehavior: true, disableMoveStep: false, disableTransition: false});
const minmax_brightness = {
tz: {
key: ['min_brightness', 'max_brightness'],
convertSet: async (entity, key, value, meta) => {
if (key == 'min_brightness') {
await entity.write('genLevelCtrl', { minLevel: value });
}
if (key == 'max_brightness') {
await entity.write('genLevelCtrl', { maxLevel: value });
}
return {state: {[key]: value}};
},
convertGet: async (entity, key, meta) => await entity.read('genLevelCtr>
},
fz: {
cluster: 'genLevelCtrl',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const result = {};
if (msg.data.hasOwnProperty('minLevel')) {
result[utils.postfixWithEndpointName('min_brightness', msg, model, meta)] = msg.data['minLevel'];
}
if (msg.data.hasOwnProperty('maxLevel')) {
result[utils.postfixWithEndpointName('max_brightness', msg, model, meta)] = msg.data['maxLevel'];
}
return result;
}
}
};
const definition = {
// Since a lot of Tuya devices use the same modelID, but use different data points
// it's usually necessary to provide a fingerprint instead of a zigbeeModel
fingerprint: tuya.fingerprint('TS0052', ['_TZ3000_qyo2yzwk']),
model: 'TS0052',
vendor: 'TuYa', // Vendor of the device (only used for documentation and startup logging)
description: 'Mini Dimmable Switch', // Description of the device, copy from vendor site. (only used for documentation and startup logging)
fromZigbee: tuyaBase.fromZigbee.concat([tuya.fz.power_on_behavior_2, tuya.fz.switch_type, minmax_brightness.fz],
toZigbee: tuyaBase.toZigbee.concat([tuya.tz.power_on_behavior_2, tuya.tz.switch_type, minmax_brightness.tz,tuya.tz.do_not_disturb])
exposes: [e.linkquality(),e.light_brightness(),tuya.exposes.lightBrightnessWithMinMax(),e.power_on_behavior(),tuya.exposes.switchType(),tuya.exposes.doNotDisturb()],
configure: async (device, coordinatorEndpoint, logger) => {
await tuya.configureMagicPacket(device, coordinatorEndpoint, logger);
await extend.light_onoff_brightness().configure(device, coordinatorEndpoint, logger);
await reporting.bind(device.getEndpoint(1), coordinatorEndpoint, ['genOnOff', 'genLevelCtrl']);
},
};
module.exports = definition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment