Skip to content

Commit

Permalink
Prefer external converter definitions of zigbee-herdsman-converters d…
Browse files Browse the repository at this point in the history
…efinitions.
  • Loading branch information
Koenkk committed Jul 5, 2021
1 parent 3099beb commit 335742a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function addToLookup(zigbeeModel, definition) {
}

if (!lookup.get(zigbeeModel).includes(definition)) {
lookup.get(zigbeeModel).push(definition);
lookup.get(zigbeeModel).splice(0, 0, definition);
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ function addDefinition(definition) {
}

validateDefinition(definition);
definitions.push(definition);
definitions.splice(0, 0, definition);

if (definition.hasOwnProperty('fingerprint')) {
for (const fingerprint of definition.fingerprint) {
Expand Down Expand Up @@ -128,11 +128,8 @@ function findByDevice(device) {
} else if (candidates.length === 1 && candidates[0].hasOwnProperty('zigbeeModel')) {
return candidates[0];
} else {
// Multiple candidates possible, to use external converters in priority, reverse the order of candidates before searching.
const reversedCandidates = candidates.reverse();

// First try to match based on fingerprint, return the first matching one.
for (const candidate of reversedCandidates) {
for (const candidate of candidates) {
if (candidate.hasOwnProperty('fingerprint')) {
for (const fingerprint of candidate.fingerprint) {
if (fingerprintMatch(fingerprint, device)) {
Expand All @@ -143,7 +140,7 @@ function findByDevice(device) {
}

// Match based on fingerprint failed, return first matching definition based on zigbeeModel
for (const candidate of reversedCandidates) {
for (const candidate of candidates) {
if (candidate.hasOwnProperty('zigbeeModel') && candidate.zigbeeModel.includes(device.modelID)) {
return candidate;
}
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ describe('index.js', () => {
expect(device.model).toBe(mockDevice.model);
});

it('Verify addDeviceDefinition overwrite existing', () => {
const device = {type: 'Router', modelID: 'lumi.light.aqcn02'};
expect(index.findByDevice(device).vendor).toBe('Xiaomi');

const overwriteDefinition = {
model: 'mock-model',
vendor: 'other-vendor',
zigbeeModel: ['lumi.light.aqcn02'],
description: '',
fromZigbee: [],
toZigbee: [],
exposes: []
};
index.addDeviceDefinition(overwriteDefinition);
expect(index.findByDevice(device).vendor).toBe('other-vendor');
});

it('Exposes light with endpoint', () => {
const expected = {
"type":"light",
Expand Down

0 comments on commit 335742a

Please sign in to comment.