Skip to content

Commit

Permalink
Move converters to devices.js. Koenkk#45
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed May 23, 2018
1 parent 81774af commit dddb190
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 109 deletions.
5 changes: 1 addition & 4 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 zigbee2mqtt = require('./converters/zigbee2mqtt');
const mqtt2zigbee = require('./converters/mqtt2zigbee');
const homeassistant = require('./homeassistant');
const debug = require('debug')('zigbee2mqtt:controller');
Expand Down Expand Up @@ -217,9 +216,7 @@ class Controller {

// Find a conveter for this message.
const cid = message.data.cid;
const converters = zigbee2mqtt.filter((c) =>
c.devices.includes(mappedModel.model) && c.cid === cid && c.type === message.type
);
const converters = mappedModel.fromZigbee.filter((c) => c.cid === cid && c.type === message.type);

if (!converters.length) {
logger.warn(
Expand Down
131 changes: 38 additions & 93 deletions lib/converters/zigbee2mqtt.js → lib/converters/fromZigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ const precisionRound = (number, precision) => {
// Global variable store that can be used by devices.
const store = {};

const parsers = [
{
devices: [
'WXKG01LM', 'RTCGQ01LM', 'WSDCGQ01LM', 'MCCGQ01LM', 'WXKG11LM', 'MCCGQ11LM', 'RTCGQ11LM', 'WSDCGQ11LM',
'SJCGQ11LM', 'MFKZQ01LM', 'JTYJ-GD-01LM/BW', 'WXKG02LM', 'WXKG03LM',
],
const parsers = {
xiaomi_battery_3v: {
cid: 'genBasic',
type: 'attReport',
convert: (msg) => {
Expand All @@ -65,8 +61,7 @@ const parsers = [
}
},
},
{
devices: ['WXKG01LM'],
WXKG01LM_click: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg, publish) => {
Expand All @@ -92,16 +87,14 @@ const parsers = [
}
},
},
{
devices: ['WSDCGQ01LM', 'WSDCGQ11LM'],
xiaomi_temperature: {
cid: 'msTemperatureMeasurement',
type: 'attReport',
convert: (msg) => {
return {temperature: parseFloat(msg.data.data['measuredValue']) / 100.0};
},
},
{
devices: ['MFKZQ01LM'],
MFKZQ01LM_action_multistate: {
cid: 'genMultistateInput',
type: 'attReport',
convert: (msg) => {
Expand Down Expand Up @@ -139,8 +132,7 @@ const parsers = [
return action ? {'action': action} : null;
},
},
{
devices: ['MFKZQ01LM'],
MFKZQ01LM_action_analog: {
cid: 'genAnalogInput',
type: 'attReport',
convert: (msg) => {
Expand All @@ -152,16 +144,14 @@ const parsers = [
return {action: value < 0 ? 'rotate_left' : 'rotate_right'};
},
},
{
devices: ['WSDCGQ01LM', 'WSDCGQ11LM'],
xiaomi_humidity: {
cid: 'msRelativeHumidity',
type: 'attReport',
convert: (msg) => {
return {humidity: parseFloat(msg.data.data['measuredValue']) / 100.0};
},
},
{
devices: ['RTCGQ01LM', 'RTCGQ11LM'],
xiaomi_occupancy: {
cid: 'msOccupancySensing',
type: 'attReport',
convert: (msg, publish, options) => {
Expand All @@ -183,27 +173,21 @@ const parsers = [
return {occupancy: true};
},
},
{
devices: ['MCCGQ01LM', 'MCCGQ11LM'],
xiaomi_contact: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
return {contact: msg.data.data['onOff'] === 0};
},
},
{
devices: [
'LED1545G12', '7146060PH', 'LED1537R6', 'LED1623G12', 'LED1650R5', 'LED1536G5', 'F7C033',
'LED1622G12',
],
light_brightness: {
cid: 'genLevelCtrl',
type: 'devChange',
convert: (msg) => {
return {brightness: msg.data.data['currentLevel']};
},
},
{
devices: ['LED1545G12', '7146060PH', 'LED1537R6', 'LED1536G5'],
light_color_colortemp: {
cid: 'lightingColorCtrl',
type: 'devChange',
convert: (msg) => {
Expand All @@ -223,8 +207,7 @@ const parsers = [
return result;
},
},
{
devices: ['WXKG11LM'],
WXKG11LM_click: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
Expand All @@ -242,72 +225,63 @@ const parsers = [
}
},
},
{
devices: ['RTCGQ11LM'],
xiaomi_illuminance: {
cid: 'msIlluminanceMeasurement',
type: 'attReport',
convert: (msg) => {
return {illuminance: msg.data.data['measuredValue']};
},
},
{
devices: ['WSDCGQ11LM'],
xiaomi_pressure: {
cid: 'msPressureMeasurement',
type: 'attReport',
convert: (msg) => {
return {pressure: msg.data.data['measuredValue']};
},
},
{
devices: ['WXKG02LM'],
WXKG02LM_click: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
return {click: WXKG02LM[msg.endpoints[0].epId]};
},
},
{
devices: ['WXKG03LM'],
WXKG03LM_click: {
cid: 'genOnOff',
type: 'attReport',
convert: () => {
return {click: 'single'};
},
},
{
devices: ['SJCGQ11LM'],
SJCGQ11LM_water_leak_basic: {
cid: 'genBasic',
type: 'attReport',
convert: (msg) => {
return {water_leak: msg.data.data['65281']['100'] === 1};
},
},
{
devices: ['SJCGQ11LM'],
SJCGQ11LM_water_leak_iaszone: {
cid: 'ssIasZone',
type: 'statusChange',
convert: (msg) => {
return {water_leak: msg.data.zoneStatus === 1};
},
},
{
devices: ['ZNCZ02LM', 'QBCZ11LM'],
xiaomi_state: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
return {state: msg.data.data['onOff'] === 1 ? 'ON' : 'OFF'};
},
},
{
devices: ['ZNCZ02LM', 'QBCZ11LM'],
xiaomi_power: {
cid: 'genAnalogInput',
type: 'attReport',
convert: (msg) => {
return {power: precisionRound(msg.data.data['presentValue'], 2)};
},
},
{
devices: ['ZNCZ02LM'],
ZNCZ02LM_state: {
cid: 'genBasic',
type: 'attReport',
convert: (msg) => {
Expand All @@ -323,8 +297,7 @@ const parsers = [
}
},
},
{
devices: ['QBKG04LM'],
QBKG04LM_state: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
Expand All @@ -333,8 +306,7 @@ const parsers = [
}
},
},
{
devices: ['QBKG03LM'],
QBKG03LM_state: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
Expand All @@ -346,32 +318,28 @@ const parsers = [
}
},
},
{
devices: ['JTYJ-GD-01LM/BW'],
JTYJGD01LMBW_smoke: {
cid: 'ssIasZone',
type: 'statusChange',
convert: (msg) => {
return {smoke: msg.data.zoneStatus === 1};
},
},
{
devices: ['PLUG EDP RE:DY'],
EDP_power: {
cid: 'seMetering',
type: 'attReport',
convert: (msg) => {
return {power: precisionRound(msg.data.data['instantaneousDemand'], 2)};
},
},
{
devices: ['CC2530.ROUTER'],
CC2530ROUTER_state: {
cid: 'genOnOff',
type: 'attReport',
convert: (msg) => {
return {state: msg.data.data['onOff'] === 1};
},
},
{
devices: ['CC2530.ROUTER'],
CC2530ROUTER_meta: {
cid: 'genBinaryValue',
type: 'attReport',
convert: (msg) => {
Expand All @@ -385,79 +353,56 @@ const parsers = [
},

// Ignore parsers (these message dont need parsing).
{
devices: [
'WXKG11LM', 'MCCGQ11LM', 'MCCGQ01LM', 'WXKG01LM', 'LED1545G12', '7146060PH', 'LED1537R6', 'ZNCZ02LM',
'QBCZ11LM', 'QBKG04LM', 'QBKG03LM', 'LED1623G12', 'LED1650R5', 'LED1536G5', 'F7C033', 'LED1622G12',
'PLUG EDP RE:DY',
],
ignore_onoff_change: {
cid: 'genOnOff',
type: 'devChange',
convert: () => null,
},
{
devices: [
'WXKG11LM', 'MCCGQ11LM', 'RTCGQ11LM', 'WSDCGQ11LM', 'SJCGQ11LM', 'MCCGQ01LM', 'RTCGQ01LM', 'WXKG01LM',
'WSDCGQ01LM', 'JTYJ-GD-01LM/BW', 'ZNCZ02LM',
],
ignore_basic_change: {
cid: 'genBasic',
type: 'devChange',
convert: () => null,
},
{
devices: ['RTCGQ11LM'],
ignore_illuminance_change: {
cid: 'msIlluminanceMeasurement',
type: 'devChange',
convert: () => null,
},
{
devices: ['RTCGQ11LM'],
ignore_occupancy_change: {
cid: 'msOccupancySensing',
type: 'devChange',
convert: () => null,
},
{
devices: ['WSDCGQ11LM'],
ignore_temperature_change: {
cid: 'msTemperatureMeasurement',
type: 'devChange',
convert: () => null,
},
{
devices: ['WSDCGQ11LM'],
ignore_humidity_change: {
cid: 'msRelativeHumidity',
type: 'devChange',
convert: () => null,
},
{
devices: ['WSDCGQ11LM'],
ignore_pressure_change: {
cid: 'msPressureMeasurement',
type: 'devChange',
convert: () => null,
},
{
devices: ['ZNCZ02LM', 'QBCZ11LM'],
ignore_analog_change: {
cid: 'genAnalogInput',
type: 'devChange',
convert: () => null,
},
{
devices: ['MFKZQ01LM'],
ignore_multistate_change: {
cid: 'genMultistateInput',
type: 'devChange',
convert: () => null,
},
{
devices: ['MFKZQ01LM'],
cid: 'genAnalogInput',
type: 'devChange',
convert: () => null,
},
{
devices: ['PLUG EDP RE:DY'],
ignore_metering_change: {
cid: 'seMetering',
type: 'devChange',
convert: () => null,
},
];
};

module.exports = parsers;
Loading

0 comments on commit dddb190

Please sign in to comment.