Skip to content

Commit

Permalink
Start on group support. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Dec 20, 2018
1 parent 9191788 commit 3254be3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const ExtensionDeviceConfigure = require('./extension/deviceConfigure');
const ExtensionDeviceReceive = require('./extension/deviceReceive');
const ExtensionMarkOnlineXiaomi = require('./extension/markOnlineXiaomi');
const ExtensionBridgeConfig = require('./extension/bridgeConfig');
const ExtensionGroups = require('./extension/groups');

class Controller {
constructor() {
Expand All @@ -38,6 +39,7 @@ class Controller {
new ExtensionRouterPollXiaomi(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
new ExtensionMarkOnlineXiaomi(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
new ExtensionBridgeConfig(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
new ExtensionGroups(this.zigbee, this.mqtt, this.state, this.publishDeviceState),
];

if (settings.get().homeassistant) {
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/devicePublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const Queue = require('queue');
const logger = require('../util/logger');

const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/.+/(set|get)$`);
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/((?!group).+)/(set|get)$`);
const postfixes = ['left', 'right', 'center', 'bottom_left', 'bottom_right', 'top_left', 'top_right'];
const maxDepth = 20;

Expand Down
86 changes: 86 additions & 0 deletions lib/extension/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const settings = require('../util/settings');
const logger = require('../util/logger');

const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/group/.+/set$`);

class Groups {
constructor(zigbee, mqtt, state, publishDeviceState) {
this.zigbee = zigbee;
this.mqtt = mqtt;
this.state = state;
this.publishDeviceState = publishDeviceState;
}

onMQTTConnected() {
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/group/+/set`);
}

onZigbeeStarted() {
const groups = settings.get().groups;

Object.keys(groups).forEach((name) => {
const group = groups[name];

const callback = (device, error, rsp) => {
if (!error) {
logger.info(`Added '${device}' to group '${name}' (${group.ID})`);
} else {
logger.error(`Failed to add '${device}' to group '${name}' (${group.ID})`);
}
};

group.devices.forEach((device) => {
const ieeeAddr = settings.getIeeeAddrByFriendlyName(device) || device;
this.zigbee.publish(
ieeeAddr,
'genGroups',
'add',
'functional',
{groupid: group.ID, groupname: name},
null,
null,
(error, rsp) => callback(device, error, rsp),
);
});
});
}

parseTopic(topic) {
if (!topic.match(topicRegex)) {
return null;
}

// Remove base from topic
topic = topic.replace(`${settings.get().mqtt.base_topic}/group/`, '');

// Parse type from topic
const type = topic.substr(topic.lastIndexOf('/') + 1, topic.length);

// Remove type from topic
topic = topic.replace(`/${type}`, '');

const name = topic;

return {type: type, name: name};
}

onMQTTMessage(topic, message) {
topic = this.parseTopic(topic);

if (!topic) {
return false;
}

if (!settings.get().groups.hasOwnProperty(topic.name)) {
logger.error(`Group '${topic.name}' doesn't exist`);
return false;
}

const groupID = settings.get().groups[topic.name].ID;
logger.info(groupID, message.toString());

return true;
}
}

module.exports = Groups;
1 change: 1 addition & 0 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaults = {
mqtt: {
include_device_information: false,
},
groups: {},
advanced: {
log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'),
log_level: process.env.DEBUG ? 'debug' : 'info',
Expand Down
7 changes: 6 additions & 1 deletion lib/zigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const shepherdSettings = {
},
};

const defaultCfg = {
manufSpec: 0,
disDefaultRsp: 0,
};

logger.debug(`Using zigbee-shepherd with settings: '${JSON.stringify(shepherdSettings)}'`);

class Zigbee {
Expand Down Expand Up @@ -187,7 +192,7 @@ class Zigbee {
return this.shepherd.find(device.ieeeAddr, 1);
}

publish(ieeAddr, cid, cmd, cmdType, zclData, cfg, ep, callback) {
publish(ieeAddr, cid, cmd, cmdType, zclData, cfg=defaultCfg, ep, callback) {
const device = this.findDevice(ieeAddr, ep);
if (!device) {
logger.error(`Zigbee cannot publish message to device because '${ieeAddr}' not known by zigbee-shepherd`);
Expand Down

0 comments on commit 3254be3

Please sign in to comment.