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: expose Basic CC currentValue when compat flag says so #6964

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 54 additions & 6 deletions packages/cc/src/cc/BasicCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,19 @@ remaining duration: ${basicResponse.duration?.toString() ?? "undefined"}`;
ret.push(...super.getDefinedValueIDs(applHost));
}

if (
applHost.getDeviceConfig?.(endpoint.nodeId)?.compat?.mapBasicSet
=== "event"
) {
const compat = applHost.getDeviceConfig?.(endpoint.nodeId)?.compat;
if (compat?.mapBasicSet === "event") {
// Add the compat event value if it should be exposed
ret.push(BasicCCValues.compatEvent.endpoint(endpoint.index));
} else if (endpoint.controlsCC(CommandClasses.Basic)) {
// Otherwise, only expose currentValue on devices that only control Basic CC
} else if (
!endpoint.supportsCC(CommandClasses.Basic) && (
endpoint.controlsCC(CommandClasses.Basic)
|| compat?.mapBasicReport === false
|| compat?.mapBasicSet === "report"
)
) {
// Otherwise, only expose currentValue on devices that only control Basic CC,
// or devices where a compat flag indicates that currentValue is meant to be exposed
ret.push(BasicCCValues.currentValue.endpoint(endpoint.index));
}

Expand Down Expand Up @@ -431,6 +436,49 @@ export class BasicCCReport extends BasicCC {
@ccValue(BasicCCValues.duration)
public readonly duration: Duration | undefined;

public persistValues(applHost: ZWaveApplicationHost): boolean {
// Basic CC Report persists its values itself, since there are some
// specific rules when which value may be persisted.
// These rules are essentially encoded in the getDefinedValueIDs overload,
// so we simply reuse that here.

// Figure out which values may be persisted.
const definedValueIDs = this.getDefinedValueIDs(applHost);
const shouldPersistCurrentValue = definedValueIDs.some((vid) =>
BasicCCValues.currentValue.is(vid)
);
const shouldPersistTargetValue = definedValueIDs.some((vid) =>
BasicCCValues.targetValue.is(vid)
);
const shouldPersistDuration = definedValueIDs.some((vid) =>
BasicCCValues.duration.is(vid)
);

if (this.currentValue !== undefined && shouldPersistCurrentValue) {
this.setValue(
applHost,
BasicCCValues.currentValue,
this.currentValue,
);
}
if (this.targetValue !== undefined && shouldPersistTargetValue) {
this.setValue(
applHost,
BasicCCValues.targetValue,
this.targetValue,
);
}
if (this.duration !== undefined && shouldPersistDuration) {
this.setValue(
applHost,
BasicCCValues.duration,
this.duration,
);
}

return true;
}

public serialize(): Buffer {
this.payload = Buffer.from([
this.currentValue ?? 0xfe,
Expand Down
3 changes: 3 additions & 0 deletions packages/cc/src/cc/VersionCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ export class VersionCC extends CommandClass {
let logMessage: string;
if (supportedVersion > 0) {
endpoint.addCC(cc, {
isSupported: true,
version: supportedVersion,
});
logMessage = ` supports CC ${CommandClasses[cc]} (${
Expand Down Expand Up @@ -591,6 +592,8 @@ export class VersionCC extends CommandClass {
for (const [cc] of endpoint.getCCs()) {
// We already queried the Version CC version at the start of this interview
if (cc === CommandClasses.Version) continue;
// And we queried Basic CC just before this
if (cc === CommandClasses.Basic) continue;
// Skip the query of endpoint CCs that are also supported by the root device
if (this.endpointIndex > 0 && node.getCCVersion(cc) > 0) continue;
await queryCCVersion(cc);
Expand Down
3 changes: 3 additions & 0 deletions packages/zwave-js/src/lib/node/MockNodeBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const respondToRequestNodeInfo: MockNodeBehavior = {
nodeId: self.id,
...self.capabilities,
supportedCCs: [...self.implementedCCs]
// Basic CC must not be included in the NIF
.filter(([ccId]) => ccId !== CommandClasses.Basic)
// Only include supported CCs
.filter(([, info]) => info.isSupported)
.map(([ccId]) => ccId),
});
Expand Down
6 changes: 5 additions & 1 deletion packages/zwave-js/src/lib/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,18 @@ export function getDefinedValueIDsInternal(
): TranslatedValueID[] {
let ret: ValueID[] = [];
const allowControlled: CommandClasses[] = [
CommandClasses.Basic,
CommandClasses["Scene Activation"],
];
for (const endpoint of getAllEndpoints(applHost, node)) {
for (const cc of allCCs) {
if (
// Create values only for supported CCs
endpoint.supportsCC(cc)
// ...and some controlled CCs
|| (endpoint.controlsCC(cc) && allowControlled.includes(cc))
// ...and possibly Basic CC, which has some extra checks to know
// whether values should be exposed
|| cc === CommandClasses.Basic
) {
const ccInstance = CommandClass.createInstanceUnchecked(
applHost,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { BasicCCValues } from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
import path from "node:path";
import { integrationTest } from "../integrationTestSuite";

integrationTest(
"A Thermostat with compat flag mapBasicReport=false exposes currentValue",
{
// debug: true,

nodeCapabilities: {
manufacturerId: 0xdead,
productType: 0xbeef,
productId: 0xcafe,

// General Thermostat V2
genericDeviceClass: 0x08,
specificDeviceClass: 0x06,
commandClasses: [
CommandClasses["Z-Wave Plus Info"],
// CommandClasses["Multilevel Sensor"],
CommandClasses["Version"],
{
ccId: CommandClasses["Thermostat Setpoint"],
version: 3,
},
{
ccId: CommandClasses["Thermostat Mode"],
version: 3,
},
CommandClasses["Manufacturer Specific"],
// CommandClasses["Meter"],
{
ccId: CommandClasses.Basic,
version: 1,
},
],
},

additionalDriverOptions: {
storage: {
deviceConfigPriorityDir: path.join(
__dirname,
"fixtures/mapBasicReportFalse",
),
},
},

async testBody(t, driver, node, mockController, mockNode) {
// Despite the compat flag, Basic CC should not be considered supported
t.false(node.supportsCC(CommandClasses.Basic));

// But currentValue should be exposed - otherwise it makes no sense to not map it
const valueIDs = node.getDefinedValueIDs();
t.true(
valueIDs.some((v) => BasicCCValues.currentValue.is(v)),
"Did not find Basic CC currentValue although it should be exposed",
);
t.false(
valueIDs.some((v) => BasicCCValues.targetValue.is(v)),
"Found Basic CC targetValue although it shouldn't be exposed",
);
},
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"manufacturer": "Test Manufacturer",
"manufacturerId": "0xdead",
"label": "Test Device",
"description": "With Basic Event",
"devices": [
{
"productType": "0xbeef",
"productId": "0xcafe"
}
],
"firmwareVersion": {
"min": "0.0",
"max": "255.255"
},
"compat": {
"mapBasicReport": false
}
}
Loading