|
5 | 5 | JSONPatchWsIncomingMessage, |
6 | 6 | JSONPatchWsOutgoingMessage, |
7 | 7 | PrincessHeaterAccessoryContext, |
8 | | - PrincessHeaterStateWsIncomingMessage, |
| 8 | + PrincessHeaterStateWsIncomingMessage, ResponseWsIncomingMessage, |
9 | 9 | SubscribeWsOutgoingMessage, |
10 | | - WsIncomingMessage, |
| 10 | + WsIncomingMessage, WsOutgoingMessage, |
11 | 11 | } from './ws/types'; |
12 | 12 | import {WsAPIClient} from './ws'; |
13 | 13 | import {MessageType} from './ws/const'; |
@@ -53,12 +53,53 @@ export class HomewizardPrincessHeaterAccessory { |
53 | 53 |
|
54 | 54 | this.platform.log.debug('Subscribing to device updates:', this.accessory.context.device.name); |
55 | 55 |
|
56 | | - wsClient.send<SubscribeWsOutgoingMessage>({ |
| 56 | + this.subscribe().catch( |
| 57 | + err => this.platform.log.error('Failed to subscribe to device ->', this.accessory.context.device.name, err), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + subscribe(): Promise<ResponseWsIncomingMessage> { |
| 62 | + return this.wsClient.send<SubscribeWsOutgoingMessage>({ |
57 | 63 | type: MessageType.SubscribeDevice, |
58 | 64 | device: this.accessory.context.device.identifier, |
59 | 65 | }); |
60 | 66 | } |
61 | 67 |
|
| 68 | + async jsonPatch(path: string, value: boolean | number, callback: CharacteristicSetCallback): Promise<ResponseWsIncomingMessage> { |
| 69 | + |
| 70 | + const message = { |
| 71 | + type: MessageType.JSONPatch, |
| 72 | + device: this.accessory.context.device.identifier, |
| 73 | + patch: [{ |
| 74 | + op: 'replace', |
| 75 | + path: path, |
| 76 | + value, |
| 77 | + }], |
| 78 | + }; |
| 79 | + |
| 80 | + const trySend = () => this.wsClient.send(message).then(m => { |
| 81 | + callback(null); |
| 82 | + return m; |
| 83 | + }); |
| 84 | + |
| 85 | + return trySend() |
| 86 | + .catch(err => { |
| 87 | + if (err.code === 400) { |
| 88 | + this.platform.log.warn('Error code 400. Might mean we need to re-subscribe ->', message, err); |
| 89 | + return this.subscribe().then(() => trySend()); |
| 90 | + } else { |
| 91 | + this.platform.log.error('Failed to send jsonPatch message ->', message, err); |
| 92 | + callback(err); |
| 93 | + throw err; |
| 94 | + } |
| 95 | + }) |
| 96 | + .catch(err => { |
| 97 | + this.platform.log.error('Failed to send jsonPatch message ->', message, err); |
| 98 | + callback(err); |
| 99 | + throw err; |
| 100 | + }); |
| 101 | + } |
| 102 | + |
62 | 103 | onWsMessage(message: WsIncomingMessage) { |
63 | 104 | if ('state' in message) { |
64 | 105 | this.onStateMessage(message as PrincessHeaterStateWsIncomingMessage); |
@@ -146,33 +187,17 @@ export class HomewizardPrincessHeaterAccessory { |
146 | 187 |
|
147 | 188 | this.platform.log.debug('Set Characteristic TargetHeatingCoolingState ->', value); |
148 | 189 |
|
149 | | - this.wsClient.send<JSONPatchWsOutgoingMessage>({ |
150 | | - type: MessageType.JSONPatch, |
151 | | - device: this.accessory.context.device.identifier, |
152 | | - patch: [{ |
153 | | - op: 'replace', |
154 | | - path: '/state/power_on', |
155 | | - value: value === this.platform.Characteristic.TargetHeatingCoolingState.HEAT, |
156 | | - }], |
157 | | - }) |
158 | | - .then(() => callback(null)) |
159 | | - .catch((err) => callback(err)); |
| 190 | + this.jsonPatch( |
| 191 | + '/state/power_on', |
| 192 | + value === this.platform.Characteristic.TargetHeatingCoolingState.HEAT, |
| 193 | + callback, |
| 194 | + ); |
160 | 195 | } |
161 | 196 |
|
162 | 197 | setTargetTemperature(value: CharacteristicValue, callback: CharacteristicSetCallback) { |
163 | 198 |
|
164 | 199 | this.platform.log.debug('Set Characteristic TargetTemperature ->', value); |
165 | 200 |
|
166 | | - this.wsClient.send<JSONPatchWsOutgoingMessage>({ |
167 | | - type: MessageType.JSONPatch, |
168 | | - device: this.accessory.context.device.identifier, |
169 | | - patch: [{ |
170 | | - op: 'replace', |
171 | | - path: '/state/target_temperature', |
172 | | - value: value as number, |
173 | | - }], |
174 | | - }) |
175 | | - .then(() => callback(null)) |
176 | | - .catch((err) => callback(err)); |
| 201 | + this.jsonPatch('/state/target_temperature', value as number, callback); |
177 | 202 | } |
178 | 203 | } |
0 commit comments