Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add workaround for devices that omit notification parameters instead of sending "no data" #6719

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions packages/cc/src/cc/NotificationCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,17 @@ export class NotificationCCReport extends NotificationCC {
.map(([param, val]) => `\n ${param}: ${num2hex(val)}`)
.join("");
}
} else if (
valueConfig?.parameter
instanceof NotificationParameterWithEnum
&& valueConfig.parameter.default != undefined
) {
const label = valueConfig.parameter.values.get(
valueConfig.parameter.default,
);
if (label) {
message["state parameters"] = `${label} (omitted)`;
}
}
return {
...super.toLogEntry(applHost),
Expand All @@ -1190,11 +1201,10 @@ export class NotificationCCReport extends NotificationCC {
}

private parseEventParameters(applHost: ZWaveApplicationHost): void {
// This only makes sense for V2+ notifications with a non-empty event parameters buffer
// This only makes sense for V2+ notifications
if (
this.notificationType == undefined
|| this.notificationEvent == undefined
|| !Buffer.isBuffer(this.eventParameters)
) {
return;
}
Expand All @@ -1213,6 +1223,10 @@ export class NotificationCCReport extends NotificationCC {
if (
valueConfig.parameter instanceof NotificationParameterWithDuration
) {
// This only makes sense if the event parameters are a buffer
if (!Buffer.isBuffer(this.eventParameters)) {
return;
}
// The parameters contain a Duration
this.eventParameters = Duration.parseReport(
this.eventParameters[0],
Expand All @@ -1221,6 +1235,10 @@ export class NotificationCCReport extends NotificationCC {
valueConfig.parameter
instanceof NotificationParameterWithCommandClass
) {
// This only makes sense if the event parameters are a buffer
if (!Buffer.isBuffer(this.eventParameters)) {
return;
}
// The parameters **should** contain a CC, however there might be some exceptions
if (
this.eventParameters.length === 1
Expand Down Expand Up @@ -1298,6 +1316,10 @@ export class NotificationCCReport extends NotificationCC {
} else if (
valueConfig.parameter instanceof NotificationParameterWithValue
) {
// This only makes sense if the event parameters are a buffer
if (!Buffer.isBuffer(this.eventParameters)) {
return;
}
// The parameters contain a named value
this.eventParameters = {
[valueConfig.parameter.propertyName]: this.eventParameters
Expand All @@ -1310,9 +1332,20 @@ export class NotificationCCReport extends NotificationCC {
valueConfig.parameter instanceof NotificationParameterWithEnum
) {
// The parameters may contain an enum value
this.eventParameters = this.eventParameters.length === 1
this.eventParameters = Buffer.isBuffer(this.eventParameters)
&& this.eventParameters.length === 1
? this.eventParameters[0]
: undefined;

// Some devices send notifications without an event parameter when they should.
// In this case, fall back to the default value where possible.

if (
this.eventParameters == undefined
&& valueConfig.parameter.default != undefined
) {
this.eventParameters = valueConfig.parameter.default;
}
}
}

Expand Down
24 changes: 18 additions & 6 deletions packages/config/config/notifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@
"0x02": "Below low threshold",
"0x03": "Above high threshold",
"0x04": "Max"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand All @@ -318,7 +320,9 @@
"0x02": "Below low threshold",
"0x03": "Above high threshold",
"0x04": "Max"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand All @@ -334,7 +338,9 @@
"0x01": "No data",
"0x02": "Below low threshold",
"0x03": "Above high threshold"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand All @@ -350,7 +356,9 @@
"0x01": "No data",
"0x02": "Below low threshold",
"0x03": "Above high threshold"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand Down Expand Up @@ -1249,7 +1257,9 @@
"0x02": "Below low threshold",
"0x03": "Above high threshold",
"0x04": "Max"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand All @@ -1266,7 +1276,9 @@
"0x02": "Below low threshold",
"0x03": "Above high threshold",
"0x04": "Max"
}
},
// Translate missing event parameter to "no data"
"default": "0x01"
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions packages/config/src/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,34 @@ export class NotificationParameterWithEnum {
}

this.values = values;

if (definition.default != undefined) {
let defaultVal: number;
if (typeof definition.default === "number") {
defaultVal = definition.default;
} else if (
typeof definition.default === "string"
&& hexKeyRegexNDigits.test(definition.default)
) {
defaultVal = parseInt(definition.default, 16);
} else {
throwInvalidConfig(
"notifications",
`The default value for an event parameter enum must be a number or a string in hexadecimal format`,
);
}

if (!values.has(defaultVal)) {
throwInvalidConfig(
"notifications",
`The default value for an event parameter enum must be one of the defined values`,
);
}

this.default = defaultVal;
}
}

public readonly values: ReadonlyMap<number, string>;
public readonly default?: number;
}
107 changes: 107 additions & 0 deletions packages/zwave-js/src/lib/test/cc-specific/notificationEnums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,110 @@ integrationTest("The 'simple' Door state value works correctly", {
t.is(node.getValue(valueSimple.id), 0x17);
},
});

integrationTest(
"Notification types with 'replace'-type enums fall back to the default value if the event parameter is not contained in the CC",
{
// debug: true,

nodeCapabilities: {
commandClasses: [
{
ccId: CommandClasses.Notification,
version: 8,
supportsV1Alarm: false,
notificationTypesAndEvents: {
// Water Alarm - Water pressure alarm status
[0x05]: [0x07],
},
},
],
},

testBody: async (t, driver, node, mockController, mockNode) => {
await node.commandClasses.Notification.getSupportedEvents(0x06);

const waterPressureAlarmValueId =
NotificationCCValues.notificationVariable(
"Water Alarm",
"Water pressure alarm status",
).id;
const states = (
node.getValueMetadata(
waterPressureAlarmValueId,
) as ValueMetadataNumeric
).states;
t.deepEqual(states, {
[0x00]: "idle",
[0x01]: "No data",
[0x02]: "Below low threshold",
[0x03]: "Above high threshold",
[0x04]: "Max",
});

// Send notifications to the node
let cc = new NotificationCCReport(mockNode.host, {
nodeId: mockController.host.ownNodeId,
notificationType: 0x05,
notificationEvent: 0x07,
eventParameters: Buffer.from([0x02]), // Below low threshold
});
await mockNode.sendToController(
createMockZWaveRequestFrame(cc, {
ackRequested: false,
}),
);
// wait a bit for the value to be updated
await wait(100);

let value = node.getValue(waterPressureAlarmValueId);
t.is(value, 0x02);

// Now send one without an event parameter
cc = new NotificationCCReport(mockNode.host, {
nodeId: mockController.host.ownNodeId,
notificationType: 0x05,
notificationEvent: 0x07,
});
await mockNode.sendToController(
createMockZWaveRequestFrame(cc, {
ackRequested: false,
}),
);
await wait(100);

value = node.getValue(waterPressureAlarmValueId);
t.is(value, 0x01);

// cc = new NotificationCCReport(mockNode.host, {
// nodeId: mockController.host.ownNodeId,
// notificationType: 0x06,
// notificationEvent: 0x16, // open
// });
// await mockNode.sendToController(
// createMockZWaveRequestFrame(cc, {
// ackRequested: false,
// }),
// );
// await wait(100);

// value = node.getValue(waterPressureAlarmValueId);
// t.is(value, 0x16);

// cc = new NotificationCCReport(mockNode.host, {
// nodeId: mockController.host.ownNodeId,
// notificationType: 0x06,
// notificationEvent: 0x17, // closed
// });
// await mockNode.sendToController(
// createMockZWaveRequestFrame(cc, {
// ackRequested: false,
// }),
// );
// await wait(100);

// value = node.getValue(waterPressureAlarmValueId);
// t.is(value, 0x17);
},
},
);
Loading