Skip to content

Instantly share code, notes, and snippets.

@9zigen

9zigen/alab-ph-sensor.js

Last active Mar 15, 2021
Embed
What would you like to do?
/* eslint-disable */
const {
fromZigbeeConverters,
toZigbeeConverters,
exposes
} = require('zigbee-herdsman-converters');
const ACCESS_STATE = 0b001, ACCESS_WRITE = 0b010, ACCESS_READ = 0b100;
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 = {
ph_measurement: {
cluster: 'pHMeasurement',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
if (msg.data.hasOwnProperty('measuredValue')) {
const ph_measurement = parseFloat(msg.data['measuredValue']) / 100.0;
return {'ph_measurement': ph_measurement};
}
},
},
alab_ph_sensor: {
cluster: 'pHMeasurement',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
return {
'scan_interval': parseInt(msg.data[0x1000]),
'calibration_low': parseFloat(msg.data[0x1001] / 100),
'calibration_mid': parseFloat(msg.data[0x1002] / 100),
'calibration_high': parseFloat(msg.data[0x1003] / 100),
'probe_raw': parseInt(msg.data[0x1010])
};
},
},
};
const tz = {
alab_ph_calibration: {
key: ['scan_interval', 'calibration_low', 'calibration_mid', 'calibration_high', 'probe_raw'],
convertSet: async (entity, key, value, meta) => {
const payloads = {
'scan_interval': {0x1000: {value, type: 0x21}},
'calibration_low': {0x1001: {value, type: 0x21}},
'calibration_mid': {0x1002: {value, type: 0x21}},
'calibration_high': {0x1003: {value, type: 0x21}},
'probe_raw': {0x1010: {value, type: 0x21}},
}
await entity.write('pHMeasurement', payloads[key]);
},
convertGet: async (entity, key, meta) => {
return await entity.read('pHMeasurement', [0x1000, 0x1001, 0x1002, 0x1003]);
}
},
}
const device = {
zigbeeModel: ['Alab-PhSensor-1.0'],
model: 'Alab-PhSensor-1.0',
vendor: 'Alab',
description: '[DiY Wireless PH Sensor](https://github.com/9zigen/zigbee-ph-sensor)',
supports: 'temperature, humidity',
fromZigbee: [
fromZigbeeConverters.temperature,
fromZigbeeConverters.humidity,
fromZigbeeConverters.battery,
fz.ph_measurement, fz.alab_ph_sensor],
toZigbee: [toZigbeeConverters.factory_reset, tz.alab_ph_calibration],
meta: {
configureKey: 1,
timeout: 10000
},
configure: async (device, coordinatorEndpoint) => {
const endpoint = device.getEndpoint(10);
await bind(endpoint, coordinatorEndpoint, ['msTemperatureMeasurement', 'msRelativeHumidity', 'pHMeasurement', 'genPowerCfg']);
// await configureReporting.batteryPercentageRemaining(endpoint);
// await configureReporting.temperature(endpoint);
// await configureReporting.humidity(endpoint, 60);
/* pH Reporting */
// const payload = [{
// attribute: 'pHMeasurement',
// minimumReportInterval: 60,
// maximumReportInterval: repInterval.HOUR,
// }];
// await endpoint.configureReporting('measuredValue', payload);
},
exposes: [
exposes.numeric('pH', ACCESS_STATE).withUnit('pH'),
exposes.numeric('probe_raw', ACCESS_STATE).withUnit('mV'),
exposes.numeric('temperature', ACCESS_STATE).withUnit('°C'),
exposes.numeric('humidity', ACCESS_STATE).withUnit('%'),
//device options
exposes.numeric('scan_interval', ACCESS_STATE | ACCESS_WRITE | ACCESS_READ).withUnit('seconds'),
exposes.numeric('calibration_low', ACCESS_STATE | ACCESS_WRITE | ACCESS_READ).withUnit('pH Units'),
exposes.numeric('calibration_mid', ACCESS_STATE | ACCESS_WRITE | ACCESS_READ).withUnit('pH Units'),
exposes.numeric('calibration_high', ACCESS_STATE | ACCESS_WRITE | ACCESS_READ).withUnit('pH Units'),
],
};
module.exports = device;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment