@@ -24,7 +24,7 @@ export class HomewizardPrincessHeaterAccessory {
2424 * These are just used to create a working example
2525 * You should implement your own code to track the state of your accessory
2626 */
27- private state : PrincessHeaterState | null = null ;
27+ // private state: PrincessHeaterState | null = null;
2828
2929 constructor (
3030 private readonly platform : HomebridgePrincessHeaterPlatform ,
@@ -77,24 +77,44 @@ export class HomewizardPrincessHeaterAccessory {
7777 }
7878
7979 onJSONMessage ( message : JSONPatchWsIncomingMessage ) {
80- console . log ( 'Incoming JSON patch' , message )
80+ message . patch . forEach ( patchItem => {
81+ const { op, path, value} = patchItem
82+
83+ if ( op === 'replace' ) {
84+ const match = path . match ( / ^ \/ s t a t e \/ ( .* ) $ / )
85+ if ( match && match [ 1 ] ) {
86+ const stateKey = match [ 1 ] ;
87+
88+ switch ( stateKey ) {
89+ case 'power_on' :
90+ this . service . updateCharacteristic (
91+ this . platform . Characteristic . CurrentHeatingCoolingState ,
92+ value ?
93+ this . platform . Characteristic . CurrentHeatingCoolingState . HEAT :
94+ this . platform . Characteristic . CurrentHeatingCoolingState . OFF
95+ ) ;
96+ break ;
97+ case 'current_temperature' :
98+ this . service . updateCharacteristic (
99+ this . platform . Characteristic . CurrentTemperature ,
100+ value
101+ ) ;
102+ break ;
103+ }
104+ }
105+ }
106+ } )
81107 }
82108
83109 onStateMessage ( message : PrincessHeaterStateWsIncomingMessage ) {
84110 this . platform . log . info ( 'Updating state from message ->' , message ) ;
85- this . state = message . state
111+ // this.state = message.state
86112
87113 Object . keys ( message . state ) . forEach ( key => {
88114 const value = message . state [ key ]
89115
90116 switch ( key ) {
91117 case 'power_on' :
92- this . service . setCharacteristic (
93- this . platform . Characteristic . TargetHeatingCoolingState ,
94- value ?
95- this . platform . Characteristic . TargetHeatingCoolingState . HEAT :
96- this . platform . Characteristic . TargetHeatingCoolingState . OFF
97- ) ;
98118 this . service . setCharacteristic (
99119 this . platform . Characteristic . CurrentHeatingCoolingState ,
100120 value ?
@@ -108,89 +128,81 @@ export class HomewizardPrincessHeaterAccessory {
108128 value
109129 ) ;
110130 break ;
111- case 'target_temperature' :
112- this . service . setCharacteristic (
113- this . platform . Characteristic . TargetTemperature ,
114- value
115- ) ;
116- break ;
117131 }
118132 } )
119133 }
120134
121135 setTargetHeatingCoolingState ( value : CharacteristicValue , callback : CharacteristicSetCallback ) {
122- if ( this . state ) {
136+ // if (this.state) {
137+
138+ let stateValue : boolean
139+ let normalizedValue : CharacteristicValue = value
140+
141+ switch ( value ) {
142+ case this . platform . Characteristic . TargetHeatingCoolingState . OFF :
143+ case this . platform . Characteristic . TargetHeatingCoolingState . COOL :
144+ stateValue = false ;
145+ normalizedValue = this . platform . Characteristic . TargetHeatingCoolingState . OFF
146+ break ;
147+ case this . platform . Characteristic . TargetHeatingCoolingState . HEAT :
148+ case this . platform . Characteristic . TargetHeatingCoolingState . AUTO :
149+ stateValue = true ;
150+ normalizedValue = this . platform . Characteristic . TargetHeatingCoolingState . HEAT
151+ break ;
152+ default :
153+ this . platform . log . warn ( 'Setting Characteristic TargetHeatingCoolingState, but value is not supported ->' , value ) ;
154+ callback ( new Error ( 'Unsupported characteristic value' ) )
155+ return ;
156+ }
123157
124- let currentValue : CharacteristicValue = this . state . power_on ?
125- this . platform . Characteristic . TargetHeatingCoolingState . HEAT :
126- this . platform . Characteristic . TargetHeatingCoolingState . OFF ;
158+ this . platform . log . debug ( 'Set Characteristic TargetHeatingCoolingState ->' , value , stateValue ) ;
127159
128- let stateValue : boolean
129- let normalizedValue : CharacteristicValue = value
160+ const message : JSONPatchWsOutgoingMessage = {
161+ type : MessageType . JSONPatch ,
162+ message_id : this . wsClient . generateMessageId ( ) ,
163+ device : this . accessory . context . device . identifier ,
164+ patch : [ {
165+ op : "replace" ,
166+ path : `/state/power_on` ,
167+ value : stateValue
168+ } ]
169+ }
130170
131- switch ( value ) {
132- case this . platform . Characteristic . TargetHeatingCoolingState . OFF :
133- case this . platform . Characteristic . TargetHeatingCoolingState . COOL :
134- stateValue = false ;
135- normalizedValue = this . platform . Characteristic . TargetHeatingCoolingState . OFF
136- break ;
137- case this . platform . Characteristic . TargetHeatingCoolingState . HEAT :
138- case this . platform . Characteristic . TargetHeatingCoolingState . AUTO :
139- stateValue = true ;
140- normalizedValue = this . platform . Characteristic . TargetHeatingCoolingState . HEAT
141- break ;
142- default :
143- this . platform . log . warn ( 'Setting Characteristic TargetHeatingCoolingState, but value is not supported ->' , value ) ;
144- callback ( new Error ( 'Unsupported characteristic value' ) , currentValue )
145- return ;
146- }
171+ this . wsClient . send ( message ) ;
147172
148- this . platform . log . debug ( 'Set Characteristic TargetHeatingCoolingState ->' , value , stateValue ) ;
149-
150- const message : JSONPatchWsOutgoingMessage = {
151- type : MessageType . JSONPatch ,
152- message_id : this . wsClient . generateMessageId ( ) ,
153- device : this . accessory . context . device . identifier ,
154- patch : [ {
155- op : "replace" ,
156- path : `/state/power_on` ,
157- value : stateValue
158- } ]
159- }
173+ this . service . updateCharacteristic ( this . platform . Characteristic . TargetHeatingCoolingState , normalizedValue )
160174
161- this . wsClient . send ( message ) ;
175+ callback ( null )
176+ // } else {
177+ // this.platform.log.warn('Trying to set TargetHeatingCoolingState but state is null');
178+ // callback(new Error('Trying to set TargetHeatingCoolingState but state is null'))
179+ // }
180+ }
162181
163- this . service . updateCharacteristic ( this . platform . Characteristic . TargetHeatingCoolingState , normalizedValue )
182+ setTargetTemperature ( value : CharacteristicValue , callback : CharacteristicSetCallback ) {
183+ // if (this.state) {
164184
165- callback ( null )
166- } else {
167- this . platform . log . warn ( 'Trying to set TargetHeatingCoolingState but state is null' ) ;
168- callback ( new Error ( 'Trying to set TargetHeatingCoolingState but state is null' ) )
185+ const message : JSONPatchWsOutgoingMessage = {
186+ type : MessageType . JSONPatch ,
187+ message_id : this . wsClient . generateMessageId ( ) ,
188+ device : this . accessory . context . device . identifier ,
189+ patch : [ {
190+ op : "replace" ,
191+ path : `/state/target_temperature` ,
192+ value : value
193+ } ]
169194 }
170- }
171195
172- setTargetTemperature ( value : CharacteristicValue , callback : CharacteristicSetCallback ) {
173- if ( this . state ) {
174-
175- const message : JSONPatchWsOutgoingMessage = {
176- type : MessageType . JSONPatch ,
177- message_id : this . wsClient . generateMessageId ( ) ,
178- device : this . accessory . context . device . identifier ,
179- patch : [ {
180- op : "replace" ,
181- path : `/state/target_temperature` ,
182- value : value
183- } ]
184- }
196+ this . platform . log . debug ( 'Set Characteristic TargetTemperature ->' , value ) ;
185197
186- this . platform . log . debug ( 'Set Characteristic TargetTemperature ->' , value ) ;
198+ this . wsClient . send ( message ) ;
187199
188- this . wsClient . send ( message ) ;
200+ this . service . updateCharacteristic ( this . platform . Characteristic . TargetTemperature , value )
189201
190- callback ( null )
191- } else {
192- this . platform . log . warn ( 'Trying to set TargetTemperature but state is null' ) ;
193- callback ( new Error ( 'Trying to set TargetTemperature but state is null' ) )
194- }
202+ callback ( null )
203+ // } else {
204+ // this.platform.log.warn('Trying to set TargetTemperature but state is null');
205+ // callback(new Error('Trying to set TargetTemperature but state is null'))
206+ // }
195207 }
196208}
0 commit comments