Skip to content

Commit

Permalink
fix: correctly match manufacturerCode in findClusterNameByID
Browse files Browse the repository at this point in the history
Previous logic stopped on the first cluster with no manufacturerCode.

Fixes Koenkk/zigbee2mqtt#25333
  • Loading branch information
vmeurisse committed Dec 27, 2024
1 parent 1e6ca6f commit 84a3e25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 16 additions & 14 deletions src/zspec/zcl/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,33 @@ function findClusterNameByID(
manufacturerCode: number | undefined,
clusters: CustomClusters,
): [name: string | undefined, partialMatch: boolean] {
let name: string | undefined;
// if manufacturer code is given, consider partial match if didn't match against manufacturer code
let partialMatch = Boolean(manufacturerCode);
let matchingManufacturer: string | undefined;
let noManufacturer: string | undefined;
let anyManufacturer: string | undefined;

for (const clusterName in clusters) {
const cluster = clusters[clusterName as ClusterName];

if (cluster.ID === id) {
// priority on first match when matching only ID
if (name == undefined) {
name = clusterName;
}

// When a manufacturerCode is requested, match the first cluster with matching manufacturerCode,
if (manufacturerCode && cluster.manufacturerCode === manufacturerCode) {
name = clusterName;
partialMatch = false;
break;
} else if (!cluster.manufacturerCode) {
name = clusterName;
matchingManufacturer = clusterName;
break;

// Otherwise, match the first cluster with no manufacturerCode,
} else if (!noManufacturer && !cluster.manufacturerCode) {
noManufacturer = clusterName;

// Finally, match the first cluster with a matching ID.
} else if (!anyManufacturer) {
anyManufacturer = clusterName;
}
}
}

return [name, partialMatch];
const exactMatch = (manufacturerCode && matchingManufacturer) || (!manufacturerCode && noManufacturer);
const clusterName = matchingManufacturer || noManufacturer || anyManufacturer;
return [clusterName, !exactMatch];
}

function getClusterDefinition(
Expand Down
8 changes: 6 additions & 2 deletions test/zspec/zcl/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ describe('ZCL Utils', () => {
],
[
'by ID with matching manufacturer code',
{key: Zcl.Clusters.sprutDevice.ID, manufacturerCode: Zcl.Clusters.sprutDevice.manufacturerCode!, customClusters: {}},
{cluster: Zcl.Clusters.sprutDevice, name: 'sprutDevice'},
{
key: Zcl.Clusters.manuSpecificNodOnPilotWire.ID,
manufacturerCode: Zcl.Clusters.manuSpecificNodOnPilotWire.manufacturerCode!,
customClusters: {},
},
{cluster: Zcl.Clusters.manuSpecificNodOnPilotWire, name: 'manuSpecificNodOnPilotWire'},
],
[
'custom by ID',
Expand Down

0 comments on commit 84a3e25

Please sign in to comment.