Created
June 28, 2020 22:18
-
-
Save nurikk/b9c7389e0abf81fec9a1b99baa21f49c to your computer and use it in GitHub Desktop.
Example external converters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters'); | |
const getKey = (object, value) => { | |
for (const key in object) { | |
if (object[key] == value) return key; | |
} | |
}; | |
const bind = async (endpoint, target, clusters) => { | |
for (const cluster of clusters) { | |
await endpoint.bind(cluster, target); | |
} | |
}; | |
const fz = { | |
external_fz_converter_example: { | |
cluster: 'genMultistateInput', | |
type: ['readResponse', 'attributeReport'], | |
convert: (model, msg, publish, options, meta) => { | |
return { | |
action: 'dummy' | |
}; | |
}, | |
}, | |
}; | |
const tz = { | |
external_tz_converter_example: { | |
key: ['switch_type', 'switch_actions'], | |
convertSet: async (entity, key, value, meta) => { | |
//dummy | |
}, | |
}, | |
}; | |
const device = { | |
zigbeeModel: ['dummy device'], | |
model: 'dummy device', | |
vendor: 'dummy endor', | |
description: 'dummy description', | |
supports: '', | |
//this is example how to use converters from z2m | |
fromZigbee: [fz.external_fz_converter_example, zigbeeHerdsmanConverters.fromZigbeeConverters.battery], | |
//this is example how to use converters from z2m | |
toZigbee: [tz.external_tz_converter_example, zigbeeHerdsmanConverters.toZigbeeConverters.factory_reset], | |
meta: { | |
configureKey: 1, | |
}, | |
configure: async (device, coordinatorEndpoint) => { | |
//dummy configure | |
}, | |
}; | |
module.exports = device; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters'); | |
const getKey = (object, value) => { | |
for (const key in object) { | |
if (object[key] == value) return key; | |
} | |
}; | |
const bind = async (endpoint, target, clusters) => { | |
for (const cluster of clusters) { | |
await endpoint.bind(cluster, target); | |
} | |
}; | |
const fz = { | |
diyruz_freepad_clicks: { | |
cluster: 'genMultistateInput', | |
type: ['readResponse', 'attributeReport'], | |
convert: (model, msg, publish, options, meta) => { | |
const button = getKey(model.endpoint(msg.device), msg.endpoint.ID); | |
const lookup = { | |
0: 'hold', | |
1: 'single', | |
2: 'double', | |
3: 'triple', | |
4: 'quadruple', | |
255: 'release', | |
}; | |
const clicks = msg.data['presentValue']; | |
const action = lookup[clicks] ? lookup[clicks] : `many_${clicks}`; | |
return { | |
action: `${button}_${action}`, | |
}; | |
}, | |
}, | |
}; | |
const tz = { | |
diyruz_freepad_on_off_config: { | |
key: ['switch_type', 'switch_actions'], | |
convertSet: async (entity, key, value, meta) => { | |
const switchTypesLookup = { | |
toggle: 0x00, | |
momentary: 0x01, | |
multifunction: 0x02, | |
}; | |
const switchActionsLookup = { | |
on: 0x00, | |
off: 0x01, | |
toggle: 0x02, | |
}; | |
const intVal = parseInt(value, 10); | |
const switchType = switchTypesLookup.hasOwnProperty(value) ? switchTypesLookup[value] : intVal; | |
const switchActions = switchActionsLookup.hasOwnProperty(value) ? switchActionsLookup[value] : intVal; | |
const payloads = { | |
switch_type: { | |
switchType, | |
}, | |
switch_actions: { | |
switchActions, | |
}, | |
}; | |
await entity.write('genOnOffSwitchCfg', payloads[key]); | |
}, | |
}, | |
}; | |
const device = { | |
zigbeeModel: ['DIYRuZ_FreePad_ext'], | |
model: 'DIYRuZ_FreePad_ext', | |
vendor: 'DIYRuZ', | |
description: '[DiY 8/12/20 button keypad](http://modkam.ru/?p=1114)', | |
supports: 'single, double, triple, quadruple, many, hold/release', | |
fromZigbee: [fz.diyruz_freepad_clicks, zigbeeHerdsmanConverters.fromZigbeeConverters.battery], | |
toZigbee: [tz.diyruz_freepad_on_off_config, zigbeeHerdsmanConverters.toZigbeeConverters.factory_reset], | |
meta: { | |
configureKey: 1, | |
}, | |
configure: async (device, coordinatorEndpoint) => { | |
const endpoint = device.getEndpoint(1); | |
await bind(endpoint, coordinatorEndpoint, ['genPowerCfg']); | |
const payload = [{ | |
attribute: 'batteryPercentageRemaining', | |
minimumReportInterval: 0, | |
maximumReportInterval: 3600, | |
reportableChange: 0, | |
}, { | |
attribute: 'batteryVoltage', | |
minimumReportInterval: 0, | |
maximumReportInterval: 3600, | |
reportableChange: 0, | |
}]; | |
await endpoint.configureReporting('genPowerCfg', payload); | |
device.endpoints.forEach(async (ep) => { | |
await bind(ep, coordinatorEndpoint, ['genMultistateInput']); | |
}); | |
}, | |
endpoint: (device) => { | |
return { | |
button_1: 1, | |
button_2: 2, | |
button_3: 3, | |
button_4: 4, | |
button_5: 5, | |
button_6: 6, | |
button_7: 7, | |
button_8: 8, | |
button_9: 9, | |
button_10: 10, | |
button_11: 11, | |
button_12: 12, | |
button_13: 13, | |
button_14: 14, | |
button_15: 15, | |
button_16: 16, | |
button_17: 17, | |
button_18: 18, | |
button_19: 19, | |
button_20: 20, | |
}; | |
}, | |
}; | |
module.exports = device; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment