Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modern extend: help with example or docs? #8384

Open
matteovisotto opened this issue Nov 26, 2024 · 0 comments
Open

Modern extend: help with example or docs? #8384

matteovisotto opened this issue Nov 26, 2024 · 0 comments

Comments

@matteovisotto
Copy link

Hi,
I wrote a converter for a custom device having a custom cluster. Without modern extend it is necessary to add the cluster definition in cluster.ts, while the function deviceAddCustomCluster is now available. I read a lot of converters for available device but I'm not able to understand how the converterSet and Get are handled in the modern way (some attributes are both GET and SET).

I leave here my converter (classic, old way) which I would like to transform in a modern one to make it update safe. Someone can help me?

/*
ADD TO CLUSTER.JS in NODE-MODULE
mvManuWaterLevel: {
        ID: 0xfc01,
        attributes: {
            measuredValue: {ID: 0, type: dataType_1.default.UINT16},
            tankHeight: {ID: 1, type: dataType_1.default.UINT16},
            elevation: {ID: 2, type: dataType_1.default.UINT16},
        },
        commands: {},
        commandsResponse: {},
    },

*/

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 constants = require('zigbee-herdsman-converters/lib/constants');
const reporting = require('zigbee-herdsman-converters/lib/reporting');
const e = exposes.presets;
const ea = exposes.access;

const  fromZbLocal = {
    waterLevel: {
        cluster: 'mvManuWaterLevel',
        type: ['attributeReport', 'readResponse'],
        convert: (model, msg, publish, options, meta) => {
            if (msg.data.hasOwnProperty('measuredValue')) {
                const level = parseFloat(msg.data['measuredValue']) / 100.0;
                return {'waterLevel': level};
            }
        },
    },
    tankHeight: {
        cluster: 'mvManuWaterLevel',
        type: ['attributeReport', 'readResponse'],
        convert: (model, msg, publish, options, meta) => {
            if (msg.data.hasOwnProperty('tankHeight')) {
                const level = parseFloat(msg.data['tankHeight']) / 100.0;
                return {'tankHeight': level};
            }
        },
    },

    elevation: {
        cluster: 'mvManuWaterLevel',
        type: ['attributeReport', 'readResponse'],
        convert: (model, msg, publish, options, meta) => {
            if (msg.data.hasOwnProperty('elevation')) {
                const level = parseFloat(msg.data['elevation']) / 100.0;
                return {'elevation': level};
            }
        },
    },
}

const toZbLocal = {
    tankHeight: {
        key: ['tankHeight'],
        convertSet: async (entity, key, value, meta) => {
            var v = value * 100;
            const payload = {tankHeight: v}; 
            await entity.write('mvManuWaterLevel',payload);
            return {'tankHeight': v};
        },
        convertGet: async (entity, key, meta) => {
            await entity.read('mvManuWaterLevel', ['tankHeight']);
        },
    },
    elevation: {
        key: ['elevation'],
        convertSet: async (entity, key, value, meta) => {
            var v = value * 100;
            const payload = {elevation: v}; //UINT16
            await entity.write('mvManuWaterLevel',payload);
            return {'elevation': v};
        },
        convertGet: async (entity, key, meta) => {
            await entity.read('mvManuWaterLevel', ['elevation']);
        },
    },
}

const definition = {
    zigbeeModel: ['PLSYSZB1'],
    model: 'PLSYSZB1',
    vendor: 'MVDevZB',
    description: 'Irrigation Plant System',
    extend: [],
    fromZigbee: [fz.on_off, fromZbLocal.waterLevel, fromZbLocal.tankHeight, fromZbLocal.elevation],
    toZigbee: [tz.on_off, toZbLocal.tankHeight, toZbLocal.elevation],
    exposes: [e.switch().withLabel("Pump State"), e.numeric('waterLevel', ea.STATE).withDescription('Water Level Percentage').withUnit('%').withLabel("Water Level"), e.numeric('tankHeight', ea.ALL).withDescription('Tank Height in cm. The max height water can reach').withUnit('cm').withLabel("Max Water Level"), e.numeric('elevation', ea.ALL).withDescription('Sensor elevation from the maximum water surface').withUnit('cm').withLabel("Sensor Elevation")],
    configure: async (device, coordinatorEndpoint) => {
        const endpoint = device.getEndpoint(10);
        await reporting.bind(endpoint, coordinatorEndpoint, ['genOnOff', 'mvManuWaterLevel']);
        await endpoint.configureReporting('mvManuWaterLevel', [{attribute: 'measuredValue', minimumReportInterval: constants.repInterval.SECONDS_5, maximumReportInterval: constants.repInterval.HOUR, reportableChange: 1 }]);
    },
};

module.exports = definition;

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant