Skip to content

Commit

Permalink
refactor: remove ZWaveApplicationHost from more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 16, 2024
1 parent ea549c4 commit f467c5a
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 134 deletions.
8 changes: 4 additions & 4 deletions packages/cc/src/cc/AlarmSensorCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,17 @@ duration: ${currentValue.duration}`;
}

protected createMetadataForSensorType(
applHost: ZWaveApplicationHost,
ctx: GetValueDB,
sensorType: AlarmSensorType,
): void {
const stateValue = AlarmSensorCCValues.state(sensorType);
const severityValue = AlarmSensorCCValues.severity(sensorType);
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);
}
}

Expand Down
22 changes: 18 additions & 4 deletions packages/cc/src/cc/BasicCC.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,6 +20,9 @@ import {
} from "@zwave-js/core/safe";
import type {
CCEncodingContext,
GetDeviceConfig,
GetNode,
GetSupportedCCVersion,
GetValueDB,
ZWaveApplicationHost,
} from "@zwave-js/host/safe";
Expand Down Expand Up @@ -330,12 +338,18 @@ remaining duration: ${basicResponse.duration?.toString() ?? "undefined"}`;
}

public override getDefinedValueIDs(
applHost: ZWaveApplicationHost<CCNode>,
ctx:
& GetValueDB
& GetSupportedCCVersion
& GetDeviceConfig
& GetNode<
NodeId & GetEndpoint<EndpointId & SupportsCC & ControlsCC>
>,
): 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));
Expand All @@ -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));
Expand Down
18 changes: 16 additions & 2 deletions packages/cc/src/cc/BatteryCC.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,6 +20,9 @@ import {
} from "@zwave-js/core/safe";
import type {
CCEncodingContext,
GetDeviceConfig,
GetNode,
GetSupportedCCVersion,
GetValueDB,
ZWaveApplicationHost,
} from "@zwave-js/host/safe";
Expand Down Expand Up @@ -366,10 +374,16 @@ temperature: ${batteryHealth.temperature} °C`;

public shouldRefreshValues(
this: SinglecastCC<this>,
applHost: ZWaveApplicationHost<CCNode>,
ctx:
& GetValueDB
& GetSupportedCCVersion
& GetDeviceConfig
& GetNode<
NodeId & GetEndpoint<EndpointId & SupportsCC & ControlsCC>
>,
): 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(
Expand Down
4 changes: 2 additions & 2 deletions packages/cc/src/cc/BinarySensorCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/cc/src/cc/BinarySwitchCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
38 changes: 25 additions & 13 deletions packages/cc/src/cc/ConfigurationCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +33,7 @@ import type {
CCEncodingContext,
CCParsingContext,
GetDeviceConfig,
GetNode,
GetSupportedCCVersion,
GetValueDB,
ZWaveApplicationHost,
Expand Down Expand Up @@ -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,
);
Expand All @@ -1336,30 +1342,30 @@ 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<ConfigurationMetadata>,
): void {
// 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,
);
// Override it with new data
Object.assign(metadata, info);
// And store it back
this.setMetadata(
applHost,
ctx,
ConfigurationCCValues.paramInformation(parameter, valueBitMask),
metadata,
);
Expand Down Expand Up @@ -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<EndpointId & SupportsCC & ControlsCC>
>,
): Record<number, ConfigurationMetadata> {
const parameters = distinct(
this.getDefinedValueIDs(ctx)
Expand Down Expand Up @@ -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)) {
Expand All @@ -1496,7 +1508,7 @@ alters capabilities: ${!!properties.altersCapabilities}`;

// Allow overwriting the param info (mark it as unloaded)
this.setValue(
applHost,
ctx,
ConfigurationCCValues.isParamInformationFromConfig,
false,
);
Expand Down Expand Up @@ -1531,7 +1543,7 @@ alters capabilities: ${!!properties.altersCapabilities}`;
isFromConfig: true,
});
this.extendParamInformation(
applHost,
ctx,
param.parameter,
param.valueBitMask,
paramInfo,
Expand All @@ -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,
);
Expand Down
14 changes: 7 additions & 7 deletions packages/cc/src/cc/IndicatorCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,18 +850,18 @@ 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<number[]>(
applHost,
ctx,
IndicatorCCValues.supportedIndicatorIds,
);
if (!supportedIndicatorIds?.length) return false;
// Then test if there are any property ids defined
return supportedIndicatorIds.some(
(indicatorId) =>
!!this.getValue<number[]>(
applHost,
ctx,
IndicatorCCValues.supportedPropertyIDs(indicatorId),
)?.length,
);
Expand Down Expand Up @@ -1119,15 +1119,15 @@ 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
const overrideIndicatorLabel = isManufacturerDefinedIndicator(
value.indicatorId,
)
? this.getValue<string>(
applHost,
ctx,
IndicatorCCValues.indicatorDescription(value.indicatorId),
)
: undefined;
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions packages/cc/src/cc/ManufacturerProprietaryCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(
Expand All @@ -60,7 +60,7 @@ export class ManufacturerProprietaryCCAPI extends CCAPI {
SpecificAPIConstructor != undefined
&& new.target !== SpecificAPIConstructor
) {
return new SpecificAPIConstructor(applHost, endpoint);
return new SpecificAPIConstructor(host, endpoint);
}
}
}
Expand Down
Loading

0 comments on commit f467c5a

Please sign in to comment.