Created
November 25, 2023 21:42
-
-
Save 9zigen/08e315809dc64cd7b88cd636276eec3b to your computer and use it in GitHub Desktop.
inputs as switch
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
/* Custom converter for 4/8/12 channel zigbee switch */ | |
const { | |
fromZigbeeConverters, | |
toZigbeeConverters, | |
exposes | |
} = require('zigbee-herdsman-converters'); | |
const e = exposes.presets; | |
const ea = exposes.access; | |
function hasEndpoints(device, endpoints) { | |
const eps = device.endpoints.map((e) => e.ID); | |
for (const endpoint of endpoints) { | |
if (!eps.includes(endpoint)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
const fzLocal = { | |
input: { | |
cluster: 'genAnalogInput', | |
type: ['attributeReport', 'readResponse'], | |
convert: (model, msg, publish, options, meta) => { | |
const payload = {}; | |
payload['sw' + (msg.endpoint.ID - 4)] = msg.data["presentValue"]? 'on' : 'off'; | |
return payload; | |
} | |
}, | |
switch: { | |
cluster: 'genOnOff', | |
type: ['attributeReport'], | |
convert: (model, msg, publish, options, meta) => { | |
const payload = {}; | |
payload['state_l' + (msg.endpoint.ID)] = msg.data["onOff"]? 'ON' : 'OFF'; | |
return payload; | |
} | |
} | |
} | |
const tzLocal = { | |
input: { | |
key: ['in1', 'in2', 'in3', 'in4'], | |
convertGet: async (entity, key, meta) => { | |
const epId = parseInt(key.substr(1, 2)); | |
if (hasEndpoints(meta.device, [epId])) { | |
const endpoint = meta.device.getEndpoint(epId); | |
await endpoint.read('genAnalogInput', ['presentValue']); | |
} | |
}, | |
}, | |
} | |
const device = { | |
zigbeeModel: ['alab.switch'], | |
model: 'alab.switch', | |
vendor: 'Alab', | |
description: '[DiY Four Channels Relay Board](https://github.com/9zigen/)', | |
fromZigbee: [fromZigbeeConverters.on_off, fromZigbeeConverters.temperature, fzLocal.input, fzLocal.switch], | |
toZigbee: [toZigbeeConverters.on_off, tzLocal.input], | |
endpoint: (device) => { | |
return { | |
l1: 1, l2: 2, l3: 3, l4: 4, in1: 5, in2: 6, in3: 7, in4: 8 | |
}; | |
}, | |
exposes: [ | |
e.switch().withEndpoint('l1'), | |
e.switch().withEndpoint('l2'), | |
e.switch().withEndpoint('l3'), | |
e.switch().withEndpoint('l4'), | |
exposes.binary('sw1', ea.ALL, 'on', 'off').withDescription('Input 1 state'), | |
exposes.binary('sw2', ea.ALL, 'on', 'off').withDescription('Input 2 state'), | |
exposes.binary('sw3', ea.ALL, 'on', 'off').withDescription('Input 3 state'), | |
exposes.binary('sw4', ea.ALL, 'on', 'off').withDescription('Input 4 state') | |
] | |
}; | |
module.exports = device; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment