import { assertZWaveError , CommandClasses , ZWaveErrorCodes , } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { MeterCC , MeterCCGet , MeterCCReport , MeterCCReset , MeterCCSupportedGet , MeterCCSupportedReport , MeterCommand , RateType , } from "./MeterCC" ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses . Meter , ] ) , payload , ] ) ; } describe ( "lib/commandclass/MeterCC => " , ( ) => { let fakeDriver ; let node1 ; beforeAll ( async ( ) => { fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; node1 = new ZWaveNode ( 1 , fakeDriver ) ; ( fakeDriver . controller . nodes as any ) . set ( 1 , node1 ) ; if ( process . env . CI ) jest . setTimeout ( 30000 ) ; await fakeDriver . configManager . loadMeters ( ) ; } ) ; afterAll ( ( ) => { node1 . destroy ( ) ; } ) ; it ( "the Get command (V1) should serialize correctly" , ( ) => { const cc = new MeterCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command (V2) should serialize correctly" , ( ) => { const cc = new MeterCCGet ( fakeDriver , { nodeId : 1 , scale : 0x03 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Get , 24 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command (V3) should serialize correctly" , ( ) => { const cc = new MeterCCGet ( fakeDriver , { nodeId : 1 , scale : 0x06 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Get , 48 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command (V4) should serialize correctly" , ( ) => { const cc = new MeterCCGet ( fakeDriver , { nodeId : 1 , scale : 0x0f } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Get , 56 , 0x1 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the SupportedGet command should serialize correctly" , ( ) => { const cc = new MeterCCSupportedGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . SupportedGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Reset command (V2) should serialize correctly" , ( ) => { const cc = new MeterCCReset ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Reset , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Reset command (V6) should serialize correctly" , ( ) => { const cc = new MeterCCReset ( fakeDriver , { nodeId : 1 , type : 7 , targetValue : 0x010203 , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MeterCommand . Reset , 135 , 0 , 1 , 2 , 3 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (V1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 0x03 , 49 , 55 , ] ) ) ; const cc = new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . type ) . toBe ( 3 ) ; expect ( cc . scale . key ) . toBe ( 2 ) ; expect ( cc . value ) . toBe ( 5.5 ) ; expect ( cc . rateType ) . toBe ( RateType . Unspecified ) ; expect ( cc . deltaTime ) . toBe ( 0 ) ; expect ( cc . previousValue ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (V2) should be deserialized correctly (no time delta)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 67 , 49 , 55 , 0 , 0 , ] ) ) ; const cc = new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . type ) . toBe ( 3 ) ; expect ( cc . scale . key ) . toBe ( 2 ) ; expect ( cc . value ) . toBe ( 5.5 ) ; expect ( cc . rateType ) . toBe ( RateType . Produced ) ; expect ( cc . deltaTime ) . toBe ( 0 ) ; expect ( cc . previousValue ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (V2) should be deserialized correctly (with time delta)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 67 , 49 , 55 , 0 , 5 , 54 , ] ) ) ; const cc = new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . type ) . toBe ( 3 ) ; expect ( cc . scale . key ) . toBe ( 2 ) ; expect ( cc . value ) . toBe ( 5.5 ) ; expect ( cc . rateType ) . toBe ( RateType . Produced ) ; expect ( cc . deltaTime ) . toBe ( 5 ) ; expect ( cc . previousValue ) . toBe ( 5.4 ) ; } ) ; it ( "the Report command (V3) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 193 , 49 , 55 , 0 , 5 , 54 , ] ) ) ; const cc = new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . scale . key ) . toBe ( 6 ) ; } ) ; it ( "the Report command (V4) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 193 , 57 , 55 , 0 , 5 , 54 , 0b01 , ] ) ) ; const cc = new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . scale . key ) . toBe ( 8 ) ; } ) ; it ( "the Report command should validate that a known meter type is given" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 223 , 57 , 55 , 0 , 5 , 54 , 0b01 , ] ) ) ; assertZWaveError ( ( ) => new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) , { errorCode : ZWaveErrorCodes . PacketFormat_InvalidPayload , } ) ; } ) ; it ( "the Report command should validate that a known meter scale is given" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . Report , 196 , 57 , 55 , 0 , 5 , 54 , 0b01 , ] ) ) ; assertZWaveError ( ( ) => new MeterCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) , { errorCode : ZWaveErrorCodes . PacketFormat_InvalidPayload , } ) ; } ) ; it ( "the value IDs should be translated correctly" , ( ) => { expect ( node1 [ "translateValueID" ] ( { commandClass : CommandClasses . Meter , property : "value" , propertyKey : 329986 , } ) ) . toMatchObject ( { propertyKeyName : "Cooling_Unknown (0x09)_Produced" , } ) ; } ) ; it ( "the SupportedReport command (V2/V3) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . SupportedReport , 149 , 0b01101110 , ] ) ) ; const cc = new MeterCCSupportedReport ( fakeDriver , { nodeId : 1 , data : ccData , } ) ; expect ( cc . type ) . toBe ( 21 ) ; expect ( cc . supportsReset ) . toBeTrue ( ) ; expect ( cc . supportedRateTypes ) . toEqual ( [ ] ) ; expect ( cc . supportedScales ) . toEqual ( [ 1 , 2 , 3 , 5 , 6 ] ) ; } ) ; it ( "the SupportedReport command (V4/V5) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MeterCommand . SupportedReport , 213 , 129 , 2 , 1 , 1 , ] ) ) ; const cc = new MeterCCSupportedReport ( fakeDriver , { nodeId : 1 , data : ccData , } ) ; expect ( cc . type ) . toBe ( 21 ) ; expect ( cc . supportsReset ) . toBeTrue ( ) ; expect ( cc . supportedRateTypes ) . toEqual ( [ RateType . Produced ] ) ; expect ( cc . supportedScales ) . toEqual ( [ 0 , 7 , 15 ] ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of MeterCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new MeterCC ( fakeDriver , { nodeId : 1 , data : serializedCC , } ) ; expect ( cc . constructor ) . toBe ( MeterCC ) ; } ) ; } ) ;