Skip to content

Instantly share code, notes, and snippets.

@Koenkk

Koenkk/ext.js Secret

Last active May 20, 2023 12:25
Show Gist options
  • Save Koenkk/c7bb3c0a01bf48fd1c9ee89aaf2316bb to your computer and use it in GitHub Desktop.
Save Koenkk/c7bb3c0a01bf48fd1c9ee89aaf2316bb to your computer and use it in GitHub Desktop.
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 ota = require('zigbee-herdsman-converters/lib/ota');
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 e = exposes.presets;
const ea = exposes.access;
const fzLocal = {
TS011F_electrical_measurement: {
...fz.electrical_measurement,
convert: (model, msg, publish, options, meta) => {
const result = fz.electrical_measurement.convert(model, msg, publish, options, meta);
const lookup = {power: 'activePower', current: 'rmsCurrent', voltage: 'rmsVoltage'};
// Wait 5 seconds before reporting a 0 value as this could be an invalid measurement.
// https://github.com/Koenkk/zigbee2mqtt/issues/16709#issuecomment-1509599046
if (result && ['_TZ3000_gvn91tmx', '_TZ3000_amdymr7l', '_TZ3000_typdpbpg', '_TZ3000_hdopuwv6'].includes(meta.device.manufacturerName)) {
for (const key of ['power', 'current', 'voltage']) {
if (key in result) {
const value = result[key];
clearTimeout(globalStore.getValue(msg.endpoint, key));
if (value === 0) {
const configuredReporting = msg.endpoint.configuredReportings.find((c) =>
c.cluster.name === 'haElectricalMeasurement' && c.attribute.name === lookup[key]);
const time = ((configuredReporting ? configuredReporting.minimumReportInterval : 5) * 2) + 1;
console.log('NEXT 0 publish time', time);
globalStore.putValue(msg.endpoint, key, setTimeout(() => publish({[key]: value}), time * 1000));
delete result[key];
}
}
}
}
return result;
},
},
}
const definition = {
// Note: below you will find the TS011F_plug_2 and TS011F_plug_3. These are identified via a fingerprint and
// thus preferred above the TS011F_plug_1 if the fingerprint matches
zigbeeModel: ['TS011F'],
model: 'TS011F_plug_1',
description: 'Smart plug (with power monitoring) CUSTOM',
vendor: 'TuYa',
whiteLabel: [{vendor: 'LELLKI', model: 'TS011F_plug'}, {vendor: 'NEO', model: 'NAS-WR01B'},
{vendor: 'BlitzWolf', model: 'BW-SHP15'}, {vendor: 'Nous', model: 'A1Z'}, {vendor: 'BlitzWolf', model: 'BW-SHP13'},
{vendor: 'MatSee Plus', model: 'PJ-ZSW01'}, {vendor: 'MODEMIX', model: 'MOD037'}, {vendor: 'MODEMIX', model: 'MOD048'},
{vendor: 'Coswall', model: 'CS-AJ-DE2U-ZG-11'}, {vendor: 'Aubess', model: 'TS011F_plug_1'}, {vendor: 'Immax', model: '07752L'},
tuya.whitelabel('NOUS', 'A1Z', 'Smart plug (with power monitoring)', ['_TZ3000_2putqrmw']),
],
ota: ota.zigbeeOTA,
extend: tuya.extend.switch({
electricalMeasurements: true, electricalMeasurementsFzConverter: fzLocal.TS011F_electrical_measurement,
powerOutageMemory: true, indicatorMode: true, childLock: true}),
configure: async (device, coordinatorEndpoint, logger) => {
await tuya.configureMagicPacket(device, coordinatorEndpoint, logger);
const endpoint = device.getEndpoint(1);
await reporting.bind(endpoint, coordinatorEndpoint, ['genOnOff', 'haElectricalMeasurement', 'seMetering']);
await reporting.rmsVoltage(endpoint, {change: 5});
await reporting.rmsCurrent(endpoint, {change: 50});
await reporting.activePower(endpoint, {change: 10});
await reporting.currentSummDelivered(endpoint);
endpoint.saveClusterAttributeKeyValue('haElectricalMeasurement', {acCurrentDivisor: 1000, acCurrentMultiplier: 1});
endpoint.saveClusterAttributeKeyValue('seMetering', {divisor: 100, multiplier: 1});
device.save();
},
};
module.exports = definition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment