From f467c5aa8b035dab608c55c7679b73d7339fbfe7 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Wed, 16 Oct 2024 16:16:16 +0200 Subject: [PATCH] refactor: remove ZWaveApplicationHost from more methods --- packages/cc/src/cc/AlarmSensorCC.ts | 8 ++-- packages/cc/src/cc/BasicCC.ts | 22 +++++++++-- packages/cc/src/cc/BatteryCC.ts | 18 ++++++++- packages/cc/src/cc/BinarySensorCC.ts | 4 +- packages/cc/src/cc/BinarySwitchCC.ts | 4 +- packages/cc/src/cc/ConfigurationCC.ts | 38 ++++++++++++------- packages/cc/src/cc/IndicatorCC.ts | 14 +++---- .../cc/src/cc/ManufacturerProprietaryCC.ts | 6 +-- packages/cc/src/cc/MeterCC.ts | 19 ++++++++-- packages/cc/src/cc/MultilevelSensorCC.ts | 19 ++++++++-- packages/cc/src/cc/MultilevelSwitchCC.ts | 10 ++--- packages/cc/src/cc/NotificationCC.ts | 37 ++++++++++++------ packages/cc/src/cc/ScheduleEntryLockCC.ts | 8 ++-- packages/cc/src/cc/SupervisionCC.ts | 24 ++++++------ packages/cc/src/cc/UserCodeCC.ts | 29 +++++++------- packages/cc/src/cc/WindowCoveringCC.ts | 6 +-- .../cc/manufacturerProprietary/FibaroCC.ts | 5 ++- packages/cc/src/lib/CommandClass.ts | 18 +++++++-- .../cc/src/lib/EncapsulatingCommandClass.ts | 32 +--------------- packages/cc/src/lib/utils.ts | 4 +- packages/zwave-js/src/lib/node/utils.ts | 22 +++++++++-- 21 files changed, 213 insertions(+), 134 deletions(-) diff --git a/packages/cc/src/cc/AlarmSensorCC.ts b/packages/cc/src/cc/AlarmSensorCC.ts index 7fe5e270a97d..2206e0ce4120 100644 --- a/packages/cc/src/cc/AlarmSensorCC.ts +++ b/packages/cc/src/cc/AlarmSensorCC.ts @@ -306,7 +306,7 @@ duration: ${currentValue.duration}`; } protected createMetadataForSensorType( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, sensorType: AlarmSensorType, ): void { const stateValue = AlarmSensorCCValues.state(sensorType); @@ -314,9 +314,9 @@ duration: ${currentValue.duration}`; const durationValue = AlarmSensorCCValues.duration(sensorType); // Always create metadata if it does not exist - this.ensureMetadata(applHost, stateValue); - this.ensureMetadata(applHost, severityValue); - this.ensureMetadata(applHost, durationValue); + this.ensureMetadata(ctx, stateValue); + this.ensureMetadata(ctx, severityValue); + this.ensureMetadata(ctx, durationValue); } } diff --git a/packages/cc/src/cc/BasicCC.ts b/packages/cc/src/cc/BasicCC.ts index 3dea123b5dae..2d2f840e7a42 100644 --- a/packages/cc/src/cc/BasicCC.ts +++ b/packages/cc/src/cc/BasicCC.ts @@ -1,12 +1,17 @@ import { CommandClasses, + type ControlsCC, Duration, + type EndpointId, + type GetEndpoint, type MaybeNotKnown, type MaybeUnknown, type MessageOrCCLogEntry, MessagePriority, type MessageRecord, + type NodeId, type SupervisionResult, + type SupportsCC, type ValueID, ValueMetadata, maybeUnknownToString, @@ -15,6 +20,9 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, + GetNode, + GetSupportedCCVersion, GetValueDB, ZWaveApplicationHost, } from "@zwave-js/host/safe"; @@ -330,12 +338,18 @@ remaining duration: ${basicResponse.duration?.toString() ?? "undefined"}`; } public override getDefinedValueIDs( - applHost: ZWaveApplicationHost, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): ValueID[] { const ret: ValueID[] = []; - const endpoint = this.getEndpoint(applHost)!; + const endpoint = this.getEndpoint(ctx)!; - const compat = applHost.getDeviceConfig?.(endpoint.nodeId)?.compat; + const compat = ctx.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)); @@ -344,7 +358,7 @@ remaining duration: ${basicResponse.duration?.toString() ?? "undefined"}`; if (endpoint.supportsCC(this.ccId)) { // Defer to the base implementation if Basic CC is supported. // This implies that no other actuator CC is supported. - ret.push(...super.getDefinedValueIDs(applHost)); + ret.push(...super.getDefinedValueIDs(ctx)); } else if (endpoint.controlsCC(CommandClasses.Basic)) { // During the interview, we mark Basic CC as controlled only if we want to expose currentValue ret.push(BasicCCValues.currentValue.endpoint(endpoint.index)); diff --git a/packages/cc/src/cc/BatteryCC.ts b/packages/cc/src/cc/BatteryCC.ts index 659e026b1143..59758631af1e 100644 --- a/packages/cc/src/cc/BatteryCC.ts +++ b/packages/cc/src/cc/BatteryCC.ts @@ -1,8 +1,13 @@ import { timespan } from "@zwave-js/core"; import type { + ControlsCC, + EndpointId, + GetEndpoint, MessageOrCCLogEntry, MessageRecord, + NodeId, SinglecastCC, + SupportsCC, } from "@zwave-js/core/safe"; import { CommandClasses, @@ -15,6 +20,9 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, + GetNode, + GetSupportedCCVersion, GetValueDB, ZWaveApplicationHost, } from "@zwave-js/host/safe"; @@ -366,10 +374,16 @@ temperature: ${batteryHealth.temperature} °C`; public shouldRefreshValues( this: SinglecastCC, - applHost: ZWaveApplicationHost, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): boolean { // Check when the battery state was last updated - const valueDB = applHost.tryGetValueDB(this.nodeId); + const valueDB = ctx.tryGetValueDB(this.nodeId); if (!valueDB) return true; const lastUpdated = valueDB.getTimestamp( diff --git a/packages/cc/src/cc/BinarySensorCC.ts b/packages/cc/src/cc/BinarySensorCC.ts index 3b1b536f97cd..5bd2ed9396d1 100644 --- a/packages/cc/src/cc/BinarySensorCC.ts +++ b/packages/cc/src/cc/BinarySensorCC.ts @@ -325,11 +325,11 @@ export class BinarySensorCC extends CommandClass { } public setMappedBasicValue( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, value: number, ): boolean { this.setValue( - applHost, + ctx, BinarySensorCCValues.state(BinarySensorType.Any), value > 0, ); diff --git a/packages/cc/src/cc/BinarySwitchCC.ts b/packages/cc/src/cc/BinarySwitchCC.ts index a84ec85fcf0c..1232f6a61af2 100644 --- a/packages/cc/src/cc/BinarySwitchCC.ts +++ b/packages/cc/src/cc/BinarySwitchCC.ts @@ -296,10 +296,10 @@ remaining duration: ${resp.duration?.toString() ?? "undefined"}`; } public setMappedBasicValue( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, value: number, ): boolean { - this.setValue(applHost, BinarySwitchCCValues.currentValue, value > 0); + this.setValue(ctx, BinarySwitchCCValues.currentValue, value > 0); return true; } } diff --git a/packages/cc/src/cc/ConfigurationCC.ts b/packages/cc/src/cc/ConfigurationCC.ts index 97efff691d25..c4934b81004f 100644 --- a/packages/cc/src/cc/ConfigurationCC.ts +++ b/packages/cc/src/cc/ConfigurationCC.ts @@ -3,12 +3,17 @@ import { CommandClasses, ConfigValueFormat, type ConfigurationMetadata, + type ControlsCC, + type EndpointId, + type GetEndpoint, type MaybeNotKnown, type MessageOrCCLogEntry, MessagePriority, type MessageRecord, + type NodeId, type SupervisionResult, SupervisionStatus, + type SupportsCC, type ValueID, ValueMetadata, ZWaveError, @@ -28,6 +33,7 @@ import type { CCEncodingContext, CCParsingContext, GetDeviceConfig, + GetNode, GetSupportedCCVersion, GetValueDB, ZWaveApplicationHost, @@ -1299,20 +1305,20 @@ alters capabilities: ${!!properties.altersCapabilities}`; * If this is true, we don't trust what the node reports */ protected paramExistsInConfigFile( - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetDeviceConfig, parameter: number, valueBitMask?: number, ): boolean { if ( this.getValue( - applHost, + ctx, ConfigurationCCValues.isParamInformationFromConfig, ) !== true ) { return false; } const paramInformation = getParamInformationFromConfigFile( - applHost, + ctx, this.nodeId as number, this.endpointIndex, ); @@ -1336,7 +1342,7 @@ alters capabilities: ${!!properties.altersCapabilities}`; * Stores config parameter metadata for this CC's node */ public extendParamInformation( - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetDeviceConfig, parameter: number, valueBitMask: number | undefined, info: Partial, @@ -1344,14 +1350,14 @@ alters capabilities: ${!!properties.altersCapabilities}`; // Don't trust param information that a node reports if we have already loaded it from a config file if ( valueBitMask === undefined - && this.paramExistsInConfigFile(applHost, parameter) + && this.paramExistsInConfigFile(ctx, parameter) ) { return; } // Retrieve the base metadata const metadata = this.getParamInformation( - applHost, + ctx, parameter, valueBitMask, ); @@ -1359,7 +1365,7 @@ alters capabilities: ${!!properties.altersCapabilities}`; Object.assign(metadata, info); // And store it back this.setMetadata( - applHost, + ctx, ConfigurationCCValues.paramInformation(parameter, valueBitMask), metadata, ); @@ -1389,7 +1395,13 @@ alters capabilities: ${!!properties.altersCapabilities}`; * and does not include partial parameters. */ public getQueriedParamInfos( - ctx: GetValueDB & GetSupportedCCVersion & GetDeviceConfig, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): Record { const parameters = distinct( this.getDefinedValueIDs(ctx) @@ -1473,10 +1485,10 @@ alters capabilities: ${!!properties.altersCapabilities}`; /** Deserializes the config parameter info from a config file */ public deserializeParamInformationFromConfig( - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetDeviceConfig, config: ParamInfoMap, ): void { - const valueDB = this.getValueDB(applHost); + const valueDB = this.getValueDB(ctx); // Clear old param information for (const meta of valueDB.getAllMetadata(this.ccId)) { @@ -1496,7 +1508,7 @@ alters capabilities: ${!!properties.altersCapabilities}`; // Allow overwriting the param info (mark it as unloaded) this.setValue( - applHost, + ctx, ConfigurationCCValues.isParamInformationFromConfig, false, ); @@ -1531,7 +1543,7 @@ alters capabilities: ${!!properties.altersCapabilities}`; isFromConfig: true, }); this.extendParamInformation( - applHost, + ctx, param.parameter, param.valueBitMask, paramInfo, @@ -1540,7 +1552,7 @@ alters capabilities: ${!!properties.altersCapabilities}`; // Remember that we loaded the param information from a config file this.setValue( - applHost, + ctx, ConfigurationCCValues.isParamInformationFromConfig, true, ); diff --git a/packages/cc/src/cc/IndicatorCC.ts b/packages/cc/src/cc/IndicatorCC.ts index 0179432368e1..e1fe2ce5e2cf 100644 --- a/packages/cc/src/cc/IndicatorCC.ts +++ b/packages/cc/src/cc/IndicatorCC.ts @@ -850,10 +850,10 @@ export class IndicatorCC extends CommandClass { return super.translateProperty(ctx, property, propertyKey); } - protected supportsV2Indicators(applHost: ZWaveApplicationHost): boolean { + protected supportsV2Indicators(ctx: GetValueDB): boolean { // First test if there are any indicator ids defined const supportedIndicatorIds = this.getValue( - applHost, + ctx, IndicatorCCValues.supportedIndicatorIds, ); if (!supportedIndicatorIds?.length) return false; @@ -861,7 +861,7 @@ export class IndicatorCC extends CommandClass { return supportedIndicatorIds.some( (indicatorId) => !!this.getValue( - applHost, + ctx, IndicatorCCValues.supportedPropertyIDs(indicatorId), )?.length, ); @@ -1119,7 +1119,7 @@ export class IndicatorCCReport extends IndicatorCC { public readonly values: IndicatorObject[] | undefined; private setIndicatorValue( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, value: IndicatorObject, ): void { // Manufacturer-defined indicators may need a custom label @@ -1127,7 +1127,7 @@ export class IndicatorCCReport extends IndicatorCC { value.indicatorId, ) ? this.getValue( - applHost, + ctx, IndicatorCCValues.indicatorDescription(value.indicatorId), ) : undefined; @@ -1147,8 +1147,8 @@ export class IndicatorCCReport extends IndicatorCC { value.indicatorId, value.propertyId, ); - this.setMetadata(applHost, valueV2, metadata); - this.setValue(applHost, valueV2, value.value); + this.setMetadata(ctx, valueV2, metadata); + this.setValue(ctx, valueV2, value.value); } public serialize(ctx: CCEncodingContext): Buffer { diff --git a/packages/cc/src/cc/ManufacturerProprietaryCC.ts b/packages/cc/src/cc/ManufacturerProprietaryCC.ts index a214f19ffc0c..86c1a7de91eb 100644 --- a/packages/cc/src/cc/ManufacturerProprietaryCC.ts +++ b/packages/cc/src/cc/ManufacturerProprietaryCC.ts @@ -42,10 +42,10 @@ export type ManufacturerProprietaryCCConstructor< @API(CommandClasses["Manufacturer Proprietary"]) export class ManufacturerProprietaryCCAPI extends CCAPI { public constructor( - applHost: CCAPIHost, + host: CCAPIHost, endpoint: CCAPIEndpoint, ) { - super(applHost, endpoint); + super(host, endpoint); // Read the manufacturer ID from Manufacturer Specific CC const manufacturerId = this.getValueDB().getValue( @@ -60,7 +60,7 @@ export class ManufacturerProprietaryCCAPI extends CCAPI { SpecificAPIConstructor != undefined && new.target !== SpecificAPIConstructor ) { - return new SpecificAPIConstructor(applHost, endpoint); + return new SpecificAPIConstructor(host, endpoint); } } } diff --git a/packages/cc/src/cc/MeterCC.ts b/packages/cc/src/cc/MeterCC.ts index c45ed14b8092..ed82d2df44c5 100644 --- a/packages/cc/src/cc/MeterCC.ts +++ b/packages/cc/src/cc/MeterCC.ts @@ -12,13 +12,17 @@ import { } from "@zwave-js/core"; import { CommandClasses, + type ControlsCC, type EndpointId, + type GetEndpoint, type MaybeNotKnown, type MessageOrCCLogEntry, MessagePriority, type MessageRecord, + type NodeId, type SinglecastCC, type SupervisionResult, + type SupportsCC, UNKNOWN_STATE, ValueMetadata, parseBitMask, @@ -27,6 +31,9 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, + GetNode, + GetSupportedCCVersion, GetValueDB, ZWaveApplicationHost, } from "@zwave-js/host/safe"; @@ -762,15 +769,21 @@ supports reset: ${suppResp.supportsReset}`; public shouldRefreshValues( this: SinglecastCC, - applHost: ZWaveApplicationHost, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): boolean { // Poll the device when all of the supported values were last updated longer than 6 hours ago. // This may lead to some values not being updated, but the user may have disabled some unnecessary // reports to reduce traffic. - const valueDB = applHost.tryGetValueDB(this.nodeId); + const valueDB = ctx.tryGetValueDB(this.nodeId); if (!valueDB) return true; - const values = this.getDefinedValueIDs(applHost).filter((v) => + const values = this.getDefinedValueIDs(ctx).filter((v) => MeterCCValues.value.is(v) ); return values.every((v) => { diff --git a/packages/cc/src/cc/MultilevelSensorCC.ts b/packages/cc/src/cc/MultilevelSensorCC.ts index 7fcb8074f590..776eff4246f6 100644 --- a/packages/cc/src/cc/MultilevelSensorCC.ts +++ b/packages/cc/src/cc/MultilevelSensorCC.ts @@ -7,12 +7,16 @@ import { timespan, } from "@zwave-js/core"; import type { + ControlsCC, EndpointId, + GetEndpoint, MessageOrCCLogEntry, MessageRecord, + NodeId, Scale, SinglecastCC, SupervisionResult, + SupportsCC, ValueID, } from "@zwave-js/core/safe"; import { @@ -27,6 +31,9 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, + GetNode, + GetSupportedCCVersion, GetUserPreferences, GetValueDB, LogNode, @@ -551,15 +558,21 @@ value: ${mlsResponse.value}${ public shouldRefreshValues( this: SinglecastCC, - applHost: ZWaveApplicationHost, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): boolean { // Poll the device when all of the supported values were last updated longer than 6 hours ago. // This may lead to some values not being updated, but the user may have disabled some unnecessary // reports to reduce traffic. - const valueDB = applHost.tryGetValueDB(this.nodeId); + const valueDB = ctx.tryGetValueDB(this.nodeId); if (!valueDB) return true; - const values = this.getDefinedValueIDs(applHost).filter((v) => + const values = this.getDefinedValueIDs(ctx).filter((v) => MultilevelSensorCCValues.value.is(v) ); return values.every((v) => { diff --git a/packages/cc/src/cc/MultilevelSwitchCC.ts b/packages/cc/src/cc/MultilevelSwitchCC.ts index a96696c780d4..95afbbe73d48 100644 --- a/packages/cc/src/cc/MultilevelSwitchCC.ts +++ b/packages/cc/src/cc/MultilevelSwitchCC.ts @@ -593,24 +593,24 @@ export class MultilevelSwitchCC extends CommandClass { } public setMappedBasicValue( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, value: number, ): boolean { - this.setValue(applHost, MultilevelSwitchCCValues.currentValue, value); + this.setValue(ctx, MultilevelSwitchCCValues.currentValue, value); return true; } protected createMetadataForLevelChangeActions( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, // SDS13781: The Primary Switch Type SHOULD be 0x02 (Up/Down) switchType: SwitchType = SwitchType["Down/Up"], ): void { this.ensureMetadata( - applHost, + ctx, MultilevelSwitchCCValues.levelChangeUp(switchType), ); this.ensureMetadata( - applHost, + ctx, MultilevelSwitchCCValues.levelChangeDown(switchType), ); } diff --git a/packages/cc/src/cc/NotificationCC.ts b/packages/cc/src/cc/NotificationCC.ts index 20d756d97628..eab3b0e84798 100644 --- a/packages/cc/src/cc/NotificationCC.ts +++ b/packages/cc/src/cc/NotificationCC.ts @@ -11,8 +11,10 @@ import { } from "@zwave-js/core"; import { CommandClasses, + type ControlsCC, Duration, type EndpointId, + type GetEndpoint, type MaybeNotKnown, type MessageOrCCLogEntry, MessagePriority, @@ -20,6 +22,7 @@ import { type NodeId, type SinglecastCC, type SupervisionResult, + type SupportsCC, type ValueID, ValueMetadata, type ValueMetadataNumeric, @@ -33,7 +36,11 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, + GetNode, + GetSupportedCCVersion, GetValueDB, + LogNode, ZWaveApplicationHost, } from "@zwave-js/host/safe"; import { buffer2hex, num2hex, pick } from "@zwave-js/shared/safe"; @@ -491,11 +498,11 @@ export class NotificationCC extends CommandClass { } private async determineNotificationMode( - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetNode & LogNode, api: NotificationCCAPI, supportedNotificationEvents: ReadonlyMap, ): Promise<"push" | "pull"> { - const node = this.getNode(applHost)!; + const node = this.getNode(ctx)!; // SDS14223: If the supporting node does not support the Association Command Class, // it may be concluded that the supporting node implements Pull Mode and discovery may be aborted. @@ -505,7 +512,7 @@ export class NotificationCC extends CommandClass { try { const groupsIssueingNotifications = AssociationGroupInfoCC .findGroupsForIssuedCommand( - applHost, + ctx, node, this.ccId, NotificationCommand.Report, @@ -516,7 +523,7 @@ export class NotificationCC extends CommandClass { } } - applHost.logNode(node.id, { + ctx.logNode(node.id, { endpoint: this.endpointIndex, message: `determining whether this node is pull or push...`, direction: "outbound", @@ -552,10 +559,10 @@ export class NotificationCC extends CommandClass { /** Whether the node implements push or pull notifications */ public static getNotificationMode( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, node: NodeId, ): MaybeNotKnown<"push" | "pull"> { - return applHost + return ctx .getValueDB(node.id) .getValue(NotificationCCValues.notificationMode.id); } @@ -884,18 +891,24 @@ export class NotificationCC extends CommandClass { public shouldRefreshValues( this: SinglecastCC, - applHost: ZWaveApplicationHost, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): boolean { // Pull-mode nodes must be polled regularly const isPullMode = NotificationCC.getNotificationMode( - applHost, - this.getNode(applHost)!, + ctx, + this.getNode(ctx)!, ) === "pull"; if (!isPullMode) return false; const lastUpdated = this.getValue( - applHost, + ctx, NotificationCCValues.lastRefresh, ); @@ -1226,7 +1239,7 @@ export class NotificationCCReport extends NotificationCC { }; } - private parseEventParameters(applHost: ZWaveApplicationHost): void { + private parseEventParameters(ctx: LogNode): void { // This only makes sense for V2+ notifications if ( this.notificationType == undefined @@ -1323,7 +1336,7 @@ export class NotificationCCReport extends NotificationCC { userId: this.eventParameters[2], }; } else { - applHost.logNode( + ctx.logNode( this.nodeId as number, `Failed to parse Notification CC event parameters, ignoring them...`, "error", diff --git a/packages/cc/src/cc/ScheduleEntryLockCC.ts b/packages/cc/src/cc/ScheduleEntryLockCC.ts index 351bce154700..0d7360a14586 100644 --- a/packages/cc/src/cc/ScheduleEntryLockCC.ts +++ b/packages/cc/src/cc/ScheduleEntryLockCC.ts @@ -852,7 +852,7 @@ daily repeating: ${slotsResp.numDailyRepeatingSlots}`; } public static getScheduleCached( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, scheduleKind: ScheduleEntryLockScheduleKind.WeekDay, userId: number, @@ -860,7 +860,7 @@ daily repeating: ${slotsResp.numDailyRepeatingSlots}`; ): MaybeNotKnown; public static getScheduleCached( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, scheduleKind: ScheduleEntryLockScheduleKind.YearDay, userId: number, @@ -868,7 +868,7 @@ daily repeating: ${slotsResp.numDailyRepeatingSlots}`; ): MaybeNotKnown; public static getScheduleCached( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, scheduleKind: ScheduleEntryLockScheduleKind.DailyRepeating, userId: number, @@ -877,7 +877,7 @@ daily repeating: ${slotsResp.numDailyRepeatingSlots}`; // Catch-all overload for applications which haven't narrowed `scheduleKind` public static getScheduleCached( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, scheduleKind: ScheduleEntryLockScheduleKind, userId: number, diff --git a/packages/cc/src/cc/SupervisionCC.ts b/packages/cc/src/cc/SupervisionCC.ts index 337bfc379b8b..4faea6a5614f 100644 --- a/packages/cc/src/cc/SupervisionCC.ts +++ b/packages/cc/src/cc/SupervisionCC.ts @@ -21,8 +21,8 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetNode, GetValueDB, - ZWaveApplicationHost, } from "@zwave-js/host/safe"; import { getEnumMemberName } from "@zwave-js/shared/safe"; import { PhysicalCCAPI } from "../lib/API"; @@ -199,13 +199,13 @@ export class SupervisionCC extends CommandClass { * Returns whether a node supports the given CC with Supervision encapsulation. */ public static getCCSupportedWithSupervision( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, ccId: CommandClasses, ): boolean { // By default assume supervision is supported for all CCs, unless we've remembered one not to be return ( - applHost + ctx .getValueDB(endpoint.nodeId) .getValue( SupervisionCCValues.ccSupported(ccId).endpoint( @@ -219,12 +219,12 @@ export class SupervisionCC extends CommandClass { * Remembers whether a node supports the given CC with Supervision encapsulation. */ public static setCCSupportedWithSupervision( - applHost: ZWaveApplicationHost, + ctx: GetValueDB, endpoint: EndpointId, ccId: CommandClasses, supported: boolean, ): void { - applHost + ctx .getValueDB(endpoint.nodeId) .setValue( SupervisionCCValues.ccSupported(ccId).endpoint(endpoint.index), @@ -234,9 +234,11 @@ export class SupervisionCC extends CommandClass { /** Returns whether this is a valid command to send supervised */ public static mayUseSupervision( - applHost: ZWaveApplicationHost< - NodeId & SupportsCC & GetEndpoint - >, + ctx: + & GetValueDB + & GetNode< + NodeId & SupportsCC & GetEndpoint + >, command: T, ): command is SinglecastCC { // Supervision may only be used for singlecast CCs that expect no response @@ -245,9 +247,9 @@ export class SupervisionCC extends CommandClass { if (command.expectsCCResponse()) return false; // with a valid node and endpoint - const node = command.getNode(applHost); + const node = command.getNode(ctx); if (!node) return false; - const endpoint = command.getEndpoint(applHost); + const endpoint = command.getEndpoint(ctx); if (!endpoint) return false; // and only if ... @@ -258,7 +260,7 @@ export class SupervisionCC extends CommandClass { && shouldUseSupervision(command) // ... and we haven't previously determined that the node doesn't properly support it && SupervisionCC.getCCSupportedWithSupervision( - applHost, + ctx, endpoint, command.ccId, ) diff --git a/packages/cc/src/cc/UserCodeCC.ts b/packages/cc/src/cc/UserCodeCC.ts index 8006928e3d93..45e74c1229e9 100644 --- a/packages/cc/src/cc/UserCodeCC.ts +++ b/packages/cc/src/cc/UserCodeCC.ts @@ -17,6 +17,7 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetSupportedCCVersion, GetValueDB, ZWaveApplicationHost, } from "@zwave-js/host/safe"; @@ -186,17 +187,17 @@ function validateCode(code: string, supportedChars: string): boolean { function setUserCodeMetadata( this: UserCodeCC, - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetSupportedCCVersion, userId: number, userCode?: string | Buffer, ) { const statusValue = UserCodeCCValues.userIdStatus(userId); const codeValue = UserCodeCCValues.userCode(userId); - const ccVersion = getEffectiveCCVersion(applHost, this); + const ccVersion = getEffectiveCCVersion(ctx, this); const supportedUserIDStatuses: UserIDStatus[] = - this.getValue(applHost, UserCodeCCValues.supportedUserIDStatuses) + this.getValue(ctx, UserCodeCCValues.supportedUserIDStatuses) ?? (ccVersion === 1 ? [ UserIDStatus.Available, @@ -211,7 +212,7 @@ function setUserCodeMetadata( UserIDStatus.PassageMode, ]); - this.ensureMetadata(applHost, statusValue, { + this.ensureMetadata(ctx, statusValue, { ...statusValue.meta, states: enumValuesToMetadataStates( UserIDStatus, @@ -227,14 +228,14 @@ function setUserCodeMetadata( maxLength: 10, label: `User Code (${userId})`, }; - if (this.getMetadata(applHost, codeValue)?.type !== codeMetadata.type) { - this.setMetadata(applHost, codeValue, codeMetadata); + if (this.getMetadata(ctx, codeValue)?.type !== codeMetadata.type) { + this.setMetadata(ctx, codeValue, codeMetadata); } } function persistUserCode( this: UserCodeCC, - applHost: ZWaveApplicationHost, + ctx: GetValueDB & GetSupportedCCVersion, userId: number, userIdStatus: UserIDStatus, userCode: string | Buffer, @@ -245,15 +246,15 @@ function persistUserCode( // Check if this code is supported if (userIdStatus === UserIDStatus.StatusNotAvailable) { // It is not, remove all values if any exist - this.removeValue(applHost, statusValue); - this.removeValue(applHost, codeValue); - this.removeMetadata(applHost, statusValue); - this.removeMetadata(applHost, codeValue); + this.removeValue(ctx, statusValue); + this.removeValue(ctx, codeValue); + this.removeMetadata(ctx, statusValue); + this.removeMetadata(ctx, codeValue); } else { // Always create metadata in case it does not exist - setUserCodeMetadata.call(this, applHost, userId, userCode); - this.setValue(applHost, statusValue, userIdStatus); - this.setValue(applHost, codeValue, userCode); + setUserCodeMetadata.call(this, ctx, userId, userCode); + this.setValue(ctx, statusValue, userIdStatus); + this.setValue(ctx, codeValue, userCode); } return true; diff --git a/packages/cc/src/cc/WindowCoveringCC.ts b/packages/cc/src/cc/WindowCoveringCC.ts index f8876925abb4..175c3cd92734 100644 --- a/packages/cc/src/cc/WindowCoveringCC.ts +++ b/packages/cc/src/cc/WindowCoveringCC.ts @@ -669,14 +669,14 @@ ${ } public translatePropertyKey( - _applHost: ZWaveApplicationHost, - _property: string | number, + ctx: GetValueDB, + property: string | number, propertyKey: string | number, ): string | undefined { if (typeof propertyKey === "number") { return getEnumMemberName(WindowCoveringParameter, propertyKey); } - return super.translatePropertyKey(_applHost, _property, propertyKey); + return super.translatePropertyKey(ctx, property, propertyKey); } } diff --git a/packages/cc/src/cc/manufacturerProprietary/FibaroCC.ts b/packages/cc/src/cc/manufacturerProprietary/FibaroCC.ts index 0a3aa1b16961..4c7939b6932a 100644 --- a/packages/cc/src/cc/manufacturerProprietary/FibaroCC.ts +++ b/packages/cc/src/cc/manufacturerProprietary/FibaroCC.ts @@ -12,6 +12,7 @@ import { } from "@zwave-js/core/safe"; import type { CCEncodingContext, + GetDeviceConfig, GetValueDB, ZWaveApplicationHost, } from "@zwave-js/host/safe"; @@ -91,10 +92,10 @@ export function getFibaroVenetianBlindTiltMetadata(): ValueMetadata { } function getSupportedFibaroCCIDs( - applHost: ZWaveApplicationHost, + ctx: GetDeviceConfig, nodeId: number, ): FibaroCCIDs[] { - const proprietaryConfig = applHost.getDeviceConfig?.( + const proprietaryConfig = ctx.getDeviceConfig?.( nodeId, )?.proprietary; if (proprietaryConfig && isArray(proprietaryConfig.fibaroCCs)) { diff --git a/packages/cc/src/lib/CommandClass.ts b/packages/cc/src/lib/CommandClass.ts index 8003be18d4c9..a86c5db61c9c 100644 --- a/packages/cc/src/lib/CommandClass.ts +++ b/packages/cc/src/lib/CommandClass.ts @@ -532,7 +532,13 @@ export class CommandClass implements CCId { */ public shouldRefreshValues( this: SinglecastCC, - _applHost: ZWaveApplicationHost, + _ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, ): boolean { // This needs to be overwritten per command class. // In the default implementation, don't require a refresh @@ -564,7 +570,7 @@ export class CommandClass implements CCId { * @param _value The value of the received BasicCC */ public setMappedBasicValue( - _applHost: ZWaveApplicationHost, + _ctx: GetValueDB, _value: number, ): boolean { // By default, don't map @@ -820,7 +826,13 @@ export class CommandClass implements CCId { /** Returns a list of all value names that are defined for this CommandClass */ public getDefinedValueIDs( - ctx: GetValueDB & GetSupportedCCVersion & GetDeviceConfig, + ctx: + & GetValueDB + & GetSupportedCCVersion + & GetDeviceConfig + & GetNode< + NodeId & GetEndpoint + >, includeInternal: boolean = false, ): ValueID[] { // In order to compare value ids, we need them to be strings diff --git a/packages/cc/src/lib/EncapsulatingCommandClass.ts b/packages/cc/src/lib/EncapsulatingCommandClass.ts index 246a4f980069..5f9103e47682 100644 --- a/packages/cc/src/lib/EncapsulatingCommandClass.ts +++ b/packages/cc/src/lib/EncapsulatingCommandClass.ts @@ -1,26 +1,11 @@ -import type { ZWaveApplicationHost } from "@zwave-js/host"; import { isArray } from "alcalzone-shared/typeguards"; -import { CommandClass, type CommandClassOptions } from "./CommandClass"; - -/** Defines the static side of an encapsulating command class */ -export interface EncapsulatingCommandClassStatic { - new ( - applHost: ZWaveApplicationHost, - options: CommandClassOptions, - ): EncapsulatingCommandClass; - - encapsulate( - applHost: ZWaveApplicationHost, - cc: CommandClass, - ): EncapsulatingCommandClass; -} +import { CommandClass } from "./CommandClass"; export type EncapsulatedCommandClass = CommandClass & { encapsulatingCC: EncapsulatingCommandClass; }; export type EncapsulatingCommandClass = CommandClass & { - constructor: EncapsulatingCommandClassStatic; encapsulated: EncapsulatedCommandClass; }; @@ -57,22 +42,7 @@ export function getInnermostCommandClass(cc: CommandClass): CommandClass { } } -/** Defines the static side of an encapsulating command class */ -export interface MultiEncapsulatingCommandClassStatic { - new ( - applHost: ZWaveApplicationHost, - options: CommandClassOptions, - ): MultiEncapsulatingCommandClass; - - requiresEncapsulation(cc: CommandClass): boolean; - encapsulate( - applHost: ZWaveApplicationHost, - CCs: CommandClass[], - ): MultiEncapsulatingCommandClass; -} - export interface MultiEncapsulatingCommandClass { - constructor: MultiEncapsulatingCommandClassStatic; encapsulated: EncapsulatedCommandClass[]; } diff --git a/packages/cc/src/lib/utils.ts b/packages/cc/src/lib/utils.ts index 8c5f0fecb2ce..6a5d7d617db4 100644 --- a/packages/cc/src/lib/utils.ts +++ b/packages/cc/src/lib/utils.ts @@ -654,7 +654,7 @@ export async function removeAssociations( } export function getLifelineGroupIds( - applHost: GetValueDB & GetDeviceConfig, + ctx: GetValueDB & GetDeviceConfig, endpoint: EndpointId & SupportsCC, ): number[] { // For now only support this for the root endpoint - i.e. node @@ -671,7 +671,7 @@ export function getLifelineGroupIds( // We have a device config file that tells us which (additional) association to assign let associations: ReadonlyMap | undefined; - const deviceConfig = applHost.getDeviceConfig?.(endpoint.nodeId); + const deviceConfig = ctx.getDeviceConfig?.(endpoint.nodeId); if (endpoint.index === 0) { // The root endpoint's associations may be configured separately or as part of "endpoints" associations = deviceConfig?.associations diff --git a/packages/zwave-js/src/lib/node/utils.ts b/packages/zwave-js/src/lib/node/utils.ts index 3a2a2c3a12ec..65e84820bd7a 100644 --- a/packages/zwave-js/src/lib/node/utils.ts +++ b/packages/zwave-js/src/lib/node/utils.ts @@ -19,10 +19,10 @@ import { } from "@zwave-js/core"; import type { GetDeviceConfig, + GetNode, GetSupportedCCVersion, GetValueDB, HostIDs, - ZWaveApplicationHost, } from "@zwave-js/host"; function getValue( @@ -309,14 +309,21 @@ export function filterRootApplicationCCValueIDs( /** Returns a list of all value names that are defined on all endpoints of this node */ export function getDefinedValueIDs( - applHost: ZWaveApplicationHost, + ctx: + & HostIDs + & GetValueDB + & GetDeviceConfig + & GetSupportedCCVersion + & GetNode< + NodeId & GetEndpoint + >, node: & NodeId & SupportsCC & ControlsCC & GetEndpoint, ): TranslatedValueID[] { - return getDefinedValueIDsInternal(applHost, node, false); + return getDefinedValueIDsInternal(ctx, node, false); } /** @@ -324,7 +331,14 @@ export function getDefinedValueIDs( * Returns a list of all value names that are defined on all endpoints of this node */ export function getDefinedValueIDsInternal( - ctx: HostIDs & GetValueDB & GetDeviceConfig & GetSupportedCCVersion, + ctx: + & HostIDs + & GetValueDB + & GetDeviceConfig + & GetSupportedCCVersion + & GetNode< + NodeId & GetEndpoint + >, node: & NodeId & SupportsCC