import { clamp } from "alcalzone-shared/math" ; import { deserializers , serializers } from "./conversions" ; import { IPSODevice } from "./ipsoDevice" ; import { deserializeWith , ipsoKey , required , serializeWith } from "./ipsoObject" ; import { Scene } from "./scene" ; export interface GroupInfo { group : Group ; scenes : Record < string , Scene > ; } export class Group extends IPSODevice { @ ipsoKey ( "5850" ) @ required ( ( me , ref ? ) => ref != null && me . sceneId !== ref . sceneId ) public onOff ! : boolean ; @ ipsoKey ( "5851" ) @ serializeWith ( serializers . brightness ) @ deserializeWith ( deserializers . brightness ) public dimmer ! : number ; @ ipsoKey ( "5536" ) @ serializeWith ( serializers . position ) @ deserializeWith ( deserializers . position ) public position ! : number ; @ ipsoKey ( "5523" ) public trigger : number | undefined ; @ ipsoKey ( "9039" ) public sceneId ! : number ; @ ipsoKey ( "9018" ) @ deserializeWith ( obj => parseAccessoryLink ( obj ) ) @ serializeWith ( ids => toAccessoryLink ( ids ) , { splitArrays : false } ) public deviceIDs ! : number [ ] ; internal @ ipsoKey ( "9108" ) public groupType ! : number ; @ ipsoKey ( "5712" ) @ required ( ( me , ref ? ) => ref != null && me . dimmer !== ref . dimmer ) @ serializeWith ( serializers . transitionTime , { neverSkip : true } ) @ deserializeWith ( deserializers . transitionTime , { neverSkip : true } ) public transitionTime ! : number ; throws private ensureLink ( ) { if ( this . client == null ) { throw new Error ( "Cannot use the simplified API on groups which aren't linked to a client instance." ) ; } } /** Turn all lightbulbs on */ public turnOn ( ) { this . ensureLink ( ) ; return this . client ! . operateGroup ( this , { onOff : true , } , true ) ; } /** Turn all lightbulbs off */ public turnOff ( ) { this . ensureLink ( ) ; return this . client ! . operateGroup ( this , { onOff : false , } , true ) ; } /** Set all lightbulbs on/off to the given state */ public toggle ( value ) { this . ensureLink ( ) ; return this . client ! . operateGroup ( this , { onOff : value , } , true ) ; } /** Activates the given scene */ public activateScene ( sceneOrId ) { this . ensureLink ( ) ; const id = ( sceneOrId instanceof Scene ) ? sceneOrId . instanceId : sceneOrId ; return this . client ! . operateGroup ( this , { sceneId : id , onOff : true , } , true ) ; } private operateGroup ( operation , transitionTime ? ) { this . ensureLink ( ) ; if ( transitionTime != null ) { transitionTime = Math . max ( 0 , transitionTime ) ; operation . transitionTime = transitionTime ; } return this . client ! . operateGroup ( this , operation , true ) ; } returns public setBrightness ( value , transitionTime ? ) { value = clamp ( value , 0 , 100 ) ; return this . operateGroup ( { dimmer : value , } , transitionTime ) ; } returns public setPosition ( value ) { value = clamp ( value , 0 , 100 ) ; return this . operateGroup ( { position : value , } ) ; } returns public stopBlinds ( ) { return this . operateGroup ( { trigger : 0.0 , } ) ; } } export type GroupOperation = Partial < Pick < Group , "onOff" | "dimmer" | "position" | "trigger" | "sceneId" | "transitionTime" > > ; interface AccessoryLink { 15002 : { 9003 : number [ ] ; } ; } function parseAccessoryLink ( link ) { const hsLink = link [ "15002" ] ; const deviceIDs = hsLink [ "9003" ] ; return deviceIDs ; } function toAccessoryLink ( ids ) { return { 15002 : { 9003 : ids , } , } ; }