Skip to content

Commit

Permalink
feat(cc): implement Inclusion Controller CC
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Jul 26, 2022
1 parent 7877f01 commit 81d1f5d
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 134 deletions.
156 changes: 156 additions & 0 deletions packages/cc/src/cc/InclusionControllerCC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { CommandClasses, Maybe, validatePayload } from "@zwave-js/core";
import type { ZWaveHost } from "@zwave-js/host";
import { CCAPI } from "../lib/API";
import {
CommandClass,
gotDeserializationOptions,
type CCCommandOptions,
type CommandClassDeserializationOptions,
} from "../lib/CommandClass";
import {
API,
CCCommand,
commandClass,
expectedCCResponse,
implementedVersion,
} from "../lib/CommandClassDecorators";
import {
InclusionControllerCommand,
InclusionControllerStatus,
InclusionControllerStep,
} from "../lib/_Types";

@commandClass(CommandClasses["Inclusion Controller"])
@implementedVersion(1)
export class InclusionControllerCC extends CommandClass {
declare ccCommand: InclusionControllerCommand;
}

@API(CommandClasses["Inclusion Controller"])
export class InclusionControllerCCAPI extends CCAPI {
public supportsCommand(cmd: InclusionControllerCommand): Maybe<boolean> {
switch (cmd) {
case InclusionControllerCommand.Initiate:
case InclusionControllerCommand.Complete:
return true; // This is mandatory
}
return super.supportsCommand(cmd);
}

/** Instruct the target to initiate the given inclusion step for the given node */
public async initiateStep(
nodeId: number,
step: InclusionControllerStep,
): Promise<InclusionControllerStatus | undefined> {
this.assertSupportsCommand(
InclusionControllerCommand,
InclusionControllerCommand.Initiate,
);

const cc = new InclusionControllerCCInitiate(this.applHost, {
nodeId: this.endpoint.nodeId,
endpoint: this.endpoint.index,
includedNodeId: nodeId,
step,
});
const response =
await this.applHost.sendCommand<InclusionControllerCCComplete>(
cc,
this.commandOptions,
);

return response?.status;
}

/** Indicate to the other node that the given inclusion step has been completed */
public async completeStep(
step: InclusionControllerStep,
status: InclusionControllerStatus,
): Promise<void> {
this.assertSupportsCommand(
InclusionControllerCommand,
InclusionControllerCommand.Complete,
);

const cc = new InclusionControllerCCComplete(this.applHost, {
nodeId: this.endpoint.nodeId,
endpoint: this.endpoint.index,
step,
status,
});
await this.applHost.sendCommand(cc, this.commandOptions);
}
}

interface InclusionControllerCCCompleteOptions extends CCCommandOptions {
step: InclusionControllerStep;
status: InclusionControllerStatus;
}

@CCCommand(InclusionControllerCommand.Complete)
export class InclusionControllerCCComplete extends InclusionControllerCC {
public constructor(
host: ZWaveHost,
options:
| CommandClassDeserializationOptions
| InclusionControllerCCCompleteOptions,
) {
super(host, options);
if (gotDeserializationOptions(options)) {
validatePayload(this.payload.length >= 2);
this.step = this.payload[0];
validatePayload.withReason("Invalid inclusion controller step")(
this.step in InclusionControllerStep,
);
this.status = this.payload[1];
} else {
this.step = options.step;
this.status = options.status;
}
}

public step: InclusionControllerStep;
public status: InclusionControllerStatus;

public serialize(): Buffer {
this.payload = Buffer.from([this.step, this.status]);
return super.serialize();
}
}

interface InclusionControllerCCInitiateOptions extends CCCommandOptions {
includedNodeId: number;
step: InclusionControllerStep;
}

@CCCommand(InclusionControllerCommand.Initiate)
@expectedCCResponse(InclusionControllerCCComplete)
export class InclusionControllerCCInitiate extends InclusionControllerCC {
public constructor(
host: ZWaveHost,
options:
| CommandClassDeserializationOptions
| InclusionControllerCCInitiateOptions,
) {
super(host, options);
if (gotDeserializationOptions(options)) {
validatePayload(this.payload.length >= 2);
this.includedNodeId = this.payload[0];
this.step = this.payload[1];
validatePayload.withReason("Invalid inclusion controller step")(
this.step in InclusionControllerStep,
);
} else {
this.includedNodeId = options.includedNodeId;
this.step = options.step;
}
}

public includedNodeId: number;
public step: InclusionControllerStep;

public serialize(): Buffer {
this.payload = Buffer.from([this.includedNodeId, this.step]);
return super.serialize();
}
}
5 changes: 5 additions & 0 deletions packages/cc/src/cc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ export {
HumidityControlSetpointCCSupportedReport,
HumidityControlSetpointCCValues,
} from "./HumidityControlSetpointCC";
export {
InclusionControllerCC,
InclusionControllerCCComplete,
InclusionControllerCCInitiate,
} from "./InclusionControllerCC";
export {
IndicatorCC,
IndicatorCCGet,
Expand Down
3 changes: 3 additions & 0 deletions packages/cc/src/lib/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ export type CCToName<CC extends CommandClasses> = [CC] extends [
? "Humidity Control Operating State"
: [CC] extends [typeof CommandClasses["Humidity Control Setpoint"]]
? "Humidity Control Setpoint"
: [CC] extends [typeof CommandClasses["Inclusion Controller"]]
? "Inclusion Controller"
: [CC] extends [typeof CommandClasses["Indicator"]]
? "Indicator"
: [CC] extends [typeof CommandClasses["Irrigation"]]
Expand Down Expand Up @@ -688,6 +690,7 @@ export interface CCAPIs {
"Humidity Control Mode": import("../cc/HumidityControlModeCC").HumidityControlModeCCAPI;
"Humidity Control Operating State": import("../cc/HumidityControlOperatingStateCC").HumidityControlOperatingStateCCAPI;
"Humidity Control Setpoint": import("../cc/HumidityControlSetpointCC").HumidityControlSetpointCCAPI;
"Inclusion Controller": import("../cc/InclusionControllerCC").InclusionControllerCCAPI;
Indicator: import("../cc/IndicatorCC").IndicatorCCAPI;
Irrigation: import("../cc/IrrigationCC").IrrigationCCAPI;
Language: import("../cc/LanguageCC").LanguageCCAPI;
Expand Down

0 comments on commit 81d1f5d

Please sign in to comment.