import { CommandClasses , validatePayload , ZWaveError , ZWaveErrorCodes , } from "@zwave-js/core" ; import { pick , staticExtends } from "@zwave-js/shared" ; import { isArray } from "alcalzone-shared/typeguards" ; import type { Driver } from "../driver/Driver" ; import { CCAPI , PollValueImplementation , POLL_VALUE , SetValueImplementation , SET_VALUE , throwMissingPropertyKey , throwUnsupportedProperty , throwUnsupportedPropertyKey , throwWrongValueType , } from "./API" ; import { API , CCCommandOptions , CommandClass , commandClass , CommandClassDeserializationOptions , gotDeserializationOptions , implementedVersion , } from "./CommandClass" ; import { MANUFACTURERID_FIBARO } from "./manufacturerProprietary/Constants" ; import type { FibaroVenetianBlindCCReport } from "./manufacturerProprietary/Fibaro" ; import { getManufacturerIdValueId } from "./ManufacturerSpecificCC" ; @ API ( CommandClasses [ "Manufacturer Proprietary" ] ) export class ManufacturerProprietaryCCAPI extends CCAPI { public async sendData ( manufacturerId , data ? ) { const cc = new ManufacturerProprietaryCC ( this . driver , { nodeId : this . endpoint . nodeId , endpoint : this . endpoint . index } ) ; cc . manufacturerId = manufacturerId ; cc . payload = data ?? Buffer . allocUnsafe ( 0 ) ; await this . driver . sendCommand ( cc , this . commandOptions ) ; } public async fibaroVenetianBlindsGet ( ) { const { FibaroVenetianBlindCCGet } = require ( "./manufacturerProprietary/Fibaro" ) as typeof import ( "./manufacturerProprietary/Fibaro" ) ; const cc = new FibaroVenetianBlindCCGet ( this . driver , { nodeId : this . endpoint . nodeId , endpoint : this . endpoint . index } ) ; const response = await this . driver . sendCommand < FibaroVenetianBlindCCReport > ( cc , this . commandOptions ) ; if ( response ) { return pick ( response , [ "position" , "tilt" ] ) ; } } public async fibaroVenetianBlindsSetPosition ( value ) { const { FibaroVenetianBlindCCSet } = require ( "./manufacturerProprietary/Fibaro" ) as typeof import ( "./manufacturerProprietary/Fibaro" ) ; const cc = new FibaroVenetianBlindCCSet ( this . driver , { nodeId : this . endpoint . nodeId , endpoint : this . endpoint . index , position : value } ) ; await this . driver . sendCommand ( cc , this . commandOptions ) ; } public async fibaroVenetianBlindsSetTilt ( value ) { const { FibaroVenetianBlindCCSet } = require ( "./manufacturerProprietary/Fibaro" ) as typeof import ( "./manufacturerProprietary/Fibaro" ) ; const cc = new FibaroVenetianBlindCCSet ( this . driver , { nodeId : this . endpoint . nodeId , endpoint : this . endpoint . index , tilt : value } ) ; await this . driver . sendCommand ( cc , this . commandOptions ) ; } protected [ SET_VALUE ] : SetValueImplementation = async ( { property , propertyKey } , value ) : Promise < void > => { if ( property !== "fibaro" ) { throwUnsupportedProperty ( this . ccId , property ) ; } if ( propertyKey === "venetianBlindsPosition" ) { if ( typeof value !== "number" ) { throwWrongValueType ( this . ccId , property , "number" , typeof value ) ; } await this . fibaroVenetianBlindsSetPosition ( value ) ; } else if ( propertyKey === "venetianBlindsTilt" ) { if ( typeof value !== "number" ) { throwWrongValueType ( this . ccId , property , "number" , typeof value ) ; } await this . fibaroVenetianBlindsSetTilt ( value ) ; } else { return ; } this . schedulePoll ( { property , propertyKey } ) ; } ; protected [ POLL_VALUE ] : PollValueImplementation = async ( { property , propertyKey , } ) : Promise < unknown > => { if ( property !== "fibaro" ) { throwUnsupportedProperty ( this . ccId , property ) ; } else if ( propertyKey == undefined ) { throwMissingPropertyKey ( this . ccId , property ) ; } switch ( propertyKey ) { case "venetianBlindsPosition" : return ( await this . fibaroVenetianBlindsGet ( ) ) ?. position ; case "venetianBlindsTilt" : return ( await this . fibaroVenetianBlindsGet ( ) ) ?. tilt ; default : throwUnsupportedPropertyKey ( this . ccId , property , propertyKey ) ; } } ; } @ commandClass ( CommandClasses [ "Manufacturer Proprietary" ] ) @ implementedVersion ( 1 ) export class ManufacturerProprietaryCC extends CommandClass { declare ccCommand : undefined ; public constructor ( driver , options ) { super ( driver , options ) ; if ( gotDeserializationOptions ( options ) ) { validatePayload ( this . payload . length >= 1 ) ; this . manufacturerId = ( ( this . ccCommand as unknown as number ) << 8 ) + this . payload [ 0 ] ; this . payload = this . payload . slice ( 1 ) ; const PCConstructor = getProprietaryCCConstructor ( this . manufacturerId ) ; if ( PCConstructor && new . target !== PCConstructor && ! staticExtends ( new . target , PCConstructor ) ) { return new PCConstructor ( driver , options ) ; } } else { this . manufacturerId = this . getValueDB ( ) . getValue < number > ( getManufacturerIdValueId ( ) ) ! ; } } public manufacturerId ! : number ; private assertManufacturerIdIsSet ( ) { if ( this . manufacturerId == undefined ) { throw new ZWaveError ( `To use an instance of ManufacturerProprietaryCC, the manufacturer ID must be stored in the value DB` , ZWaveErrorCodes . ManufacturerProprietaryCC_NoManufacturerId ) ; } } public serialize ( ) { this . assertManufacturerIdIsSet ( ) ; super . ccCommand = ( this . manufacturerId >>> 8 ) & 0xff ; this . payload = Buffer . concat ( [ Buffer . from ( [ this . manufacturerId & 0xff , ] ) , this . payload , ] ) ; return super . serialize ( ) ; } public async interview ( ) { this . assertManufacturerIdIsSet ( ) ; const node = this . getNode ( ) ! ; const proprietaryConfig = node . deviceConfig ?. proprietary ; if ( this . manufacturerId === 0x010f && proprietaryConfig && isArray ( proprietaryConfig . fibaroCCs ) && proprietaryConfig . fibaroCCs . includes ( 0x26 ) ) { const FibaroVenetianBlindCC = ( require ( "./manufacturerProprietary/Fibaro" ) as typeof import ( "./manufacturerProprietary/Fibaro" ) ) . FibaroVenetianBlindCC ; await new FibaroVenetianBlindCC ( this . driver , { nodeId : this . nodeId , endpoint : this . endpointIndex } ) . interview ( ) ; } else { this . driver . controllerLog . logNode ( node . id , { message : `${ this . constructor . name }: skipping interview because none of the implemented proprietary CCs are supported...` , direction : "none" } ) ; } this . interviewComplete = true ; } public async refreshValues ( ) { this . assertManufacturerIdIsSet ( ) ; const node = this . getNode ( ) ! ; const proprietaryConfig = node . deviceConfig ?. proprietary ; if ( this . manufacturerId === 0x010f && proprietaryConfig && isArray ( proprietaryConfig . fibaroCCs ) && proprietaryConfig . fibaroCCs . includes ( 0x26 ) ) { const FibaroVenetianBlindCC = ( require ( "./manufacturerProprietary/Fibaro" ) as typeof import ( "./manufacturerProprietary/Fibaro" ) ) . FibaroVenetianBlindCC ; await new FibaroVenetianBlindCC ( this . driver , { nodeId : this . nodeId , endpoint : this . endpointIndex } ) . refreshValues ( ) ; } else { this . driver . controllerLog . logNode ( node . id , { message : `${ this . constructor . name }: skipping interview because none of the implemented proprietary CCs are supported...` , direction : "none" } ) ; } } } function getProprietaryCCConstructor ( manufacturerId ) { switch ( manufacturerId ) { case MANUFACTURERID_FIBARO : return require ( "./manufacturerProprietary/Fibaro" ) . FibaroCC ; } }