Skip to content

Commit

Permalink
Refactor mqtt2zigbee to toZigbee and add convertes move devices. Koen…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed May 23, 2018
1 parent 29c5b87 commit 2e18e25
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Zigbee = require('./zigbee');
const logger = require('./util/logger');
const settings = require('./util/settings');
const deviceMapping = require('./devices');
const mqtt2zigbee = require('./converters/mqtt2zigbee');
const homeassistant = require('./homeassistant');
const debug = require('debug')('zigbee2mqtt:controller');

Expand Down Expand Up @@ -295,13 +294,14 @@ class Controller {

Object.keys(json).forEach((key) => {
// Find converter for this key.
const converter = mqtt2zigbee[key];
const converter = mappedModel.toZigbee.find((c) => c.key === key);

if (!converter) {
logger.error(`No converter available for '${key}' (${json[key]})`);
return;
}

const message = converter(json[key]);
const message = converter.convert(json[key]);
const callback = (error) => {
// Devices do not report when they go off, this ensures state (on/off) is always in sync.
if (!error && key.startsWith('state')) {
Expand Down
42 changes: 0 additions & 42 deletions lib/converters/mqtt2zigbee.js

This file was deleted.

54 changes: 54 additions & 0 deletions lib/converters/toZigbee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const converters = {
onoff: {
key: 'state',
convert: (value) => {
return {
cid: 'genOnOff',
cmd: value.toLowerCase(),
zclData: {},
};
},
},
light_brightness: {
key: 'brightness',
convert: (value) => {
return {
cid: 'genLevelCtrl',
cmd: 'moveToLevel',
zclData: {
level: value,
transtime: 0,
},
};
},
},
light_colortemp: {
key: 'color_temp',
convert: (value) => {
return {
cid: 'lightingColorCtrl',
cmd: 'moveToColorTemp',
zclData: {
colortemp: value,
transtime: 0,
},
};
},
},
light_color: {
key: 'color',
convert: (value) => {
return {
cid: 'lightingColorCtrl',
cmd: 'moveToColor',
zclData: {
colorx: value.x * 65535,
colory: value.y * 65535,
transtime: 0,
},
};
},
},
};

module.exports = converters;
28 changes: 28 additions & 0 deletions lib/devices.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const fz = require('./converters/fromZigbee');
const tz = require('./converters/toZigbee');

const LED1623G12 = {
model: 'LED1623G12',
vendor: 'IKEA',
description: 'TRADFRI LED bulb E27 1000 lumen, dimmable, opal white',
supports: 'on/off, brightness',
fromZigbee: [fz.light_brightness, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness],
};

const LED1536G5 = {
Expand All @@ -14,6 +16,7 @@ const LED1536G5 = {
description: 'TRADFRI LED bulb E12/E14 400 lumen, dimmable, white spectrum, opal white',
supports: 'on/off, brightness, color temperature',
fromZigbee: [fz.light_brightness, fz.light_color_colortemp, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness, tz.light_colortemp],
};

const devices = {
Expand All @@ -24,34 +27,39 @@ const devices = {
description: 'MiJia wireless switch',
supports: 'single, double, triple, quadruple, many and long click',
fromZigbee: [fz.xiaomi_battery_3v, fz.WXKG01LM_click, fz.ignore_onoff_change, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.sensor_switch.aq2': {
model: 'WXKG11LM',
vendor: 'Xiaomi',
description: 'Aqara wireless switch',
supports: 'single, double, triple, quadruple click',
fromZigbee: [fz.xiaomi_battery_3v, fz.WXKG11LM_click, fz.ignore_onoff_change, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.sensor_86sw1\u0000lu': {
model: 'WXKG03LM',
vendor: 'Xiaomi',
description: 'Aqara single key wireless wall switch',
supports: 'single click',
fromZigbee: [fz.xiaomi_battery_3v, fz.WXKG03LM_click],
toZigbee: [],
},
'lumi.sensor_86sw2\u0000Un': {
model: 'WXKG02LM',
vendor: 'Xiaomi',
description: 'Aqara double key wireless wall switch',
supports: 'left, right and both click',
fromZigbee: [fz.xiaomi_battery_3v, fz.WXKG02LM_click],
toZigbee: [],
},
'lumi.ctrl_neutral1': {
model: 'QBKG04LM',
vendor: 'Xiaomi',
description: 'Aqara single key wired wall switch',
supports: 'on/off',
fromZigbee: [fz.QBKG04LM_state, fz.ignore_onoff_change],
toZigbee: [tz.onoff],
ep: {'': 2},
},
'lumi.ctrl_neutral2': {
Expand All @@ -60,6 +68,7 @@ const devices = {
description: 'Aqara double key wired wall switch',
supports: 'l1 and l2 on/off',
fromZigbee: [fz.QBKG03LM_state, fz.ignore_onoff_change],
toZigbee: [tz.onoff],
ep: {'l1': 2, 'l2': 3},
},
'lumi.sens': {
Expand All @@ -68,6 +77,7 @@ const devices = {
description: 'MiJia temperature & humidity sensor ',
supports: 'temperature and humidity',
fromZigbee: [fz.xiaomi_battery_3v, fz.xiaomi_temperature, fz.xiaomi_humidity, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.weather': {
model: 'WSDCGQ11LM',
Expand All @@ -78,13 +88,15 @@ const devices = {
fz.xiaomi_battery_3v, fz.xiaomi_temperature, fz.xiaomi_humidity, fz.xiaomi_pressure, fz.ignore_basic_change,
fz.ignore_temperature_change, fz.ignore_humidity_change, fz.ignore_pressure_change,
],
toZigbee: [],
},
'lumi.sensor_motion': {
model: 'RTCGQ01LM',
vendor: 'Xiaomi',
description: 'MiJia human body movement sensor',
supports: 'occupancy',
fromZigbee: [fz.xiaomi_battery_3v, fz.xiaomi_occupancy, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.sensor_motion.aq2': {
model: 'RTCGQ11LM',
Expand All @@ -95,20 +107,23 @@ const devices = {
fz.xiaomi_battery_3v, fz.xiaomi_occupancy, fz.xiaomi_illuminance, fz.ignore_basic_change,
fz.ignore_illuminance_change, fz.ignore_occupancy_change,
],
toZigbee: [],
},
'lumi.sensor_magnet': {
model: 'MCCGQ01LM',
vendor: 'Xiaomi',
description: 'MiJia door & window contact sensor',
supports: 'contact',
fromZigbee: [fz.xiaomi_battery_3v, fz.xiaomi_contact, fz.ignore_onoff_change, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.sensor_magnet.aq2': {
model: 'MCCGQ11LM',
vendor: 'Xiaomi',
description: 'Aqara door & window contact sensor',
supports: 'contact',
fromZigbee: [fz.xiaomi_battery_3v, fz.xiaomi_contact, fz.ignore_onoff_change, fz.ignore_basic_change],
toZigbee: [],
},
'lumi.sensor_wleak.aq1': {
model: 'SJCGQ11LM',
Expand All @@ -119,6 +134,7 @@ const devices = {
fz.xiaomi_battery_3v, fz.SJCGQ11LM_water_leak_basic, fz.SJCGQ11LM_water_leak_iaszone,
fz.ignore_basic_change,
],
toZigbee: [],
},
'lumi.sensor_cube': {
model: 'MFKZQ01LM',
Expand All @@ -129,6 +145,7 @@ const devices = {
fz.xiaomi_battery_3v, fz.MFKZQ01LM_action_multistate, fz.MFKZQ01LM_action_analog,
fz.ignore_analog_change, fz.ignore_multistate_change,
],
toZigbee: [],
},
'lumi.plug': {
model: 'ZNCZ02LM',
Expand All @@ -139,20 +156,23 @@ const devices = {
fz.xiaomi_state, fz.xiaomi_power, fz.ZNCZ02LM_state, fz.ignore_onoff_change,
fz.ignore_basic_change, fz.ignore_analog_change,
],
toZigbee: [tz.onoff],
},
'lumi.ctrl_86plug': {
model: 'QBCZ11LM',
description: 'Aqara socket Zigbee',
supports: 'on/off, power measurement',
vendor: 'Xiaomi',
fromZigbee: [fz.xiaomi_state, fz.xiaomi_power, fz.ignore_onoff_change, fz.ignore_analog_change],
toZigbee: [tz.onoff],
},
'lumi.sensor_smoke': {
model: 'JTYJ-GD-01LM/BW',
description: 'MiJia Honeywell smoke detector',
supports: 'smoke',
vendor: 'Xiaomi',
fromZigbee: [fz.xiaomi_battery_3v, fz.JTYJGD01LMBW_smoke, fz.ignore_basic_change],
toZigbee: [],
},

// IKEA
Expand All @@ -162,6 +182,7 @@ const devices = {
description: 'TRADFRI LED bulb E27 980 lumen, dimmable, white spectrum, opal white',
supports: 'on/off, brightness, color temperature',
fromZigbee: [fz.light_brightness, fz.light_color_colortemp, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness, tz.light_colortemp],
},
// LED1623G12 uses 2 model IDs, see https://github.com/Koenkk/zigbee2mqtt/issues/21
'TRADFRI bulb E27 opal 1000lm': LED1623G12,
Expand All @@ -172,13 +193,15 @@ const devices = {
description: 'TRADFRI LED bulb GU10 400 lumen, dimmable, white spectrum',
supports: 'on/off, brightness, color temperature',
fromZigbee: [fz.light_brightness, fz.light_color_colortemp, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness, tz.light_colortemp],
},
'TRADFRI bulb GU10 W 400lm': {
model: 'LED1650R5',
vendor: 'IKEA',
description: 'TRADFRI LED bulb GU10 400 lumen, dimmable',
supports: 'on/off, brightness',
fromZigbee: [fz.light_brightness, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness],
},
// LED1536G5 has an E12 and E14 version.
'TRADFRI bulb E14 WS opal 400lm': LED1536G5,
Expand All @@ -189,6 +212,7 @@ const devices = {
description: 'TRADFRI LED bulb E26 1000 lumen, dimmable, opal white',
supports: 'on/off, brightness',
fromZigbee: [fz.light_brightness, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness],
},

// Philips
Expand All @@ -198,6 +222,7 @@ const devices = {
description: 'Hue Go',
supports: 'on/off, brightness, color temperature, color xy',
fromZigbee: [fz.light_brightness, fz.light_color_colortemp, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness, tz.light_colortemp, tz.light_color],
},

// Belkin
Expand All @@ -207,6 +232,7 @@ const devices = {
description: 'WeMo smart LED bulb',
supports: 'on/off, brightness',
fromZigbee: [fz.light_brightness, fz.ignore_onoff_change],
toZigbee: [tz.onoff, tz.light_brightness],
},

// EDP
Expand All @@ -216,6 +242,7 @@ const devices = {
description: 're:dy plug',
supports: 'on/off, power measurement',
fromZigbee: [fz.ignore_onoff_change, fz.EDP_power, fz.ignore_metering_change],
toZigbee: [tz.onoff],
report: [{
'cid': 'seMetering',
'attr': 'instantaneousDemand',
Expand All @@ -233,6 +260,7 @@ const devices = {
description: '[CC2530 router](http://ptvo.info/cc2530-based-zigbee-coordinator-and-router-112/)',
supports: 'state, description, type, rssi',
fromZigbee: [fz.CC2530ROUTER_state, fz.CC2530ROUTER_meta],
toZigbee: [],
},
};

Expand Down

0 comments on commit 2e18e25

Please sign in to comment.