-
-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cc): implement Inclusion Controller CC
- Loading branch information
Showing
4 changed files
with
183 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.