From c69f464232bd5760f37a71cca9db035a90f98ae7 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 23 Apr 2018 22:36:30 +0200 Subject: [PATCH] Support ZNCZ02LM. #4 --- lib/converters/zigbee2mqtt.js | 26 +++++++++++++++++++++++++- lib/devices.js | 7 +++++++ lib/homeassistant.js | 21 +++++++++++++++++++++ support/docgen.js | 6 ------ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/converters/zigbee2mqtt.js b/lib/converters/zigbee2mqtt.js index 44b4ee76f4..ab36a86833 100644 --- a/lib/converters/zigbee2mqtt.js +++ b/lib/converters/zigbee2mqtt.js @@ -27,6 +27,12 @@ const toPercentage = (value, min, max) => { return (normalised * 100).toFixed(2); } +const precisionRound = (number, precision) => { + const factor = Math.pow(10, precision); + return Math.round(number * factor) / factor; +} + + // Global variable store that can be used by devices. const store = {} @@ -185,10 +191,22 @@ const parsers = [ }; } }, + { + devices: ['ZNCZ02LM'], + cid: 'genOnOff', + type: 'attReport', + convert: (msg) => {return {state: msg.data.data['onOff'] === 1 ? '{"state": "ON"}': '{"state": "OFF"}'}} + }, + { + devices: ['ZNCZ02LM'], + cid: 'genAnalogInput', + type: 'attReport', + convert: (msg) => {return {power: precisionRound(msg.data.data['presentValue'], 2)}} + }, // Ignore parsers (these message dont need parsing). { - devices: ['WXKG11LM', 'MCCGQ11LM', 'MCCGQ01LM', 'WXKG01LM', 'LED1545G12', '7146060PH', 'LED1537R6'], + devices: ['WXKG11LM', 'MCCGQ11LM', 'MCCGQ01LM', 'WXKG01LM', 'LED1545G12', '7146060PH', 'LED1537R6', 'ZNCZ02LM'], cid: 'genOnOff', type: 'devChange', convert: () => null @@ -229,6 +247,12 @@ const parsers = [ type: 'devChange', convert: () => null }, + { + devices: ['ZNCZ02LM'], + cid: 'genAnalogInput', + type: 'devChange', + convert: () => null + }, ]; module.exports = parsers; diff --git a/lib/devices.js b/lib/devices.js index 52f02877f9..c3cb63e320 100644 --- a/lib/devices.js +++ b/lib/devices.js @@ -79,6 +79,13 @@ const devices = { supports: 'water leak true/false', homeassistant: [homeassistant.binary_sensor_water_leak] }, + 'lumi.plug': { + model: 'ZNCZ02LM', + description: 'Mi power plug ZigBee', + supports: 'on/off, power measurement', + vendor: 'Xiaomi', + homeassistant: [homeassistant.switch, homeassistant.sensor_power] + }, // IKEA 'TRADFRI bulb E27 WS opal 980lm': { diff --git a/lib/homeassistant.js b/lib/homeassistant.js index 7351adb312..a099dbe562 100644 --- a/lib/homeassistant.js +++ b/lib/homeassistant.js @@ -84,6 +84,15 @@ const homeassistant = { json_attributes: ['battery'], } }, + 'sensor_power': { + type: 'sensor', + object_id: 'power', + discovery_payload: { + unit_of_measurement: 'Watt', + icon: 'mdi:flash', + value_template: '{{ value_json.power }}', + } + }, // Light 'light_brightness_colortemp_xy': { @@ -107,6 +116,18 @@ const homeassistant = { command_topic: true } }, + + // Switch + 'switch': { + type: 'switch', + object_id: 'switch', + discovery_payload: { + payload_off: '{"state": "OFF"}', + payload_on: '{"state": "ON"}', + value_template: '{{ value_json.state }}', + command_topic: true + } + }, }; module.exports = homeassistant; diff --git a/support/docgen.js b/support/docgen.js index 2ad761d705..1ec3fb5077 100644 --- a/support/docgen.js +++ b/support/docgen.js @@ -22,12 +22,6 @@ const plannedToSupport = [ supports: '-', vendor: 'Xiaomi', }, - { - model: 'ZNCZ02LM', - description: 'Mi power plug ZigBee', - supports: '-', - vendor: 'Xiaomi', - }, { model: 'QBCZ11LM', description: 'Aqara wall socket',