Skip to content

Commit

Permalink
Update zigbee-herdsman to 0.7.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Sep 23, 2019
1 parent 63acd4a commit 41e68c8
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 212 deletions.
12 changes: 6 additions & 6 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class Controller {
}

// Log zigbee clients on startup
const devices = await this.zigbee.getClients();
const devices = this.zigbee.getClients();
logger.info(`Currently ${devices.length} devices are joined:`);
for (const device of devices) {
const entity = await this.zigbee.resolveEntity(device);
const entity = this.zigbee.resolveEntity(device);
logger.info(
(entity.settings ? entity.settings.friendlyName : entity.device.ieeeAddr) +
` (${entity.device.ieeeAddr}): ` +
Expand Down Expand Up @@ -121,7 +121,7 @@ class Controller {
await this.mqtt.connect();

// Send all cached states.
for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
if (this.state.exists(device.ieeeAddr)) {
this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr));
}
Expand Down Expand Up @@ -154,7 +154,7 @@ class Controller {
}

async onZigbeeEvent(type, data) {
const entity = await this.zigbee.resolveEntity(data.device || data.ieeeAddr);
const entity = this.zigbee.resolveEntity(data.device || data.ieeeAddr);
if (!entity.settings && data.device) {
// Only deviceLeave doesn't have a device (not interesting to add to settings)
entity.settings = settings.addDevice(data.device.ieeeAddr);
Expand Down Expand Up @@ -223,7 +223,7 @@ class Controller {
}

async publishEntityState(IDorName, payload) {
const entity = await this.zigbee.resolveEntity(IDorName);
const entity = this.zigbee.resolveEntity(IDorName);
if (!entity || !entity.settings) {
logger.error(`'${IDorName}' does not exist, skipping publish`);
return;
Expand All @@ -247,7 +247,7 @@ class Controller {
};

if (entity.type === 'device' && settings.get().mqtt.include_device_information) {
const device = await this.zigbee.getDevice({ieeeAddr: entity.device.ieeeAddr});
const device = this.zigbee.getDeviceByIeeeAddr(entity.device.ieeeAddr);
const attributes = [
'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource',
'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID',
Expand Down
8 changes: 4 additions & 4 deletions lib/extension/bridgeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class BridgeConfig extends BaseExtension {
}

async devices(topic, message) {
const devices = (await this.zigbee.getDevices({})).map((device) => {
const devices = this.zigbee.getDevices().map((device) => {
const payload = {
ieeeAddr: device.ieeeAddr,
type: device.type,
Expand Down Expand Up @@ -196,10 +196,10 @@ class BridgeConfig extends BaseExtension {
}
}

async addGroup(topic, message) {
addGroup(topic, message) {
const name = message;
const group = settings.addGroup(name);
await this.zigbee.createGroup(group.ID);
this.zigbee.createGroup(group.ID);
logger.info(`Added group '${name}'`);
}

Expand All @@ -218,7 +218,7 @@ class BridgeConfig extends BaseExtension {
}

async removeOrBan(ban, message) {
const entity = await this.zigbee.resolveEntity(message);
const entity = this.zigbee.resolveEntity(message);

const cleanup = () => {
// Remove from configuration.yaml
Expand Down
13 changes: 6 additions & 7 deletions lib/extension/deviceAvailability.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ class DeviceAvailability extends BaseExtension {
return result;
}

async getAllPingableDevices() {
return (await this.zigbee.getClients()).filter((d) => this.isPingable(d));
getAllPingableDevices() {
return this.zigbee.getClients().filter((d) => this.isPingable(d));
}

async onMQTTConnected() {
onMQTTConnected() {
// As some devices are not checked for availability (e.g. battery powered devices)
// we mark all device as online by default.
(await this.zigbee.getClients())
.forEach((device) => this.publishAvailability(device, true));
this.zigbee.getClients().forEach((device) => this.publishAvailability(device, true));

// Start timers for all devices
(await this.getAllPingableDevices()).forEach((device) => this.setTimer(device));
this.getAllPingableDevices().forEach((device) => this.setTimer(device));
}

async handleInterval(device) {
Expand Down Expand Up @@ -91,7 +90,7 @@ class DeviceAvailability extends BaseExtension {
clearTimeout(timer);
}

(await this.zigbee.getClients()).forEach((device) => this.publishAvailability(device, false));
this.zigbee.getClients().forEach((device) => this.publishAvailability(device, false));
}

async onReconnect(device) {
Expand Down
4 changes: 2 additions & 2 deletions lib/extension/deviceBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class DeviceBind extends BaseExtension {
const targetKey = message;

// Find source; can only be a device and target
const source = await this.zigbee.resolveEntity(sourceKey);
const source = this.zigbee.resolveEntity(sourceKey);
assert(source != null && source.type === 'device', 'Source undefined or not a device');
const target = await this.zigbee.resolveEntity(targetKey);
const target = this.zigbee.resolveEntity(targetKey);
assert(target != null, 'Target is unknown');

const sourceName = source.settings.friendlyName;
Expand Down
5 changes: 2 additions & 3 deletions lib/extension/deviceConfigure.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ class DeviceConfigure extends BaseExtension {
}

async onZigbeeStarted() {
this.coordinatorEndpoint = (await this.zigbee.getDevice({type: 'Coordinator'})).endpoints[0];
this.coordinatorEndpoint = this.zigbee.getDevicesByType('Coordinator')[0].endpoints[0];

const devices = await this.zigbee.getClients();
for (const device of devices) {
for (const device of this.zigbee.getClients()) {
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
const settingsDevice = settings.getDevice(device.ieeeAddr);
if (this.shouldConfigure(device, mappedDevice)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/extension/deviceEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');

class DeviceEvent extends BaseExtension {
async onZigbeeStarted() {
for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
this.callOnEvent(device, 'start', {});
}
}
Expand All @@ -15,7 +15,7 @@ class DeviceEvent extends BaseExtension {
}

async stop() {
for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
this.callOnEvent(device, 'stop', {});
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/deviceGroupMembership.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DeviceGroupMembership extends BaseExtension {
return null;
}

const entity = await this.zigbee.resolveEntity(match[1]);
const entity = this.zigbee.resolveEntity(match[1]);
if (!entity || entity.type !== 'device') {
logger.error(`Device '${match[1]}' does not exist`);
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/deviceReceive.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DeviceReceive extends BaseExtension {
}

async onZigbeeStarted() {
this.coordinator = await this.zigbee.getDevice({type: 'Coordinator'});
this.coordinator = this.zigbee.getDevicesByType('Coordinator')[0];
}

publishDebounce(ieeeAddr, payload, time) {
Expand Down
4 changes: 2 additions & 2 deletions lib/extension/deviceReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class DeviceReport extends BaseExtension {
}

async onZigbeeStarted() {
this.coordinatorEndpoint = (await this.zigbee.getDevice({type: 'Coordinator'})).endpoints[0];
this.coordinatorEndpoint = this.zigbee.getDevicesByType('Coordinator')[0].endpoints[0];

for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
const mappedDevice = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
if (this.shouldSetupReporting(mappedDevice, device, null)) {
this.setupReporting(device);
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/entityPublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class EntityPublish extends BaseExtension {
}

const entityName = `${topic.ID}` + (topic.postfix ? `/${topic.postfix}` : '');
const entity = await this.zigbee.resolveEntity(entityName);
const entity = this.zigbee.resolveEntity(entityName);

if (!entity) {
this.mqtt.log('entity_not_found', {friendly_name: entityName});
Expand Down
19 changes: 9 additions & 10 deletions lib/extension/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ class Groups extends BaseExtension {

async syncGroupsWithSettings() {
const settingsGroups = settings.getGroups();
const zigbeeGroups = await this.zigbee.getGroups({});
const zigbeeGroups = this.zigbee.getGroups();
for (const settingGroup of settingsGroups) {
const groupID = settingGroup.ID;
const zigbeeGroup = zigbeeGroups.find((g) => g.groupID === groupID) ||
(await this.zigbee.createGroup(groupID));
const settingsEntity = (await Promise.all(settingGroup.devices.map(async (d) => {
const entity = await this.zigbee.resolveEntity(d);
const zigbeeGroup = zigbeeGroups.find((g) => g.groupID === groupID) || this.zigbee.createGroup(groupID);
const settingsEntity = settingGroup.devices.map((d) => {
const entity = this.zigbee.resolveEntity(d);
if (!entity) logger.error(`Cannot find '${d}' of group '${settingGroup.friendlyName}'`);
return entity;
}))).filter((e) => e != null);
}).filter((e) => e != null);

// In settings but not in zigbee
for (const entity of settingsEntity) {
Expand Down Expand Up @@ -77,12 +76,12 @@ class Groups extends BaseExtension {
});

if (Object.keys(payload).length) {
const entity = await this.zigbee.resolveEntity(data.ID);
const entity = this.zigbee.resolveEntity(data.ID);
if (entity.type !== 'device') {
return;
}

const zigbeeGroups = await this.zigbee.getGroups({});
const zigbeeGroups = this.zigbee.getGroups();
for (const zigbeeGroup of zigbeeGroups) {
const settingsGroup = settings.getGroup(zigbeeGroup.groupID);
if (settingsGroup && settingsGroup.optimistic && zigbeeGroup.hasMember(entity.endpoint)) {
Expand All @@ -97,7 +96,7 @@ class Groups extends BaseExtension {
let group;
const topicMatch = topic.match(topicRegex);
if (topicMatch) {
group = await this.zigbee.resolveEntity(topicMatch[1]);
group = this.zigbee.resolveEntity(topicMatch[1]);
type = topicMatch[2];

if (!group || group.type !== 'group') {
Expand All @@ -110,7 +109,7 @@ class Groups extends BaseExtension {
return;
}

const entity = await this.zigbee.resolveEntity(message);
const entity = this.zigbee.resolveEntity(message);
if (!entity || !entity.type === 'device') {
logger.error(`Device '${message}' does not exist`);
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ class HomeAssistant extends BaseExtension {
this.mqtt.subscribe(this.statusTopic);

// MQTT discovery of all paired devices on startup.
for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
const mappedModel = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
if (mappedModel) {
this.discover(device.ieeeAddr, mappedModel, true);
Expand Down Expand Up @@ -1100,7 +1100,7 @@ class HomeAssistant extends BaseExtension {
if (message.toLowerCase() === 'online') {
const timer = setTimeout(async () => {
// Publish all device states.
for (const device of await this.zigbee.getClients()) {
for (const device of this.zigbee.getClients()) {
if (this.state.exists(device.ieeeAddr)) {
this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr));
}
Expand Down
6 changes: 3 additions & 3 deletions lib/extension/networkMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ class NetworkMap extends BaseExtension {

async networkScan(includeRoutes) {
logger.info(`Starting network scan (includeRoutes '${includeRoutes}')`);
const devices = await this.zigbee.getDevices({});
const devices = this.zigbee.getDevices();
const lqis = new Map();
const routingTables = new Map();
const failed = new Map();

for (const device of devices.filter((d) => d.type != 'EndDevice')) {
failed.set(device, []);
const resolved = await this.zigbee.resolveEntity(device);
const resolved = this.zigbee.resolveEntity(device);
try {
const result = await device.lqi();
lqis.set(device, result);
Expand All @@ -154,7 +154,7 @@ class NetworkMap extends BaseExtension {
const networkMap = {nodes: [], links: []};
// Add nodes
for (const device of devices) {
const resolved = await this.zigbee.resolveEntity(device);
const resolved = this.zigbee.resolveEntity(device);
networkMap.nodes.push({
ieeeAddr: device.ieeeAddr, friendlyName: resolved.name, type: device.type,
networkAddress: device.networkAddress, manufacturerName: device.manufacturerName,
Expand Down
41 changes: 22 additions & 19 deletions lib/zigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Zigbee extends events.EventEmitter {
logger.info(`Coordinator firmware version: '${JSON.stringify(await this.getCoordinatorVersion())}'`);
logger.debug(`Zigbee network parameters: ${JSON.stringify(await this.herdsman.getNetworkParameters())}`);

for (const device of await this.getClients()) {
for (const device of this.getClients()) {
// If a whitelist is used, all other device will be removed from the network.
if (settings.get().whitelist.length > 0) {
if (!settings.get().whitelist.includes(device.ieeeAddr)) {
Expand Down Expand Up @@ -102,20 +102,23 @@ class Zigbee extends events.EventEmitter {
return this.herdsman.getPermitJoin();
}

async getClients() {
const devices = await this.herdsman.getDevices({});
return devices.filter((device) => device.type !== 'Coordinator');
getClients() {
return this.herdsman.getDevices().filter((device) => device.type !== 'Coordinator');
}

async getDevices(query) {
return this.herdsman.getDevices(query);
getDevices() {
return this.herdsman.getDevices();
}

async getDevice(query) {
return this.herdsman.getDevice(query);
getDeviceByIeeeAddr(ieeeAddr) {
return this.herdsman.getDeviceByIeeeAddr(ieeeAddr);
}

async resolveEntity(key) {
getDevicesByType(type) {
return this.herdsman.getDevicesByType(type);
}

resolveEntity(key) {
assert(
typeof key === 'string' || typeof key === 'number' ||
key.constructor.name === 'Device', `Wrong type '${typeof key}'`
Expand All @@ -127,7 +130,7 @@ class Zigbee extends events.EventEmitter {
}

if (key === 'coordinator') {
const coordinator = await this.getDevice({type: 'Coordinator'});
const coordinator = this.getDevicesByType('Coordinator')[0];
return {
type: 'device',
device: coordinator,
Expand All @@ -150,7 +153,7 @@ class Zigbee extends events.EventEmitter {
if (!entity) {
return null;
} else if (entity.type === 'device') {
const device = await this.getDevice({ieeeAddr: entity.ID});
const device = this.getDeviceByIeeeAddr(entity.ID);
const mapped = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
const endpoints = mapped && mapped.endpoint ? mapped.endpoint(device) : null;
let isDefaultEndpoint = true;
Expand Down Expand Up @@ -178,8 +181,8 @@ class Zigbee extends events.EventEmitter {
isDefaultEndpoint, endpointName,
};
} else {
let group = await this.getGroup({groupID: entity.ID});
if (!group) group = await this.createGroup(entity.ID);
let group = this.getGroupByID(entity.ID);
if (!group) group = this.createGroup(entity.ID);
return {type: 'group', group, settings: entity, name: entity.friendlyName};
}
} else {
Expand All @@ -194,16 +197,16 @@ class Zigbee extends events.EventEmitter {
}
}

async getGroup(query) {
return this.herdsman.getGroup(query);
getGroupByID(ID) {
return this.herdsman.getGroupByID(ID);
}

async getGroups(query) {
return this.herdsman.getGroups(query);
getGroups() {
return this.herdsman.getGroups();
}

async createGroup(groupID) {
return await this.herdsman.createGroup(groupID);
createGroup(groupID) {
return this.herdsman.createGroup(groupID);
}

// TODO
Expand Down
Loading

0 comments on commit 41e68c8

Please sign in to comment.