-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mock implementation for Meter CC
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 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
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
143 changes: 143 additions & 0 deletions
143
packages/zwave-js/src/lib/node/mockCCBehaviors/Meter.ts
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,143 @@ | ||
import { | ||
MeterCCGet, | ||
MeterCCReport, | ||
MeterCCReset, | ||
MeterCCSupportedGet, | ||
MeterCCSupportedReport, | ||
RateType, | ||
} from "@zwave-js/cc"; | ||
import { CommandClasses } from "@zwave-js/core"; | ||
import { | ||
type MeterCCCapabilities, | ||
type MockNodeBehavior, | ||
MockZWaveFrameType, | ||
createMockZWaveRequestFrame, | ||
} from "@zwave-js/testing"; | ||
|
||
export const defaultCapabilities: MeterCCCapabilities = { | ||
meterType: 0x01, // Electric | ||
supportedScales: [0x00], // kWh | ||
supportedRateTypes: [RateType.Consumed], | ||
supportsReset: true, | ||
}; | ||
|
||
const respondToMeterSupportedGet: MockNodeBehavior = { | ||
async onControllerFrame(controller, self, frame) { | ||
if ( | ||
frame.type === MockZWaveFrameType.Request | ||
&& frame.payload instanceof MeterCCSupportedGet | ||
) { | ||
const capabilities = { | ||
...defaultCapabilities, | ||
...self.getCCCapabilities( | ||
CommandClasses.Meter, | ||
frame.payload.endpointIndex, | ||
), | ||
}; | ||
const cc = new MeterCCSupportedReport(self.host, { | ||
nodeId: controller.host.ownNodeId, | ||
type: capabilities.meterType, | ||
supportedScales: capabilities.supportedScales, | ||
supportedRateTypes: capabilities.supportedRateTypes, | ||
supportsReset: capabilities.supportsReset, | ||
}); | ||
await self.sendToController( | ||
createMockZWaveRequestFrame(cc, { | ||
ackRequested: false, | ||
}), | ||
); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
|
||
const respondToMeterGet: MockNodeBehavior = { | ||
async onControllerFrame(controller, self, frame) { | ||
if ( | ||
frame.type === MockZWaveFrameType.Request | ||
&& frame.payload instanceof MeterCCGet | ||
) { | ||
const capabilities = { | ||
...defaultCapabilities, | ||
...self.getCCCapabilities( | ||
CommandClasses.Meter, | ||
frame.payload.endpointIndex, | ||
), | ||
}; | ||
const scale = frame.payload.scale | ||
?? capabilities.supportedScales[0]; | ||
const rateType = frame.payload.rateType | ||
?? capabilities.supportedRateTypes[0] | ||
?? RateType.Consumed; | ||
|
||
const value = capabilities.getValue?.(scale, rateType) ?? { | ||
value: 0, | ||
deltaTime: 0, | ||
}; | ||
const normalizedValue = typeof value === "number" | ||
? { | ||
value, | ||
deltaTime: 0, | ||
} | ||
: value; | ||
|
||
const cc = new MeterCCReport(self.host, { | ||
nodeId: controller.host.ownNodeId, | ||
type: capabilities.meterType, | ||
scale, | ||
rateType, | ||
...normalizedValue, | ||
}); | ||
await self.sendToController( | ||
createMockZWaveRequestFrame(cc, { | ||
ackRequested: false, | ||
}), | ||
); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
|
||
const respondToMeterReset: MockNodeBehavior = { | ||
onControllerFrame(controller, self, frame) { | ||
if ( | ||
frame.type === MockZWaveFrameType.Request | ||
&& frame.payload instanceof MeterCCReset | ||
) { | ||
const capabilities = { | ||
...defaultCapabilities, | ||
...self.getCCCapabilities( | ||
CommandClasses.Meter, | ||
frame.payload.endpointIndex, | ||
), | ||
}; | ||
|
||
const cc = frame.payload; | ||
if ( | ||
cc.type != undefined | ||
&& cc.scale != undefined | ||
&& cc.rateType != undefined | ||
&& cc.targetValue != undefined | ||
) { | ||
capabilities.onReset?.({ | ||
scale: cc.scale, | ||
rateType: cc.rateType, | ||
targetValue: cc.targetValue, | ||
}); | ||
} else { | ||
capabilities.onReset?.(); | ||
} | ||
|
||
return true; | ||
} | ||
return false; | ||
}, | ||
}; | ||
|
||
export const MeterCCBehaviors = [ | ||
respondToMeterSupportedGet, | ||
respondToMeterGet, | ||
respondToMeterReset, | ||
]; |