Skip to content

Instantly share code, notes, and snippets.

@pyos

pyos/patch.js Secret

Last active August 22, 2022 15:54
Show Gist options
  • Save pyos/2e2a768dd1eba25bb8bc8396368463ca to your computer and use it in GitHub Desktop.
Save pyos/2e2a768dd1eba25bb8bc8396368463ca to your computer and use it in GitHub Desktop.
'use strict';
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const utils = require('zigbee-herdsman-converters/lib/utils');
const globalStore = require('zigbee-herdsman-converters/lib/store');
class PatchConverters {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
}
async start() {
this.patch(fz, fzPatch, fzOriginal);
this.patch(tz, tzPatch, tzOriginal);
}
async stop() {
this.patch(fz, fzOriginal, {});
this.patch(tz, tzOriginal, {});
Object.keys(fzOriginal).forEach(k => delete fzOriginal[k]);
Object.keys(tzOriginal).forEach(k => delete tzOriginal[k]);
}
patch(target, source, original) {
for (const key of Object.keys(source)) {
if (source[key] === undefined) {
original[key] = target[key];
delete target[key];
} else if (typeof target[key] === 'object' && typeof source[key] === 'object') {
this.patch(target[key], source[key], original[key] = {});
} else {
original[key] = target[key];
target[key] = source[key];
}
}
}
}
module.exports = PatchConverters;
const fzOriginal = {};
const fzPatch = {};
const tzOriginal = {};
const tzPatch = {
light_onoff_brightness: {
convertSet: async (entity, key, value, meta) => {
const {message, state} = meta;
if (
// when the light is off and on_level has previously been read...
state.state === 'OFF' &&
state.hasOwnProperty('level_config') && state.level_config.hasOwnProperty('on_level') &&
// ...and the user wants to issue an "on" command...
message.state && (message.state.toLowerCase() == 'on' || message.state.toLowerCase() == 'toggle') &&
message.brightness === undefined &&
// ...but zigbee2mqtt has to use "moveToLevelWithOnOff" instead...
(utils.getTransition(entity, 'brightness', meta).specified || globalStore.getValue(entity, 'turnedOffWithTransition'))
) {
// ...use the cached on_level instead of re-reading it from the device.
const oldBrightness = globalStore.getValue(entity, 'brightness',
utils.getObjectProperty(meta.state, 'brightness', 254));
message.brightness = state.level_config.on_level === "previous" ? oldBrightness : state.level_config.on_level;
}
return await tzOriginal.light_onoff_brightness.convertSet(entity, key, value, meta);
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment