From 4ebae631f983f09fc8571e44a5de90279e0b53c9 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 24 Oct 2023 10:51:29 +0200 Subject: [PATCH 1/3] fix: preserve `stateful` and `secret` flags from stored metadata --- packages/cc/src/index.ts | 7 +++++- packages/zwave-js/src/lib/node/Node.ts | 30 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/cc/src/index.ts b/packages/cc/src/index.ts index 00de6604efd7..689b8eaa6ccc 100644 --- a/packages/cc/src/index.ts +++ b/packages/cc/src/index.ts @@ -20,6 +20,11 @@ export { export * from "./lib/Security2/shared"; export * from "./lib/SetValueResult"; export { defaultCCValueOptions } from "./lib/Values"; -export type { CCValueOptions } from "./lib/Values"; +export type { + CCValue, + CCValueOptions, + DynamicCCValue, + StaticCCValue, +} from "./lib/Values"; export * from "./lib/_Types"; export { utils }; diff --git a/packages/zwave-js/src/lib/node/Node.ts b/packages/zwave-js/src/lib/node/Node.ts index 6bbbb7163c4c..d67749df46e1 100644 --- a/packages/zwave-js/src/lib/node/Node.ts +++ b/packages/zwave-js/src/lib/node/Node.ts @@ -7,6 +7,7 @@ import { CommandClass, DeviceResetLocallyCommand, DoorLockMode, + type DynamicCCValue, EntryControlDataTypes, type FirmwareUpdateCapabilities, type FirmwareUpdateMetaData, @@ -26,6 +27,7 @@ import { ScheduleEntryLockCommand, Security2Command, type SetValueAPIOptions, + type StaticCCValue, TimeCCDateGet, TimeCCTimeGet, TimeCCTimeOffsetGet, @@ -1028,26 +1030,32 @@ export class ZWaveNode extends Endpoint // Check if a corresponding CC value is defined for this value ID // so we can extend the returned metadata const definedCCValues = getCCValues(valueId.commandClass); + let valueDefinition: StaticCCValue | DynamicCCValue | undefined; let valueOptions: Required | undefined; let meta: ValueMetadata | undefined; if (definedCCValues) { - const value = Object.values(definedCCValues).find((v) => + valueDefinition = Object.values(definedCCValues).find((v) => v?.is(valueId) ); - if (value && typeof value !== "function") { - meta = value.meta; - valueOptions = value.options; + if (valueDefinition && typeof valueDefinition !== "function") { + meta = valueDefinition.meta; + valueOptions = valueDefinition.options; } } - // The priority for returned metadata is valueDB > defined value > Any (default) + const existingMetadata = this._valueDB.getMetadata(valueId); return { - ...(this._valueDB.getMetadata(valueId) - ?? meta - ?? ValueMetadata.Any), - // Don't allow overriding these flags: - stateful: valueOptions?.stateful ?? defaultCCValueOptions.stateful, - secret: valueOptions?.secret ?? defaultCCValueOptions.secret, + // The priority for returned metadata is valueDB > defined value > Any (default) + ...(existingMetadata ?? meta ?? ValueMetadata.Any), + // For static values, don't allow overriding these flags with dynamic metadata + stateful: (typeof valueDefinition === "function" + ? existingMetadata?.stateful + : valueOptions?.stateful) + ?? defaultCCValueOptions.stateful, + secret: (typeof valueDefinition === "function" + ? existingMetadata?.secret + : valueOptions?.secret) + ?? defaultCCValueOptions.secret, }; } From 8d951b92b51deb49e35af845ad5af16a434d7942 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 24 Oct 2023 11:04:13 +0200 Subject: [PATCH 2/3] fix: treat meta and options separately --- packages/zwave-js/src/lib/node/Node.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/zwave-js/src/lib/node/Node.ts b/packages/zwave-js/src/lib/node/Node.ts index d67749df46e1..5f7de0f6adb6 100644 --- a/packages/zwave-js/src/lib/node/Node.ts +++ b/packages/zwave-js/src/lib/node/Node.ts @@ -1034,11 +1034,12 @@ export class ZWaveNode extends Endpoint let valueOptions: Required | undefined; let meta: ValueMetadata | undefined; if (definedCCValues) { - valueDefinition = Object.values(definedCCValues).find((v) => - v?.is(valueId) - ); - if (valueDefinition && typeof valueDefinition !== "function") { - meta = valueDefinition.meta; + valueDefinition = Object.values(definedCCValues) + .find((v) => v?.is(valueId)); + if (valueDefinition) { + if (typeof valueDefinition !== "function") { + meta = valueDefinition.meta; + } valueOptions = valueDefinition.options; } } @@ -1047,15 +1048,9 @@ export class ZWaveNode extends Endpoint return { // The priority for returned metadata is valueDB > defined value > Any (default) ...(existingMetadata ?? meta ?? ValueMetadata.Any), - // For static values, don't allow overriding these flags with dynamic metadata - stateful: (typeof valueDefinition === "function" - ? existingMetadata?.stateful - : valueOptions?.stateful) - ?? defaultCCValueOptions.stateful, - secret: (typeof valueDefinition === "function" - ? existingMetadata?.secret - : valueOptions?.secret) - ?? defaultCCValueOptions.secret, + // Don't allow overriding these flags: + stateful: valueOptions?.stateful ?? defaultCCValueOptions.stateful, + secret: valueOptions?.secret ?? defaultCCValueOptions.secret, }; } From 55a7ffb67a52f41db1297ea85d8be2938f2a6c2e Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Tue, 24 Oct 2023 11:08:24 +0200 Subject: [PATCH 3/3] fix: cleanup --- packages/cc/src/index.ts | 7 +------ packages/zwave-js/src/lib/node/Node.ts | 15 ++++++--------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/cc/src/index.ts b/packages/cc/src/index.ts index 689b8eaa6ccc..00de6604efd7 100644 --- a/packages/cc/src/index.ts +++ b/packages/cc/src/index.ts @@ -20,11 +20,6 @@ export { export * from "./lib/Security2/shared"; export * from "./lib/SetValueResult"; export { defaultCCValueOptions } from "./lib/Values"; -export type { - CCValue, - CCValueOptions, - DynamicCCValue, - StaticCCValue, -} from "./lib/Values"; +export type { CCValueOptions } from "./lib/Values"; export * from "./lib/_Types"; export { utils }; diff --git a/packages/zwave-js/src/lib/node/Node.ts b/packages/zwave-js/src/lib/node/Node.ts index 5f7de0f6adb6..543c26eccd09 100644 --- a/packages/zwave-js/src/lib/node/Node.ts +++ b/packages/zwave-js/src/lib/node/Node.ts @@ -7,7 +7,6 @@ import { CommandClass, DeviceResetLocallyCommand, DoorLockMode, - type DynamicCCValue, EntryControlDataTypes, type FirmwareUpdateCapabilities, type FirmwareUpdateMetaData, @@ -27,7 +26,6 @@ import { ScheduleEntryLockCommand, Security2Command, type SetValueAPIOptions, - type StaticCCValue, TimeCCDateGet, TimeCCTimeGet, TimeCCTimeOffsetGet, @@ -1030,17 +1028,16 @@ export class ZWaveNode extends Endpoint // Check if a corresponding CC value is defined for this value ID // so we can extend the returned metadata const definedCCValues = getCCValues(valueId.commandClass); - let valueDefinition: StaticCCValue | DynamicCCValue | undefined; let valueOptions: Required | undefined; let meta: ValueMetadata | undefined; if (definedCCValues) { - valueDefinition = Object.values(definedCCValues) + const value = Object.values(definedCCValues) .find((v) => v?.is(valueId)); - if (valueDefinition) { - if (typeof valueDefinition !== "function") { - meta = valueDefinition.meta; + if (value) { + if (typeof value !== "function") { + meta = value.meta; } - valueOptions = valueDefinition.options; + valueOptions = value.options; } } @@ -1048,7 +1045,7 @@ export class ZWaveNode extends Endpoint return { // The priority for returned metadata is valueDB > defined value > Any (default) ...(existingMetadata ?? meta ?? ValueMetadata.Any), - // Don't allow overriding these flags: + // ...except for these flags, which are taken from defined values: stateful: valueOptions?.stateful ?? defaultCCValueOptions.stateful, secret: valueOptions?.secret ?? defaultCCValueOptions.secret, };