Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Multilevel Switch mocks, add default state for Binary Switch mocks #7270

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/cc/src/cc/MultilevelSwitchCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@

// @publicAPI
export interface MultilevelSwitchCCReportOptions extends CCCommandOptions {
currentValue: number;
targetValue: number;
currentValue: MaybeUnknown<number>;
targetValue: MaybeUnknown<number>;
duration?: Duration | string;
}

Expand Down Expand Up @@ -844,17 +844,27 @@
@useSupervision()
export class MultilevelSwitchCCStopLevelChange extends MultilevelSwitchCC {}

export interface MultilevelSwitchCCSupportedReportOptions {
switchType: SwitchType;
}

@CCCommand(MultilevelSwitchCommand.SupportedReport)
export class MultilevelSwitchCCSupportedReport extends MultilevelSwitchCC {
public constructor(
host: ZWaveHost,
options: CommandClassDeserializationOptions,
options:
| CommandClassDeserializationOptions
| (CCCommandOptions & MultilevelSwitchCCSupportedReportOptions),

Check failure on line 857 in packages/cc/src/cc/MultilevelSwitchCC.ts

View workflow job for this annotation

GitHub Actions / lint (18)

'MultilevelSwitchCCSupportedReportOptions' used in public API must be marked with a @publicapi comment

Check failure on line 857 in packages/cc/src/cc/MultilevelSwitchCC.ts

View workflow job for this annotation

GitHub Actions / lint (18)

'MultilevelSwitchCCSupportedReportOptions' used in public API must be marked with a @publicapi comment
) {
super(host, options);

validatePayload(this.payload.length >= 1);
this.switchType = this.payload[0] & 0b11111;
// We do not support the deprecated secondary switch type
if (gotDeserializationOptions(options)) {
validatePayload(this.payload.length >= 1);
this.switchType = this.payload[0] & 0b11111;
// We do not support the deprecated secondary switch type
} else {
this.switchType = options.switchType;
}
}

// This is the primary switch type. We're not supporting secondary switch types
Expand All @@ -867,6 +877,11 @@
return true;
}

public serialize(): Buffer {
this.payload = Buffer.from([this.switchType & 0b11111]);
return super.serialize();
}

public toLogEntry(host?: ZWaveValueHost): MessageOrCCLogEntry {
return {
...super.toLogEntry(host),
Expand Down
13 changes: 13 additions & 0 deletions packages/testing/src/CCSpecificCapabilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
ColorComponent,
KeypadMode,
SwitchType,
ThermostatMode,
ThermostatSetpointType,
UserIDStatus,
Expand All @@ -10,13 +11,18 @@ import type {
CommandClasses,
ConfigValue,
ConfigValueFormat,
MaybeUnknown,
} from "@zwave-js/core";

export interface BinarySensorCCCapabilities {
supportedSensorTypes: number[];
getValue?: (sensorType: number | undefined) => boolean | undefined;
}

export interface BinarySwitchCCCapabilities {
defaultValue?: MaybeUnknown<boolean>;
}

export interface ConfigurationCCCapabilities {
// We don't have bulk support implemented in the mocks
bulkSupport?: false;
Expand Down Expand Up @@ -78,6 +84,11 @@ export interface MultilevelSensorCCCapabilities {
) => number | undefined;
}

export interface MultilevelSwitchCCCapabilities {
defaultValue: MaybeUnknown<number>;
primarySwitchType: SwitchType;
}

export interface SoundSwitchCCCapabilities {
defaultToneId: number;
defaultVolume: number;
Expand Down Expand Up @@ -153,7 +164,9 @@ export type CCSpecificCapabilities = {
[CommandClasses.Configuration]: ConfigurationCCCapabilities;
[CommandClasses.Notification]: NotificationCCCapabilities;
[48 /* Binary Sensor */]: BinarySensorCCCapabilities;
[0x25 /* Binary Switch */]: BinarySwitchCCCapabilities;
[49 /* Multilevel Sensor */]: MultilevelSensorCCCapabilities;
[0x26 /* Multilevel Switch */]: MultilevelSwitchCCCapabilities;
[51 /* Color Switch */]: ColorSwitchCCCapabilities;
[121 /* Sound Switch */]: SoundSwitchCCCapabilities;
[106 /* Window Covering */]: WindowCoveringCCCapabilities;
Expand Down
2 changes: 2 additions & 0 deletions packages/zwave-js/src/lib/node/MockNodeBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
MultiChannelCCHooks,
} from "./mockCCBehaviors/MultiChannel";
import { MultilevelSensorCCBehaviors } from "./mockCCBehaviors/MultilevelSensor";
import { MultilevelSwitchCCBehaviors } from "./mockCCBehaviors/MultilevelSwitch";
import { NotificationCCBehaviors } from "./mockCCBehaviors/Notification";
import { ScheduleEntryLockCCBehaviors } from "./mockCCBehaviors/ScheduleEntryLock";
import { SoundSwitchCCBehaviors } from "./mockCCBehaviors/SoundSwitch";
Expand Down Expand Up @@ -186,6 +187,7 @@ export function createDefaultBehaviors(): MockNodeBehavior[] {
...ManufacturerSpecificCCBehaviors,
...MeterCCBehaviors,
...MultilevelSensorCCBehaviors,
...MultilevelSwitchCCBehaviors,
...NotificationCCBehaviors,
...ScheduleEntryLockCCBehaviors,
...SoundSwitchCCBehaviors,
Expand Down
24 changes: 21 additions & 3 deletions packages/zwave-js/src/lib/node/mockCCBehaviors/BinarySwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
BinarySwitchCCReport,
BinarySwitchCCSet,
} from "@zwave-js/cc/BinarySwitchCC";
import { type MaybeUnknown, UNKNOWN_STATE } from "@zwave-js/core";
import { type MockNodeBehavior } from "@zwave-js/testing";
import {
CommandClasses,
type MaybeUnknown,
UNKNOWN_STATE,
} from "@zwave-js/core";
Fixed Show fixed Hide fixed
import {
BinarySwitchCCCapabilities,
type MockNodeBehavior,
} from "@zwave-js/testing";

const defaultCapabilities: BinarySwitchCCCapabilities = {
defaultValue: false,
};

const STATE_KEY_PREFIX = "BinarySwitch_";
const StateKeys = {
Expand All @@ -14,10 +25,17 @@
const respondToBinarySwitchGet: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof BinarySwitchCCGet) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(
CommandClasses["Binary Switch"],
receivedCC.endpointIndex,
),
};
const cc = new BinarySwitchCCReport(self.host, {
nodeId: controller.host.ownNodeId,
currentValue: (self.state.get(StateKeys.currentValue)
?? UNKNOWN_STATE) as MaybeUnknown<boolean>,
?? capabilities.defaultValue) as MaybeUnknown<boolean>,
});
return { action: "sendCC", cc };
}
Expand Down
127 changes: 127 additions & 0 deletions packages/zwave-js/src/lib/node/mockCCBehaviors/MultilevelSwitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { SwitchType } from "@zwave-js/cc";
import {
MultilevelSwitchCCGet,
MultilevelSwitchCCReport,
MultilevelSwitchCCSet,
MultilevelSwitchCCStartLevelChange,
MultilevelSwitchCCStopLevelChange,
MultilevelSwitchCCSupportedGet,
MultilevelSwitchCCSupportedReport,
} from "@zwave-js/cc/MultilevelSwitchCC";
import {
CommandClasses,
type MaybeUnknown,
UNKNOWN_STATE,
} from "@zwave-js/core";
import {
type MockNodeBehavior,
MultilevelSwitchCCCapabilities,
} from "@zwave-js/testing";

const defaultCapabilities: MultilevelSwitchCCCapabilities = {
defaultValue: 0,
primarySwitchType: SwitchType["Down/Up"],
};

const STATE_KEY_PREFIX = "MultilevelSwitch_";
const StateKeys = {
currentValue: `${STATE_KEY_PREFIX}currentValue`,
} as const;

const respondToMultilevelSwitchGet: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof MultilevelSwitchCCGet) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(
CommandClasses["Multilevel Switch"],
receivedCC.endpointIndex,
),
};
const currentValue = (self.state.get(StateKeys.currentValue)
?? capabilities.defaultValue
?? UNKNOWN_STATE) as MaybeUnknown<number>;
const cc = new MultilevelSwitchCCReport(self.host, {
nodeId: controller.host.ownNodeId,
currentValue,
// We don't support transitioning yet
targetValue: currentValue,
});
return { action: "sendCC", cc };
}
},
};

const respondToMultilevelSwitchSet: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof MultilevelSwitchCCSet) {
self.state.set(StateKeys.currentValue, receivedCC.targetValue);
return { action: "ok" };
}
},
};

const respondToMultilevelSwitchSupportedGet: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof MultilevelSwitchCCSupportedGet) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(
CommandClasses["Multilevel Switch"],
receivedCC.endpointIndex,
),
};
const cc = new MultilevelSwitchCCSupportedReport(self.host, {
nodeId: controller.host.ownNodeId,
switchType: capabilities.primarySwitchType,
});
return { action: "sendCC", cc };
}
},
};

const respondToMultilevelSwitchStartLevelChange: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof MultilevelSwitchCCStartLevelChange) {
const capabilities = {

Check failure on line 86 in packages/zwave-js/src/lib/node/mockCCBehaviors/MultilevelSwitch.ts

View workflow job for this annotation

GitHub Actions / lint (18)

'capabilities' is assigned a value but never used
Fixed Show fixed Hide fixed
...defaultCapabilities,
...self.getCCCapabilities(
CommandClasses["Color Switch"],
receivedCC.endpointIndex,
),
};

// TODO: A proper simulation should gradually transition the value. We just set it to the target value.
self.state.set(
StateKeys.currentValue,
receivedCC.direction === "up" ? 99 : 0,
);

return { action: "ok" };
}
},
};

const respondToMultilevelSwitchStopLevelChange: MockNodeBehavior = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof MultilevelSwitchCCStopLevelChange) {
const capabilities = {

Check failure on line 108 in packages/zwave-js/src/lib/node/mockCCBehaviors/MultilevelSwitch.ts

View workflow job for this annotation

GitHub Actions / lint (18)

'capabilities' is assigned a value but never used
Fixed Show fixed Hide fixed
...defaultCapabilities,
...self.getCCCapabilities(
CommandClasses["Color Switch"],
receivedCC.endpointIndex,
),
};

return { action: "ok" };
}
},
};

export const MultilevelSwitchCCBehaviors = [
respondToMultilevelSwitchGet,
respondToMultilevelSwitchSet,
respondToMultilevelSwitchSupportedGet,
respondToMultilevelSwitchStartLevelChange,
respondToMultilevelSwitchStopLevelChange,
];
Loading