zigbee2mqtt icon indicating copy to clipboard operation
zigbee2mqtt copied to clipboard

[New device support]: ZNQBKG38LM Xiaomi Aqara Z1 1 gang smart wall switch

Open jomud9 opened this issue 3 years ago • 1 comments

Link

https://www.aqara.com/cn/Canon_Smart_Wall_Switch_Z1_overview

Database entry

This is a wrong one. Will replace once got one

Comments

requested from various z1 1 gang owners, including @maodun96 who dropped me a message and nice comment from @nickgoh. I tried to draft one for z1 1 gang.

As I did not own one, please comment

External converter

//. Changlog 20231124 - first ver for ZNQBKG38LM
//. Continue work from nickgoh - https://github.com/Koenkk/zigbee2mqtt/issues/18135#issuecomment-1778487502
//. 
//. 1. still thinking this version of firmware only support
//.     - action: single (no double, triple, hold)
//.     - no multiple button pressed together (no both, all)
//. 2. as button arranged in "top to buttom" instead of "standard" left to right, I renamed as button_1 instead of left
//. 3. Need help on function to make these working
//.   - flip_indicator_light, power_outage_memory and click mode
//.   - Seems address got changed but I have no idea

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 reporting = require('zigbee-herdsman-converters/lib/reporting');
const extend = require('zigbee-herdsman-converters/lib/extend');
const ota = require('zigbee-herdsman-converters/lib/ota');


//. PLEASE READ: These are present from device/xiaomi.ts, only needed for external converter
const herdsman = require('zigbee-herdsman');
const e = exposes.presets;
const ea = exposes.access;

const manufacturerOptions = {
    xiaomi: {manufacturerCode: herdsman.Zcl.ManufacturerCode.LUMI_UNITED_TECH, disableDefaultResponse: true},
};

const { hasAlreadyProcessedMessage } = require('zigbee-herdsman-converters/lib/utils');

const preventReset = async (type, data, device) => { 
    if (
        // options.allow_reset ||
        type !== 'message' ||
        data.type !== 'attributeReport' ||
        data.cluster !== 'genBasic' ||
        !data.data[0xfff0] ||
        // eg: [0xaa, 0x10, 0x05, 0x41, 0x87, 0x01, 0x01, 0x10, 0x00]
        !data.data[0xFFF0].slice(0, 5).equals(Buffer.from([0xaa, 0x10, 0x05, 0x41, 0x87]))
    ) {
        return;
    }
    const options = {manufacturerCode: 0x115f};
    const payload = {[0xfff0]: {
        value: [0xaa, 0x10, 0x05, 0x41, 0x47, 0x01, 0x01, 0x10, 0x01],
        type: 0x41,
    }};
    await device.getEndpoint(1).write('genBasic', payload, options);
}; // PLEASE READ: THE END OF THE BLOCK THAT CAN BE DELELED after released

//. PLEASE READ: These should be update to FromZigbee.js 
const fzLocal = {
    xiaomi_multistate_action: {
        cluster: 'genMultistateInput',
        type: ['attributeReport'],
        convert: (model, msg, publish, options, meta) => {
            if (hasAlreadyProcessedMessage(msg, model)) return;
            let actionLookup = {0: 'hold', 1: 'single', 2: 'double', 3: 'triple', 255: 'release'};
            if (model.model === 'WXKG12LM') {
                actionLookup = {...actionLookup, 16: 'hold', 17: 'release', 18: 'shake'};
            }
            if (['WXKG02LM_rev2', 'WXKG07LM', 'WXKG15LM', 'WXKG17LM'].includes(model.model)) {
                buttonLookup = {1: 'left', 2: 'right', 3: 'both'};
            }
            if (['ZNQBKG38LM'].includes(model.model)) {
                buttonLookup = {1: 'button_1'};
            }
			if (['ZNQBKG39LM'].includes(model.model)) {
                buttonLookup = {1: 'button_1', 2: 'button_2'};
            }
            if (['ZNQBKG40LM'].includes(model.model)) {
                buttonLookup = {1: 'button_1', 2: 'button_2', 3: 'button_3'};
            }
            if (['ZNQBKG41LM'].includes(model.model)) {
                buttonLookup = {1: 'button_1', 2: 'button_2', 3: 'button_3', 4: 'button_4'};
            }
            if (['QBKG12LM', 'QBKG24LM'].includes(model.model)) buttonLookup = {5: 'left', 6: 'right', 7: 'both'};
            if (['QBKG39LM', 'QBKG41LM', 'WS-EUK02', 'WS-EUK04', 'QBKG20LM', 'QBKG28LM', 'QBKG31LM'].includes(model.model)) {
                buttonLookup = {41: 'left', 42: 'right', 51: 'both'};
            }
            if (['QBKG25LM', 'QBKG26LM', 'QBKG29LM', 'QBKG34LM', 'ZNQBKG31LM', 'ZNQBKG26LM'].includes(model.model)) {
                buttonLookup = {
                    41: 'left', 42: 'center', 43: 'right',
                    51: 'left_center', 52: 'left_right', 53: 'center_right',
                    61: 'all',
                };
            }
            if (['WS-USC02', 'WS-USC04'].includes(model.model)) {
                buttonLookup = {41: 'top', 42: 'bottom', 51: 'both'};
            }

            const action = actionLookup[msg.data['presentValue']];
            if (buttonLookup) {
                const button = buttonLookup[msg.endpoint.ID];
                if (button) {
                    return {action: `${action}_${button}`};
                }
            } else {
                return {action};
            }
        },
    },
};   // PLEASE READ: THE END OF THE BLOCK THAT CAN BE DELETE WHEN MERGING WITH FromZigbee.js


// PLEASE READ: These should be update to ToZigbee.js 
const tzLocal = {
    xiaomi_switch_power_outage_memory: {
        key: ['power_outage_memory'],
        convertSet: async (entity, key, value, meta) => {
            if (['SP-EUC01', 'ZNCZ04LM', 'ZNCZ12LM', 'ZNCZ15LM', 'QBCZ14LM', 'QBCZ15LM', 'SSM-U01', 'SSM-U02', 'DLKZMK11LM', 'DLKZMK12LM',
                'WS-EUK01', 'WS-EUK02', 'WS-EUK03', 'WS-EUK04', 'QBKG19LM', 'QBKG20LM', 'QBKG25LM', 'QBKG26LM', 'QBKG28LM', 'QBKG29LM',
                'QBKG31LM', 'QBKG34LM', 'QBKG38LM', 'QBKG39LM', 'QBKG40LM', 'QBKG41LM', 'ZNDDMK11LM', 'ZNLDP13LM', 'ZNQBKG31LM',
                'WS-USC02', 'WS-USC03', 'WS-USC04', 'ZNQBKG24LM', 'ZNQBKG24LM', 
            ].includes(meta.mapped.model)) {
                await entity.write('aqaraOpple', {0x0201: {value: value ? 1 : 0, type: 0x10}}, manufacturerOptions.xiaomi);
            } else if (['ZNQBKG38LM','ZNQBKG39LM','ZNQBKG40LM','ZNQBKG41LM'].includes(meta.mapped.model)) {
                 // Support existing syntax of a nested object just for the state field. Though it's quite silly IMO.
                const targetValue = value.hasOwnProperty('state') ? value.state : value;

                const lookupState = {on: 0x01, electric_appliances_on: 0x00,electric_appliances_off: 0x02,Inverted: 0x03};
                await entity.write('aqaraOpple', {0x0517: {value: lookupState[targetValue], type: 0x20}}, manufacturerOptions.xiaomi);
        
            } else if (['ZNCZ02LM', 'QBCZ11LM', 'LLKZMK11LM'].includes(meta.mapped.model)) {
                const payload = value ?
                    [[0xaa, 0x80, 0x05, 0xd1, 0x47, 0x07, 0x01, 0x10, 0x01], [0xaa, 0x80, 0x03, 0xd3, 0x07, 0x08, 0x01]] :
                    [[0xaa, 0x80, 0x05, 0xd1, 0x47, 0x09, 0x01, 0x10, 0x00], [0xaa, 0x80, 0x03, 0xd3, 0x07, 0x0a, 0x01]];

                await entity.write('genBasic', {0xFFF0: {value: payload[0], type: 0x41}}, manufacturerOptions.xiaomi);
                await entity.write('genBasic', {0xFFF0: {value: payload[1], type: 0x41}}, manufacturerOptions.xiaomi);
            } else if (['ZNCZ11LM'].includes(meta.mapped.model)) {
                const payload = value ?
                    [0xaa, 0x80, 0x05, 0xd1, 0x47, 0x00, 0x01, 0x10, 0x01] :
                    [0xaa, 0x80, 0x05, 0xd1, 0x47, 0x01, 0x01, 0x10, 0x00];

                await entity.write('genBasic', {0xFFF0: {value: payload, type: 0x41}}, manufacturerOptions.xiaomi);
            } else {
                throw new Error('Not supported');
            }
            return {state: {power_outage_memory: value}};
        },
        convertGet: async (entity, key, meta) => {
            if (['SP-EUC01', 'ZNCZ04LM', 'ZNCZ12LM', 'ZNCZ15LM', 'QBCZ14LM', 'QBCZ15LM', 'SSM-U01', 'SSM-U02', 'DLKZMK11LM', 'DLKZMK12LM',
                'WS-EUK01', 'WS-EUK02', 'WS-EUK03', 'WS-EUK04', 'QBKG19LM', 'QBKG20LM', 'QBKG25LM', 'QBKG26LM', 'QBKG28LM', 'QBKG29LM',
                'QBKG31LM', 'QBKG34LM', 'QBKG38LM', 'QBKG39LM', 'QBKG40LM', 'QBKG41LM', 'ZNDDMK11LM', 'ZNLDP13LM', 'ZNQBKG31LM',
                'WS-USC02', 'WS-USC03', 'WS-USC04', 'ZNQBKG24LM',
            ].includes(meta.mapped.model)) {
                await entity.read('aqaraOpple', [0x0201]);
            } else if (['ZNQBKG38LM','ZNQBKG39LM','ZNQBKG40LM','ZNQBKG41LM'].includes(meta.mapped.model)) {
                await entity.read('aqaraOpple', [0x0517]);

            } else if (['ZNCZ02LM', 'QBCZ11LM', 'ZNCZ11LM'].includes(meta.mapped.model)) {
                await entity.read('aqaraOpple', [0xFFF0]);
            } else {
                throw new Error('Not supported');
            }
        },
    },
    xiaomi_switch_lock_relay_opple: {
        key: ['lock_relay'],

        convertSet: async (entity, key, value, meta) => {
            const lookup = {'OFF': 0x00, 'ON': 0x01};
            await entity.write('aqaraOpple', {0x0285: {value: lookup[value], type: 0x20}}, manufacturerOptions.xiaomi);
            return {state: {lock_relay: value}};
        },
        convertGet: async (entity, key, meta) => {
            await entity.read('aqaraOpple', [0x0285], manufacturerOptions.xiaomi);
        },
    },
    

    xiaomi_switch_click_mode: {
        key: ['click_mode'],
        convertSet: async (entity, key, value, meta) => {
            if (['ZNQBKG38LM','ZNQBKG39LM','ZNQBKG40LM','ZNQBKG41LM'].includes(meta.mapped.model)) {
                const lookupState = {'fast': 0x01, 'multi': 0x02};
                await entity.write('aqaraOpple', {0x0286: {value: lookupState[value], type: 0x20}}, manufacturerOptions.xiaomi);
            } else{
                const lookupState = {'fast': 0x1, 'multi': 0x02};
                await entity.write('aqaraOpple', {0x0125: {value: lookupState[value], type: 0x20}}, manufacturerOptions.xiaomi);
            }
           
            return {state: {click_mode: value}};
        },
        convertGet: async (entity, key, meta) => {
            await entity.read('aqaraOpple', [0x125], manufacturerOptions.xiaomi);
        },
    },
    
}; // PLEASE READ: THE END OF THE BLOCK THAT CAN BE DELETE WHEN MERGING WITH ToZigbee.js



const definition = {
    zigbeeModel: ['lumi.switch.acn048'], // The model ID from: Device with modelID 'lumi.sens' is not supported.
    model: 'ZNQBKG38LM', // Vendor model number, look on the device for a model number
    vendor: 'Xiaomi', // Vendor of the device (only used for documentation and startup logging)
    description: 'Aqara Z1 1 gang smart wall switch', // Description of the device, copy from vendor site. (only used for documentation and startup logging)
    fromZigbee: [fz.on_off, fzLocal.xiaomi_multistate_action, fz.aqara_opple, fz.xiaomi_power],
    toZigbee: [tz.on_off, tz.xiaomi_switch_operation_mode_opple, tzLocal.xiaomi_switch_power_outage_memory,tzLocal.xiaomi_switch_lock_relay_opple,tzLocal.xiaomi_switch_click_mode],
    endpoint: (device) => {
        return {'button_1': 1};
    },
    meta: {multiEndpoint: true},
    exposes: [
        e.power(), e.voltage(), e.device_temperature(),
        e.switch().withEndpoint('button_1'), 
        exposes.enum('power_outage_memory', ea.ALL, ['on', 'electric_appliances_on', 'electric_appliances_off', 'Inverted'])
                .withDescription('Power Outage Memory'),
        exposes.enum('operation_mode', ea.ALL, ['control_relay', 'decoupled'])
                .withDescription('Decoupled mode for button 1 button')
                .withEndpoint('button_1'),
                
        exposes.enum('lock_relay', ea.ALL, ['On', 'Off'])
                .withDescription('Lock Relay mode for button 1 button')
                .withEndpoint('button_1'),
                
        exposes.enum('click_mode', ea.ALL, ['fast', 'multi'])
    ],
    onEvent: preventReset,
    configure: async (device, coordinatorEndpoint, logger) => {
        await device.getEndpoint(1).write('aqaraOpple', {'mode': 1}, {manufacturerCode: 0x115f, disableResponse: true});
//       await device.getEndpoint(1).write('aqaraOpple', {0x0125: {value: 0x02, type: 0x20}}, {manufacturerCode: 0x115f});
    },

};

module.exports = definition;

Supported color modes

No response

Color temperature range

No response

jomud9 avatar Nov 24 '23 08:11 jomud9

Duplicated / Incompleted submit

jomud9 avatar Nov 24 '23 08:11 jomud9